Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,19 @@
;; rem : the foldX family always need an initial value
;; fold left a list
(foldl + 0 (iota 10)) ;; 0 + 1 + .. + 9
→ 45
;; fold left a sequence
(lib 'sequences)
(foldl * 1 [ 1 .. 10])
→ 362880 ;; 10!
;; folding left and right
(foldl / 1 ' ( 1 2 3 4))
→ 8/3
(foldr / 1 '(1 2 3 4))
→ 3/8
;;scanl gives the list (or sequence) of intermediate values :
(scanl * 1 '( 1 2 3 4 5))
→ (1 1 2 6 24 120)

View file

@ -0,0 +1,44 @@
' FB 1.05.0 Win64
Type IntFunc As Function(As Integer, As Integer) As Integer
Function reduce(a() As Integer, f As IntFunc) As Integer
'' if array is empty or function pointer is null, return 0 say
If UBound(a) = -1 OrElse f = 0 Then Return 0
Dim result As Integer = a(LBound(a))
For i As Integer = LBound(a) + 1 To UBound(a)
result = f(result, a(i))
Next
Return result
End Function
Function add(x As Integer, y As Integer) As Integer
Return x + y
End Function
Function subtract(x As Integer, y As Integer) As Integer
Return x - y
End Function
Function multiply(x As Integer, y As Integer) As Integer
Return x * y
End Function
Function max(x As Integer, y As Integer) As Integer
Return IIf(x > y, x, y)
End Function
Function min(x As Integer, y As Integer) As Integer
Return IIf(x < y, x, y)
End Function
Dim a(4) As Integer = {1, 2, 3, 4, 5}
Print "Sum is :"; reduce(a(), @add)
Print "Difference is :"; reduce(a(), @subtract)
Print "Product is :"; reduce(a(), @multiply)
Print "Maximum is :"; reduce(a(), @max)
Print "Minimum is :"; reduce(a(), @min)
Print "No op is :"; reduce(a(), 0)
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,19 @@
import sequtils
block:
let
numbers = @[5, 9, 11]
addition = foldl(numbers, a + b)
substraction = foldl(numbers, a - b)
multiplication = foldl(numbers, a * b)
words = @["nim", "rod", "is", "cool"]
concatenation = foldl(words, a & b)
block:
let
numbers = @[5, 9, 11]
addition = foldr(numbers, a + b)
substraction = foldr(numbers, a - b)
multiplication = foldr(numbers, a * b)
words = @["nim", "rod", "is", "cool"]
concatenation = foldr(words, a & b)

View file

@ -0,0 +1,2 @@
[ 1, 2, 3, 4, 5 ] reduce(#max)
[ "abc", "def", "gfi" ] reduce(#+)

View file

@ -0,0 +1,34 @@
n = list(10)
for i = 1 to 10
n[i] = i
next
see " +: " + cat(10,"+") + nl+
" -: " + cat(10,"-") + nl +
" *: " + cat(10,"*") + nl +
" /: " + cat(10,"/") + nl+
" ^: " + cat(10,"^") + nl +
"min: " + cat(10,"min") + nl+
"max: " + cat(10,"max") + nl+
"avg: " + cat(10,"avg") + nl +
"cat: " + cat(10,"cat") + nl
func cat count,op
cat = n[1]
cat2 = ""
for i = 2 to count
switch op
on "+" cat += n[i]
on "-" cat -= n[i]
on "*" cat *= n[i]
on "/" cat /= n[i]
on "^" cat ^= n[i]
on "max" cat = max(cat,n[i])
on "min" cat = min(cat,n[i])
on "avg" cat += n[i]
on "cat" cat2 += string(n[i])
off
next
if op = "avg" cat = cat / count ok
if op = "cat" decimals(0) cat = string(n[1])+cat2 ok
return cat

View file

@ -0,0 +1,2 @@
say (1..10 -> reduce('+'));
say (1..10 -> reduce{|a,b| a + b});

View file

@ -0,0 +1 @@
!/ ^+ [1 2 3] ; returns 6

View file

@ -0,0 +1 @@
@fold ^+ 1 [1 2 3] ; returns 7