Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
23
Task/Flatten-a-list/8th/flatten-a-list.8th
Normal file
23
Task/Flatten-a-list/8th/flatten-a-list.8th
Normal 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
|
||||
13
Task/Flatten-a-list/Ceylon/flatten-a-list.ceylon
Normal file
13
Task/Flatten-a-list/Ceylon/flatten-a-list.ceylon
Normal 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());
|
||||
}
|
||||
26
Task/Flatten-a-list/EchoLisp/flatten-a-list.echolisp
Normal file
26
Task/Flatten-a-list/EchoLisp/flatten-a-list.echolisp
Normal 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))
|
||||
27
Task/Flatten-a-list/Elm/flatten-a-list.elm
Normal file
27
Task/Flatten-a-list/Elm/flatten-a-list.elm
Normal 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)
|
||||
2
Task/Flatten-a-list/LFE/flatten-a-list.lfe
Normal file
2
Task/Flatten-a-list/LFE/flatten-a-list.lfe
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> (: lists flatten '((1) 2 ((3 4) 5) ((())) (((6))) 7 8 ()))
|
||||
(1 2 3 4 5 6 7 8)
|
||||
6
Task/Flatten-a-list/Lasso/flatten-a-list.lasso
Normal file
6
Task/Flatten-a-list/Lasso/flatten-a-list.lasso
Normal 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
|
||||
9
Task/Flatten-a-list/NGS/flatten-a-list.ngs
Normal file
9
Task/Flatten-a-list/NGS/flatten-a-list.ngs
Normal 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, []]))
|
||||
34
Task/Flatten-a-list/Nim/flatten-a-list.nim
Normal file
34
Task/Flatten-a-list/Nim/flatten-a-list.nim
Normal 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)
|
||||
1
Task/Flatten-a-list/Oforth/flatten-a-list.oforth
Normal file
1
Task/Flatten-a-list/Oforth/flatten-a-list.oforth
Normal file
|
|
@ -0,0 +1 @@
|
|||
[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] expand println
|
||||
1
Task/Flatten-a-list/Phix/flatten-a-list.phix
Normal file
1
Task/Flatten-a-list/Phix/flatten-a-list.phix
Normal file
|
|
@ -0,0 +1 @@
|
|||
?flatten({{1},2,{{3,4},5},{{{}}},{{{6}}},7,8,{}})
|
||||
6
Task/Flatten-a-list/Red/flatten-a-list.red
Normal file
6
Task/Flatten-a-list/Red/flatten-a-list.red
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
flatten: function [
|
||||
"Flatten the block"
|
||||
block [any-block!]
|
||||
][
|
||||
load form block
|
||||
]
|
||||
10
Task/Flatten-a-list/Ring/flatten-a-list.ring
Normal file
10
Task/Flatten-a-list/Ring/flatten-a-list.ring
Normal 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
|
||||
11
Task/Flatten-a-list/Sidef/flatten-a-list.sidef
Normal file
11
Task/Flatten-a-list/Sidef/flatten-a-list.sidef
Normal 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
|
||||
31
Task/Flatten-a-list/Swift/flatten-a-list-1.swift
Normal file
31
Task/Flatten-a-list/Swift/flatten-a-list-1.swift
Normal 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)
|
||||
29
Task/Flatten-a-list/Swift/flatten-a-list-2.swift
Normal file
29
Task/Flatten-a-list/Swift/flatten-a-list-2.swift
Normal 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)
|
||||
47
Task/Flatten-a-list/Swift/flatten-a-list-3.swift
Normal file
47
Task/Flatten-a-list/Swift/flatten-a-list-3.swift
Normal 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)
|
||||
7
Task/Flatten-a-list/Wart/flatten-a-list.wart
Normal file
7
Task/Flatten-a-list/Wart/flatten-a-list.wart
Normal 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))
|
||||
6
Task/Flatten-a-list/jq/flatten-a-list-1.jq
Normal file
6
Task/Flatten-a-list/jq/flatten-a-list-1.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def flatten:
|
||||
reduce .[] as $i
|
||||
([];
|
||||
if $i | type == "array" then . + ($i | flatten)
|
||||
else . + [$i]
|
||||
end);
|
||||
2
Task/Flatten-a-list/jq/flatten-a-list-2.jq
Normal file
2
Task/Flatten-a-list/jq/flatten-a-list-2.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] | flatten
|
||||
[1,2,3,4,5,6,7,8]
|
||||
Loading…
Add table
Add a link
Reference in a new issue