September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -3,8 +3,8 @@ on run
set xs to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{map(square, xs), ¬
filter(isEven, xs), ¬
foldl(sum, 0, xs)}
filter(even, xs), ¬
foldl(add, 0, xs)}
--> {{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, {2, 4, 6, 8, 10}, 55}
@ -15,43 +15,19 @@ on square(x)
x * x
end square
-- sum :: Num -> Num -> Num
on sum(a, b)
-- add :: Num -> Num -> Num
on add(a, b)
a + b
end sum
end add
-- isEven :: Int -> Bool
on isEven(n)
n mod 2 = 0
end isEven
-- even :: Int -> Bool
on even(x)
0 = x mod 2
end even
-- GENERIC HIGHER ORDER FUNCTIONS
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
@ -59,20 +35,44 @@ on filter(f, xs)
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if lambda(v, i, xs) then set end of lst to v
if |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map

View file

@ -1,6 +1,8 @@
import system'routines.
import system'routines;
public program
[
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) forEach(:n) [ console writeLine(n * n) ].
]
PrintSecondPower(n){ console.writeLine(n * n) }
public program()
{
new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.forEach:PrintSecondPower
}

View file

@ -0,0 +1 @@
{ #1 #2 #3 #4 #5 } [ #10 * ] a:map [ n:put sp ] a:for-each

View file

@ -0,0 +1,25 @@
Module Program
Function OneMoreThan(i As Integer) As Integer
Return i + 1
End Function
Sub Main()
Dim source As Integer() = {1, 2, 3}
' Create a delegate from an existing method.
Dim resultEnumerable1 = source.Select(AddressOf OneMoreThan)
' The above is just syntax sugar for this; extension methods can be called as if they were instance methods of the first parameter.
resultEnumerable1 = Enumerable.Select(source, AddressOf OneMoreThan)
' Or use an anonymous delegate.
Dim resultEnumerable2 = source.Select(Function(i) i + 1)
' The sequences are the same.
Console.WriteLine(Enumerable.SequenceEqual(resultEnumerable1, resultEnumerable2))
Dim resultArr As Integer() = resultEnumerable1.ToArray()
Array.ForEach(resultArr, AddressOf Console.WriteLine)
End Sub
End Module