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

@ -17,4 +17,20 @@ to produce the output:
<br>
If possible, also describe what happens when the arrays are of different lengths.
;Related tasks:
* &nbsp; [[Loop over multiple arrays simultaneously]]
* &nbsp; [[Loops/Break]]
* &nbsp; [[Loops/Continue]]
* &nbsp; [[Loops/Do-while]]
* &nbsp; [[Loops/Downward for]]
* &nbsp; [[Loops/For]]
* &nbsp; [[Loops/For with a specified step]]
* &nbsp; [[Loops/Foreach]]
* &nbsp; [[Loops/Increment loop index within loop body]]
* &nbsp; [[Loops/Infinite]]
* &nbsp; [[Loops/N plus one half]]
* &nbsp; [[Loops/Nested]]
* &nbsp; [[Loops/While]]
<br><br>

View file

@ -0,0 +1,75 @@
-- CONCAT MAPPED OVER A TRANSPOSITION ----------------------------------------
on run
unlines(map(concat, transpose([["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]])))
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
if length of xs > 0 and class of (item 1 of xs) is string then
set acc to ""
else
set acc to {}
end if
repeat with i from 1 to length of xs
set acc to acc & item i of xs
end repeat
acc
end concat
-- intercalate :: String -> [String] -> String
on intercalate(s, xs)
set {dlm, my text item delimiters} to {my text item delimiters, s}
set str to xs as text
set my text item delimiters to dlm
return str
end intercalate
-- 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
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- transpose :: [[a]] -> [[a]]
on transpose(xss)
script column
on |λ|(_, iCol)
script row
on |λ|(xs)
item iCol of xs
end |λ|
end script
map(row, xss)
end |λ|
end script
map(column, item 1 of xss)
end transpose
-- unlines :: [String] -> String
on unlines(xs)
intercalate(linefeed, xs)
end unlines

View file

@ -11,23 +11,15 @@
});
}
// Sample function over a list
// concat :: [a] -> s
function concat(lst) {
return ''.concat.apply('', lst);
}
// TEST
// TEST
return zipListsWith(
concat,
[["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]]
)
.join('\n');
})();

View file

@ -0,0 +1,33 @@
(() => {
'use strict';
// GENERIC FUNCTIONS -----------------------------------------------------
// concat :: [[a]] -> [a]
const concat = xs =>
xs.length > 0 ? (() => {
const unit = typeof xs[0] === 'string' ? '' : [];
return unit.concat.apply(unit, xs);
})() : [];
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// transpose :: [[a]] -> [[a]]
const transpose = xs =>
xs[0].map((_, col) => xs.map(row => row[col]));
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// TEST ------------------------------------------------------------------
const xs = [
['a', 'b', 'c'],
['A', 'B', 'C'],
[1, 2, 3]
];
return unlines(
map(concat, transpose(xs))
);
})();

View file

@ -1,5 +1 @@
julia> map(println,
('a','b','c'),('A','B','C'),(1,2,3)) ;
aA1
bB2
cC3
foreach(println, ('a', 'b', 'c'), ('A', 'B', 'C'), (1, 2, 3))

View file

@ -1,7 +1,3 @@
julia> for (i,j,k) in
zip(('a','b','c'),('A','B','C'),(1,2,3))
println(i,j,k)
end
aA1
bB2
cC3
for (i, j, k) in zip(('a', 'b', 'c'), ('A', 'B', 'C'), (1, 2, 3))
println(i, j, k)
end

View file

@ -0,0 +1,7 @@
>>blk: [["a" "b" "c"] ["A" "B" "C"] [1 2 3]]
== [["a" "b" "c"] ["A" "B" "C"] [1 2 3]]
>> repeat counter 3 [print [blk/1/:counter blk/2/:counter blk/3/:counter]]
a A 1
b B 2
c C 3

View file

@ -1,3 +1,3 @@
MultiArray.new(%w(a b c),%w(A B C),%w(1 2 3)).each { |i,j,k|
say (i, j, k);
[%w(a b c),%w(A B C),%w(1 2 3)].zip { |i,j,k|
say (i, j, k)
}