2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,13 +1,20 @@
|
|||
Loop over multiple arrays (or lists or tuples or whatever they're called in
|
||||
your language) and print the ''i''th element of each.
|
||||
Use your language's "for each" loop if it has one, otherwise iterate
|
||||
;Task:
|
||||
Loop over multiple arrays (or lists or tuples or whatever they're called in
|
||||
your language) and display the <big><big> ''i'' <sup>th</sup> </big></big> element of each.
|
||||
|
||||
Use your language's "for each" loop if it has one, otherwise iterate
|
||||
through the collection in order with some other loop.
|
||||
|
||||
For this example, loop over the arrays <code>(a,b,c)</code>,
|
||||
<code>(A,B,C)</code> and <code>(1,2,3)</code>
|
||||
to produce the output
|
||||
<pre>aA1
|
||||
bB2
|
||||
cC3</pre>
|
||||
|
||||
For this example, loop over the arrays:
|
||||
(a,b,c)
|
||||
(A,B,C)
|
||||
(1,2,3)
|
||||
to produce the output:
|
||||
aA1
|
||||
bB2
|
||||
cC3
|
||||
|
||||
<br>
|
||||
If possible, also describe what happens when the arrays are of different lengths.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,89 @@
|
|||
-- zipListsWith :: ([a] -> b) -> [[a]] -> [[b]]
|
||||
on zipListsWith(f, xss)
|
||||
set lngLists to length of xss
|
||||
|
||||
-- appliedToNths :: a -> Int -> [b]
|
||||
script appliedToNths
|
||||
on lambda(_, i)
|
||||
-- nthItem :: [a] -> a
|
||||
script nthItem
|
||||
on lambda(xs)
|
||||
item i of xs
|
||||
end lambda
|
||||
end script
|
||||
|
||||
if i ≤ lngLists then
|
||||
apply(f, (map(nthItem, xss)))
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end lambda
|
||||
end script
|
||||
|
||||
if lngLists > 0 then
|
||||
map(appliedToNths, item 1 of xss)
|
||||
else
|
||||
[]
|
||||
end if
|
||||
end zipListsWith
|
||||
|
||||
|
||||
|
||||
-- TEST
|
||||
|
||||
-- Function to apply:
|
||||
|
||||
-- concatList [String] -> String
|
||||
on concatList(lst)
|
||||
intercalate("", lst)
|
||||
end concatList
|
||||
|
||||
on run
|
||||
-- Application:
|
||||
|
||||
intercalate(linefeed, ¬
|
||||
zipListsWith(concatList, ¬
|
||||
[["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]]))
|
||||
|
||||
end run
|
||||
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- apply (a -> b) -> a -> b
|
||||
on apply(f, a)
|
||||
mReturn(f)'s lambda(a)
|
||||
end apply
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strText}
|
||||
set strJoined to lstText as text
|
||||
set my text item delimiters to dlm
|
||||
return strJoined
|
||||
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 lambda(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 lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
auto lowers = std::vector<char>({'a', 'b', 'c'});
|
||||
auto uppers = std::vector<char>({'A', 'B', 'C'});
|
||||
auto nums = std::vector<int>({1, 2, 3});
|
||||
|
||||
auto ilow = lowers.cbegin();
|
||||
auto iup = uppers.cbegin();
|
||||
auto inum = nums.cbegin();
|
||||
|
||||
for(; ilow != lowers.end()
|
||||
and iup != uppers.end()
|
||||
and inum != nums.end()
|
||||
; ++ilow, ++iup, ++inum)
|
||||
{
|
||||
std::cout << *ilow << *iup << *inum << "\n";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
char lowers[] = {'a', 'b', 'c'};
|
||||
char uppers[] = {'A', 'B', 'C'};
|
||||
int nums[] = {1, 2, 3};
|
||||
|
||||
auto ilow = std::begin(lowers);
|
||||
auto iup = std::begin(uppers);
|
||||
auto inum = std::begin(nums);
|
||||
|
||||
for(; ilow != std::end(lowers)
|
||||
and iup != std::end(uppers)
|
||||
and inum != std::end(nums)
|
||||
; ++ilow, ++iup, ++inum )
|
||||
{
|
||||
std::cout << *ilow << *iup << *inum << "\n";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
auto lowers = std::array<char, 3>({'a', 'b', 'c'});
|
||||
auto uppers = std::array<char, 3>({'A', 'B', 'C'});
|
||||
auto nums = std::array<int, 3>({1, 2, 3});
|
||||
|
||||
auto ilow = lowers.cbegin();
|
||||
auto iup = uppers.cbegin();
|
||||
auto inum = nums.cbegin();
|
||||
|
||||
for(; ilow != lowers.end()
|
||||
and iup != uppers.end()
|
||||
and inum != nums.end()
|
||||
; ++ilow, ++iup, ++inum )
|
||||
{
|
||||
std::cout << *ilow << *iup << *inum << "\n";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#include <iostream>
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
auto lowers = std::array<char, 3>({'a', 'b', 'c'});
|
||||
auto uppers = std::array<char, 3>({'A', 'B', 'C'});
|
||||
auto nums = std::array<int, 3>({1, 2, 3});
|
||||
|
||||
auto const minsize = std::min(
|
||||
lowers.size(),
|
||||
std::min(
|
||||
uppers.size(),
|
||||
nums.size()
|
||||
)
|
||||
);
|
||||
|
||||
for(size_t i = 0; i < minsize; ++i)
|
||||
{
|
||||
std::cout << lowers[i] << uppers[i] << nums[i] << "\n";
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,12 @@
|
|||
open console list imperative
|
||||
open monad io list imperative
|
||||
|
||||
xs = zipWith3 (\x y z -> show x ++ show y ++ show z) ['a','b','c']
|
||||
['A','B','C'] [1,2,3]
|
||||
['A','B','C'] [1,2,3]
|
||||
|
||||
each writen xs
|
||||
print x = do putStrLn x
|
||||
|
||||
print_and_calc xs = do
|
||||
xss <- return xs
|
||||
return $ each print xss
|
||||
|
||||
print_and_calc xs ::: IO
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
#import system.
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
#var a1 := ("a","b","c").
|
||||
#var a2 := ("A","B","C").
|
||||
#var a3 := (1,2,3).
|
||||
|
||||
#var i := Integer new:0.
|
||||
#loop (i < a1 length)?
|
||||
[
|
||||
console writeLine:(a1@i + a2@i + (a3@i) literal).
|
||||
i := i + 1.
|
||||
].
|
||||
|
||||
console readChar.
|
||||
].
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
#import system.
|
||||
#import system'routines.
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
#var a1 := ("a","b","c").
|
||||
#var a2 := ("A","B","C").
|
||||
#var a3 := (1,2,3).
|
||||
#var zipped := (a1 zip: a2 &into:(:first:second) [ first + second ])
|
||||
zip: a3 &into:(:first:second) [ first + (second literal)].
|
||||
|
||||
zipped run &each: e
|
||||
[ console writeLine:e. ].
|
||||
|
||||
console readChar.
|
||||
].
|
||||
|
|
@ -1,24 +1,33 @@
|
|||
(function (lists) {
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
// [[a]] -> [[a]]
|
||||
function zip(lists) {
|
||||
var lng = lists.length,
|
||||
lstHead = lng ? [].concat.apply([], lists.map(function (lst) {
|
||||
return lst.length ? [lst[0]] : [];
|
||||
})) : [];
|
||||
|
||||
return lstHead.length === lng ? [lstHead].concat(
|
||||
zip(lists.map(function (x) {
|
||||
return x.slice(1);
|
||||
}))
|
||||
) : [];
|
||||
// zipListsWith :: ([a] -> b) -> [[a]] -> [[b]]
|
||||
function zipListsWith(f, xss) {
|
||||
return (xss.length ? xss[0] : [])
|
||||
.map(function (_, i) {
|
||||
return f(xss.map(function (xs) {
|
||||
return xs[i];
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
// [a] -> s
|
||||
|
||||
|
||||
|
||||
// Sample function over a list
|
||||
|
||||
// concat :: [a] -> s
|
||||
function concat(lst) {
|
||||
return ''.concat.apply('', lst);
|
||||
}
|
||||
|
||||
return zip(lists).map(concat).join('\n')
|
||||
|
||||
})([["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]]);
|
||||
// TEST
|
||||
|
||||
return zipListsWith(
|
||||
concat,
|
||||
[["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]]
|
||||
)
|
||||
.join('\n');
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
for <a b c> Z <A B C> Z 1, 2, 3 -> $x, $y, $z {
|
||||
for <a b c> Z <A B C> Z 1, 2, 3 -> ($x, $y, $z) {
|
||||
say $x, $y, $z;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
for ^Inf Z <a b c d> -> ($i, $letter) { ... }
|
||||
|
|
@ -0,0 +1 @@
|
|||
for <a b c d>.kv -> $i, $letter { ... }
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
function zip3 ($a1, $a2, $a3)
|
||||
{
|
||||
while ($a1)
|
||||
{
|
||||
$x, $a1 = $a1
|
||||
$y, $a2 = $a2
|
||||
$z, $a3 = $a3
|
||||
[Tuple]::Create($x, $y, $z)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
zip3 @('a','b','c') @('A','B','C') @(1,2,3)
|
||||
|
|
@ -0,0 +1 @@
|
|||
zip3 @('a','b','c') @('A','B','C') @(1,2,3) | ForEach-Object {$_.Item1 + $_.Item2 + $_.Item3}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
/*REXX program shows how to simultaneously loop over
|
||||
multiple arrays.*/
|
||||
/*REXX program shows how to simultaneously loop over multiple arrays.*/
|
||||
x. = ' '; x.1 = "a"; x.2 = 'b'; x.3 = "c"
|
||||
y. = ' '; y.1 = "A"; y.2 = 'B'; y.3 = "C"
|
||||
z. = ' '; z.1 = "1"; z.2 = '2'; z.3 = "3"
|
||||
|
|
@ -7,5 +6,4 @@ z. = ' '; z.1 = "1"; z.2 = '2'; z.3 = "3"
|
|||
do j=1 until output=''
|
||||
output = x.j || y.j || z.j
|
||||
say output
|
||||
end /*j*/ /*stick a fork in it, we're
|
||||
done.*/
|
||||
end /*j*/ /*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -1,12 +1,9 @@
|
|||
/*REXX program shows how to simultaneously loop over
|
||||
multiple arrays.*/
|
||||
/*REXX program shows how to simultaneously loop over multiple arrays.*/
|
||||
x.=' '; x.1="a"; x.2='b'; x.3="c"; x.4='d'
|
||||
y.=' '; y.1="A"; y.2='B'; y.3="C";
|
||||
z.=' '; z.1= 1 ; z.2= 2 ; z.3= 3 ; z.4= 4; z.5=
|
||||
5
|
||||
z.=' '; z.1= 1 ; z.2= 2 ; z.3= 3 ; z.4= 4; z.5= 5
|
||||
|
||||
do j=1 until output=''
|
||||
output=x.j || y.j || z.j
|
||||
say output
|
||||
end /*j*/ /*stick a fork in it, we're
|
||||
done.*/
|
||||
end /*j*/ /*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
/*REXX program shows how to simultaneously loop over
|
||||
multiple lists.*/
|
||||
/*REXX program shows how to simultaneously loop over multiple lists.*/
|
||||
x = 'a b c d'
|
||||
y = 'A B C'
|
||||
z = 1 2 3 4
|
||||
do j=1 until output=''
|
||||
output = word(x,j) || word(y,j) || word(z,j)
|
||||
say output
|
||||
end /*j*/ /*stick a fork in it, we're
|
||||
done.*/
|
||||
end /*j*/ /*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
/*REXX program shows how to simultaneously loop over
|
||||
multiple lists.*/
|
||||
/*REXX program shows how to simultaneously loop over multiple lists.*/
|
||||
x = 'a b c d'
|
||||
y = 'A B C'
|
||||
z = 1 2 3 4 ..LAST
|
||||
do j=1 for max(words(x), words(y), words(z))
|
||||
say word(x,j) || word(y,j) || word(z,j)
|
||||
end /*j*/ /*stick a fork in it, we're
|
||||
done.*/
|
||||
end /*j*/ /*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
#x, y, z = [["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]];
|
||||
3.collect { |i| x[i] ++ y[i] ++ z[i] }
|
||||
|
|
@ -0,0 +1 @@
|
|||
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].flop.collect { |x| x.join }
|
||||
|
|
@ -0,0 +1 @@
|
|||
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].flop.collect(_.join)
|
||||
|
|
@ -0,0 +1 @@
|
|||
["a", "b", "c"] +++ ["A", "B", "C"] +++ ["1", "2", "3"]
|
||||
|
|
@ -0,0 +1 @@
|
|||
[["a", "b", "c"], ["A", "B", "C"], ["1", "2", "3"]].reduce('+++')
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
([\a,\b,\c]+++[\A,\B,\C]+++[1,2,3]).do({|array|
|
||||
array.do(_.post); "".postln })
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
10 READ size: DIM a$(size): DIM b$(size): DIM c$(size)
|
||||
20 FOR i=1 TO size
|
||||
30 READ a$(i),b$(i),c$(i)
|
||||
40 PRINT a$(i);b$(i);c$(i)
|
||||
50 NEXT i
|
||||
60 DATA 3,"a","A","1","b","B","2","c","C","3"
|
||||
Loading…
Add table
Add a link
Reference in a new issue