Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,15 @@
DIM a(4)
a() = 1, 2, 3, 4, 5
PRINT FNreduce(a(), "+")
PRINT FNreduce(a(), "-")
PRINT FNreduce(a(), "*")
END
DEF FNreduce(arr(), op$)
REM!Keep tmp, arr()
LOCAL I%, tmp
tmp = arr(0)
FOR I% = 1 TO DIM(arr(), 1)
tmp = EVAL("tmp " + op$ + " arr(I%)")
NEXT
= tmp

View file

@ -0,0 +1,26 @@
$ list = "1,2,3,4,5"
$ call reduce list "+"
$ show symbol result
$
$ numbers = "5,4,3,2,1"
$ call reduce numbers "-"
$ show symbol result
$
$ call reduce list "*"
$ show symbol result
$ exit
$
$ reduce: subroutine
$ local_list = 'p1
$ value = f$integer( f$element( 0, ",", local_list ))
$ i = 1
$ loop:
$ element = f$element( i, ",", local_list )
$ if element .eqs. "," then $ goto done
$ value = value 'p2 f$integer( element )
$ i = i + 1
$ goto loop
$ done:
$ result == value
$ exit
$ endsubroutine

View file

@ -0,0 +1,6 @@
iex(1)> Enum.reduce(1..10, fn i,acc -> i+acc end)
55
iex(2)> Enum.reduce(1..10, fn i,acc -> i*acc end)
3628800
iex(3)> Enum.reduce(10..-10, "", fn i,acc -> acc <> to_string(i) end)
"109876543210-1-2-3-4-5-6-7-8-9-10"

View file

@ -0,0 +1,16 @@
-module(catamorphism).
-export([test/0]).
test() ->
Nums = lists:seq(1,10),
Summation =
lists:foldl(fun(X, Acc) -> X + Acc end, 0, Nums),
Product =
lists:foldl(fun(X, Acc) -> X * Acc end, 1, Nums),
Concatenation =
lists:foldr(
fun(X, Acc) -> integer_to_list(X) ++ Acc end,
"",
Nums),
{Summation, Product, Concatenation}.

View file

@ -0,0 +1 @@
for op in [+, -, *] println(reduce(op, 1:5)) end