it greatly improves on it's predecessors
familiar syntax
it's designed to be practical, to the chagrin of CS purists
next-generation of applications being built (hadoop, palantir, runsecape : asana, meteor)
Name:
Anonymous2012-09-10 6:28
>>21
Some example code in it...
m:with @Xs X \= $X $@Xs
// not beginning with, like ^ in regexps (use with pattern matcher)
m:nb X = \<&L = ne (lhd &L) $X>
// usage: foo X:of,«A B C» = say "X is one of the first three letters"
m:of Xs \= <&X = fnd &X $Xs>
sconc a b = conc a b | sym
filter f Xs = fold <A B = {f B = [@A It]; A}> [[]@Xs]
flat [@Xs] = mapc flat Xs; X=[X]
includes P [X:_:@!P @_]=X; P [_@Xs] = includes P Xs
rmap f [@Xs] = map (rmap f ?) Xs | f
;f X = f X
digits X = map num X,prn,str
//insert `sep` between elements of a list `l`
//Usage: infix "+" {a b c}
//Example: map ?,hex "LISP" | infix " " | fold sconc
//TODO: add flag to create (`+` A (`+` B C)) like lists
infix Sep L = fold [@? Sep ??] [(take 1 L) @L,ltl]
join S Xs = infix S Xs | fold conc
cut P S Xs = drop P Xs | take S
// mappend from CL
// Example: maps I=" " ?,hex str,"LISP"
mapc f Xs I:{ø} = map f !Xs = {I = infix I !Xs} = fold conc Xs
subst Src Dst L = map <!Src=Dst; X=X> L
// takeInit odd? [1 5 1 2 3 1 1 2 3] --> (1 5 1)
takeInit P [@Xs @(nb P)] = Xs // Take initial elements for which `p` is true
trim Xs S:{" "} I:{ø} L:{√} R:{√}
= Xs:Xs,str
X:{L && (pos (ne ? S) Xs) = It; 0}
Y:{R && ~(pos (ne ? S) Xs) = It; 0}
= {I = conc (take X Xs) (take Y Xs); drop Y (drop X Xs)} | sym
// break list into piles of `n` items
grp N [@Xs] = [(take N Xs) @(drop N Xs | grp N)]
// partition by `f`
part f Xs = R:ø = map <X=[@!R.(f X) X]> Xs = map ?,1 R
// Example1: unfold <1=ø; X=[X-1 1]> 7
// Example2: qsort Xs = Xs |
// unfold <[X] = X
// ;[X@Xs] = [(keep (lt ? X) Xs) [X] (keep (gt ? X) Xs)]>
unfold f O = f O | <ø=[O]; Xs = mapc (unfold f ?) Xs>
// counts the number of ones in the bit representation of an integer
// use it to calculate size of bitmasks
bitCount X = cl (logcount X)
// bit-length of an integer: 2^(log 2 ? | ceil)
bitLen X = cl (`integer-length` X)
// inverts `bits` bits in `value` (bitwise not)
inv Bits Value = cl (logxor (ash 1 Bits)-1 Value)
exp X = cl (exp X)
log Base X = cl (log X Base)
sin X = cl (sin X)
cos X = cl (cos X)
tan X = cl (tan X)
asin X = cl (asin X)
acos X = cl (acos X)
atan X = cl (atan X)
sum S = fold `+` [0@S]
prod S = fold `*` [1@S]
avg Xs = Xs,sum/Xs,len
dot Xs Ys = cl (`dot-lists` Xs Ys)
abs X = {num? X = cl abs X; cl (`dot-lists` X X) | sqrt}
norm V = V/V,abs
transpose V = N:0 = [V,lhd,len++(map <X = I:N = !N+1 = X,I> V)]
// convert list to set (an ordered list that does not have duplicated elements)
uniq L = L,sort,<[@A X X @B]=[@A @[X @B],r]; E=E>
// set operations
union A B = A,<[X@Xs] = {fnd X B = Xs,r ; [X@Xs,r]}; _=B>
isect A B = A,<[X@Xs] = {fnd X B = [X@Xs,r]; Xs,r}>
diff A B = union (skip (fnd ? B) A) (skip (fnd ? A) B)
ss [X@Xs] = R:{ss Xs; [ø]} [@R @(map [X @?] R)]
m:ldb Pos Size Value \= <P S V = (cl ldb (byte S P) V)> $Pos $Size $Value
maximize p [M@Xs]
= S:M,p = fe <X = NS:X,p = {NS > S = (M=X) = (S=NS)}> Xs = M
minimize p [M@Xs]
= S:M,p = fe <X = NS:X,p = {NS < S = (M=X) = (S=NS)}> Xs = M
m:while Test @Expr \= fc <= {$Test = $Expr = r}>
m:until Test @Expr \= while (n? $Test) $@Expr
m:loop Expr \= fc <:&r = $Expr = &r>
// Usage: times I:10 say i
m:times Head @Expr = C:Head,<[":" C N]=C; _=\&C>
N:Head,<[":" C N]=N; X=X >
\= &E:$N = 0,<:&r (&E) $C = $Expr = &r $C+1>
// for (I:0; I<6; !I+1) say I
m:for «$@V; $@C; $@I» @Body = \(= $@V = cl (while @$C @$Body $@(map \@$? I)))
;[":" $X $Xs] @Body \= fe <$X=$@Body> $Xs
// usage: gen X^2 for X:1..10 if X,odd?
// gen [X Y Z] for X:1..21 Y:X..21 Z:Y..21 if X^2 + Y^2 == Z^2
//m:gen @E \for @F \if @C
// \= &Xs:n try &Xs $@(map <[O A B]=[O A \(amb $B)]> F)
// {$@C = [@!&Xs $@E]}
// bt
// these will generate list of ascending/descending numbers
// FIXME: merge these into `..`
asc Start = seq Start (asc Start+1)
dsc Start = seq Start (dsc Start-1)