June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,13 @@
import system'routines.
import system'math.
import extensions.
import extensions'routines.
program =
[
var array := (1,2,3,4,5).
var evens := array filterBy(:n)(n mod:2 == 0); toArray.
evens forEach:printingLn.
].

View file

@ -0,0 +1,5 @@
public static <T> T[] filter(T[] input, Predicate<T> filterMethod) {
return Arrays.stream(input)
.filter(filterMethod)
.toArray(size -> (T[]) Array.newInstance(input.getClass().getComponentType(), size));
}

View file

@ -0,0 +1,2 @@
Integer[] array = {1, 2, 3, 4, 5};
Integer[] result = filter(array, i -> (i % 2) == 0);

View file

@ -1,8 +1 @@
julia> filter(iseven, [1,2,3,4,5,6,7,8,9])
4-element Array{Int64,1}:
2
4
6
8
julia>
@show filter(iseven, 1:10)

View file

@ -1 +1,2 @@
my @a = 1..6;
my @even = @a.grep(* %% 2);

View file

@ -1 +1,2 @@
my @a = 1..6;
@a .= grep(* %% 2);

View file

@ -0,0 +1,9 @@
Red []
orig: [] repeat i 10 [append orig i]
?? orig
cpy: [] forall orig [if even? orig/1 [append cpy orig/1]]
;; or - because we know each second element is even :- )
;; cpy: extract next orig 2
?? cpy
remove-each ele orig [odd? ele] ;; destructive
?? orig

View file

@ -0,0 +1,47 @@
Option Explicit
Sub Main()
Dim evens() As Long, i As Long
Dim numbers() As Long
For i = 1 To 100000
ReDim Preserve numbers(1 To i)
numbers(i) = i
Next i
evens = FilterInNewArray(numbers)
Debug.Print "Count of initial array : " & UBound(numbers) & ", first item : " & numbers(LBound(numbers)) & ", last item : " & numbers(UBound(numbers))
Debug.Print "Count of new array : " & UBound(evens) & ", first item : " & evens(LBound(evens)) & ", last item : " & evens(UBound(evens))
FilterInPlace numbers
Debug.Print "Count of initial array (filtered): " & UBound(numbers) & ", first item : " & numbers(LBound(numbers)) & ", last item : " & numbers(UBound(numbers))
End Sub
Private Function FilterInNewArray(arr() As Long) As Long()
Dim i As Long, t() As Long, cpt As Long
For i = LBound(arr) To UBound(arr)
If IsEven(arr(i)) Then
cpt = cpt + 1
ReDim Preserve t(1 To cpt)
t(cpt) = i
End If
Next i
FilterInNewArray = t
End Function
Private Sub FilterInPlace(arr() As Long)
Dim i As Long, cpt As Long
For i = LBound(arr) To UBound(arr)
If IsEven(arr(i)) Then
cpt = cpt + 1
arr(cpt) = i
End If
Next i
ReDim Preserve arr(1 To cpt)
End Sub
Private Function IsEven(Number As Long) As Boolean
IsEven = (CLng(Right(CStr(Number), 1)) And 1) = 0
End Function