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,23 @@
\ take a list (array) and flatten it:
: (flatten) \ a -- a
(
\ is it a number?
dup >kind ns:n n:= if
\ yes. so add to the list
r> swap a:push >r
else
\ it is not, so flatten it
(flatten)
then
drop
) a:each drop ;
: flatten \ a -- a
[] >r (flatten) r> ;
[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
dup . cr
flatten
. cr
bye

View file

@ -0,0 +1,13 @@
shared void run() {
"Lazily flatten nested streams"
{Anything*} flatten({Anything*} stream)
=> stream.flatMap((element)
=> switch (element)
case (is {Anything*}) flatten(element)
else [element]);
value list = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []];
print(list);
print(flatten(list).sequence());
}

View file

@ -0,0 +1,26 @@
(define (fflatten l)
(cond
[(null? l) null]
[(not (list? l)) (list l)]
[else (append (fflatten (first l)) (fflatten (rest l)))]))
;;
(define L' [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []])
(fflatten L) ;; use custom function
→ (1 2 3 4 5 6 7 8)
(flatten L) ;; use built-in
→ (1 2 3 4 5 6 7 8)
;; Remarks
;; null is the same as () - the empty list -
(flatten '(null null null))
→ null
(flatten '[ () () () ])
→ null
(flatten null)
❗ error: flatten : expected list : null
;; The 'reverse' of flatten is group
(group '( 4 5 5 5 6 6 7 8 7 7 7 9))
→ ((4) (5 5 5) (6 6) (7) (8) (7 7 7) (9))

View file

@ -0,0 +1,27 @@
import Graphics.Element exposing (show)
type Tree a
= Leaf a
| Node (List (Tree a))
flatten : Tree a -> List a
flatten tree =
case tree of
Leaf a -> [a]
Node list -> List.concatMap flatten list
-- [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
tree : Tree Int
tree = Node
[ Node [Leaf 1]
, Leaf 2
, Node [Node [Leaf 3, Leaf 4], Leaf 5]
, Node [Node [Node []]]
, Node [Node [Node [Leaf 6]]]
, Leaf 7
, Leaf 8
, Node []
]
main =
show (flatten tree)

View file

@ -0,0 +1,2 @@
> (: lists flatten '((1) 2 ((3 4) 5) ((())) (((6))) 7 8 ()))
(1 2 3 4 5 6 7 8)

View file

@ -0,0 +1,6 @@
local(original = json_deserialize('[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]'))
#original
'<br />'
(with item in delve(#original)
select #item) -> asstaticarray

View file

@ -0,0 +1,9 @@
F flatten_r(a:Arr)
collector {
local kern
F kern(x) collect(x)
F kern(x:Arr) x.each(kern)
kern(a)
}
echo(flatten_r([[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]))

View file

@ -0,0 +1,34 @@
type
TreeList[T] = ref TTreeList[T]
TTreeList[T] = object
case isLeaf: bool
of true: data: T
of false: list: seq[TreeList[T]]
proc L[T](list: varargs[TreeList[T]]): TreeList[T] =
var s: seq[TreeList[T]] = @[]
for x in list: s.add x
TreeList[T](isLeaf: false, list: s)
proc N[T](data: T): TreeList[T] =
TreeList[T](isLeaf: true, data: data)
proc `$`[T](n: TreeList[T]): string =
if n.isLeaf: result = $n.data
else:
result = "["
for i, x in n.list:
if i > 0: result.add ", "
result.add($x)
result.add "]"
proc flatten[T](n: TreeList[T]): seq[T] =
if n.isLeaf: result = @[n.data]
else:
result = @[]
for x in n.list:
result.add flatten x
var x = L(L(N 1), N 2, L(L(N 3, N 4), N 5), L(L(L[int]())), L(L(L(N 6))), N 7, N 8, L[int]())
echo x
echo flatten(x)

View file

@ -0,0 +1 @@
[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] expand println

View file

@ -0,0 +1 @@
?flatten({{1},2,{{3,4},5},{{{}}},{{{6}}},7,8,{}})

View file

@ -0,0 +1,6 @@
flatten: function [
"Flatten the block"
block [any-block!]
][
load form block
]

View file

@ -0,0 +1,10 @@
aList = "[[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]"
bList = ""
cList = ""
for n=1 to len(aList)
if ascii(aList[n]) >= 48 and ascii(aList[n]) <= 57
bList = bList + ", " + aList[n] ok
next
cList = substr(bList,3,Len(bList)-2)
dList = "[" + cList + "]"
see dList + nl

View file

@ -0,0 +1,11 @@
func flatten(a) {
var flat = [];
a.each { |item|
flat += (item.is_an(Array) ? flatten(item) : [item]);
};
return flat;
}
var arr = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []];
say flatten(arr).dump; # used-defined function
say arr.flatten.dump; # built-in method for Array obj

View file

@ -0,0 +1,31 @@
func list(s: Any...) -> [Any] {
return s
}
func flatten<T>(s: [Any]) -> [T] {
var r = [T]()
for e in s {
switch e {
case let a as [Any]:
r += flatten(a)
case let x as T:
r.append(x)
default:
assert(false, "value of wrong type")
}
}
return r
}
let s = list(list(1),
2,
list(list(3, 4), 5),
list(list(list())),
list(list(list(6))),
7,
8,
list()
)
println(s)
let result : [Int] = flatten(s)
println(result)

View file

@ -0,0 +1,29 @@
func list(s: Any...) -> [Any] {
return s
}
func flatten<T>(s: [Any]) -> [T] {
return s.flatMap {
switch $0 {
case let a as [Any]:
return flatten(a)
case let x as T:
return [x]
default:
assert(false, "value of wrong type")
}
}
}
let s = list(list(1),
2,
list(list(3, 4), 5),
list(list(list())),
list(list(list(6))),
7,
8,
list()
)
println(s)
let result : [Int] = flatten(s)
println(result)

View file

@ -0,0 +1,47 @@
func list(s: Any...) -> [Any]
{
return s
}
func flatten<T>(array: [Any]) -> [T]
{
var result: [T] = []
var workstack: [(array: [Any], lastIndex: Int)] = [(array, 0)]
workstackLoop: while !workstack.isEmpty
{
for element in workstack.last!.array.suffixFrom(workstack.last!.lastIndex)
{
workstack[workstack.endIndex - 1].lastIndex++
if let element = element as? [Any]
{
workstack.append((element, 0))
continue workstackLoop
}
result.append(element as! T)
}
workstack.removeLast()
}
return result
}
let input = list(list(1),
2,
list(list(3, 4), 5),
list(list(list())),
list(list(list(6))),
7,
8,
list()
)
print(input)
let result: [Int] = flatten(input)
print(result)

View file

@ -0,0 +1,7 @@
def (flatten seq acc)
if no.seq
acc
~list?.seq
(cons seq acc)
:else
(flatten car.seq (flatten cdr.seq acc))

View file

@ -0,0 +1,6 @@
def flatten:
reduce .[] as $i
([];
if $i | type == "array" then . + ($i | flatten)
else . + [$i]
end);

View file

@ -0,0 +1,2 @@
[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] | flatten
[1,2,3,4,5,6,7,8]