Passage local: → var (3)
This commit is contained in:
parent
a49f17bc35
commit
b5c63e6dc0
|
@ -2,7 +2,7 @@ use builtin
|
|||
use str
|
||||
|
||||
fn -p [@argv]{
|
||||
local:c = (count $argv)
|
||||
var c = (count $argv)
|
||||
if (== $c 0) {
|
||||
put [ (all) ]
|
||||
} elif (== $c 1) {
|
||||
|
@ -16,7 +16,7 @@ fn -r [step begin end]{
|
|||
if (> $step 0) {
|
||||
builtin:range &step=$step $begin (+ $end 1)
|
||||
} else {
|
||||
local:d = (+ $begin $end)
|
||||
var d = (+ $begin $end)
|
||||
builtin:range &step=(* $step -1) $end (+ $begin 1) | each [e]{ - $d $e }
|
||||
}
|
||||
}
|
||||
|
@ -33,11 +33,11 @@ fn range [&step=1 b @r]{
|
|||
if (== $step 0) {
|
||||
fail 'bad value: step must be positive, but is 0'
|
||||
}
|
||||
local:c = (count $r)
|
||||
var c = (count $r)
|
||||
if (> $c 1) {
|
||||
fail 'usage: list:range &step=1 begin? end'
|
||||
}
|
||||
local:e = 0
|
||||
var e = 0
|
||||
if (== $c 0) {
|
||||
if (> $step 0) {
|
||||
b e = $e $b
|
||||
|
@ -52,8 +52,8 @@ fn indexes [&from=$false &step=1 @argv]{
|
|||
if (== $step 0) {
|
||||
fail 'bad value: step must be positive, but is 0'
|
||||
}
|
||||
local:l = (-p $@argv)
|
||||
local:c = (count $l)
|
||||
var l = (-p $@argv)
|
||||
var c = (count $l)
|
||||
if (not $from) {
|
||||
range &step=$step (- $c 1)
|
||||
} else {
|
||||
|
@ -75,7 +75,7 @@ fn indexes [&from=$false &step=1 @argv]{
|
|||
}
|
||||
|
||||
fn -for [&from=$false &step=1 @argv]{
|
||||
local:l = (-p $@argv)
|
||||
var l = (-p $@argv)
|
||||
indexes &from=$from &step=$step $l | each [i]{ put [ $i $l[$i] ] }
|
||||
}
|
||||
|
||||
|
@ -84,7 +84,7 @@ fn loop [&from=$false &step=1 &end=$false cb @argv]{
|
|||
-for &from=$from &step=$step $@argv | each [e]{ $cb $@e }
|
||||
} else {
|
||||
-for &from=$from &step=$step $@argv | each [e]{
|
||||
local:i local:v = $@e
|
||||
var i v = $@e
|
||||
if ($end $i $v) {
|
||||
break
|
||||
}
|
||||
|
@ -110,13 +110,13 @@ fn filter [cb @argv]{
|
|||
}
|
||||
fn filter-not [cb @argv]{ filter [v]{ not ($cb $v) } $@argv }
|
||||
fn first [&from=$false &reverse=$false cb @argv]{
|
||||
local:f = [v]{
|
||||
var f = [v]{
|
||||
if ($cb $v) {
|
||||
put $v
|
||||
break
|
||||
}
|
||||
}
|
||||
local:l = (-p $@argv)
|
||||
var l = (-p $@argv)
|
||||
if (not $from) {
|
||||
if $reverse {
|
||||
reach $f $l
|
||||
|
@ -124,12 +124,12 @@ fn first [&from=$false &reverse=$false cb @argv]{
|
|||
each $f $l
|
||||
}
|
||||
} else {
|
||||
local:c = (count $l)
|
||||
var c = (count $l)
|
||||
if (< $from 0) {
|
||||
from = (+ $c $from)
|
||||
}
|
||||
if $reverse {
|
||||
local:e = (+ $from 1)
|
||||
var e = (+ $from 1)
|
||||
if (> $e $c) {
|
||||
e = $c
|
||||
} elif (< $e 0) {
|
||||
|
@ -137,7 +137,7 @@ fn first [&from=$false &reverse=$false cb @argv]{
|
|||
}
|
||||
reach $f $l[..$e]
|
||||
} else {
|
||||
local:b = $from
|
||||
var b = $from
|
||||
if (> $b $c) {
|
||||
b = $c
|
||||
} elif (< $b 0) {
|
||||
|
@ -157,8 +157,8 @@ fn filter-index [cb @argv]{
|
|||
}
|
||||
fn filter-index-not [cb @argv]{ filter-index [v]{ not ($cb $v) } $@argv }
|
||||
fn first-index [&from=$false &reverse=$false cb @argv]{
|
||||
local:idx = -1
|
||||
local:step = 1
|
||||
var idx = -1
|
||||
var step = 1
|
||||
if $reverse {
|
||||
step = -1
|
||||
}
|
||||
|
@ -180,7 +180,7 @@ fn search [cb @argv]{
|
|||
}
|
||||
fn search-not [cb @argv]{ search [i v]{ not ($cb $i $v) } $@argv }
|
||||
fn search-first [&from=$false &reverse=$false cb @argv]{
|
||||
local:step = 1
|
||||
var step = 1
|
||||
if $reverse {
|
||||
step = -1
|
||||
}
|
||||
|
@ -201,8 +201,8 @@ fn search-index [cb @argv]{
|
|||
}
|
||||
fn search-index-not [cb @argv]{ search-index [i v]{ not ($cb $i $v) } $@argv }
|
||||
fn search-first-index [&from=$false &reverse=$false cb @argv]{
|
||||
local:idx = -1
|
||||
local:step = 1
|
||||
var idx = -1
|
||||
var step = 1
|
||||
if $reverse {
|
||||
step = -1
|
||||
}
|
||||
|
@ -216,7 +216,7 @@ fn search-first-index [&from=$false &reverse=$false cb @argv]{
|
|||
}
|
||||
|
||||
fn contains [cb @argv]{
|
||||
local:e = $false
|
||||
var e = $false
|
||||
each [v]{
|
||||
if ($cb $v) {
|
||||
e = $true
|
||||
|
@ -228,7 +228,7 @@ fn contains [cb @argv]{
|
|||
fn contains-not [cb @argv]{ contains [v]{ not ($cb $v) } $@argv }
|
||||
|
||||
fn exists [cb @argv]{
|
||||
local:e = $false
|
||||
var e = $false
|
||||
loop [i v]{
|
||||
if ($cb $i $v) {
|
||||
e = $true
|
||||
|
@ -248,7 +248,7 @@ fn reduce [v cb @argv]{
|
|||
}
|
||||
|
||||
fn remove-duplicate [@argv]{
|
||||
local:done = [&]
|
||||
var done = [&]
|
||||
each [v]{
|
||||
put [&v=$v &e=(has-key $done $v)]
|
||||
done[$v] = $nil
|
||||
|
@ -261,17 +261,17 @@ fn remove-duplicate [@argv]{
|
|||
}
|
||||
}
|
||||
fn premove-duplicate [@argv]{
|
||||
local:done= [&]
|
||||
var done = [&]
|
||||
peach [v]{
|
||||
local:v = $v
|
||||
var v = $v
|
||||
done[$v] = $nil
|
||||
} (-p $@argv)
|
||||
keys $done
|
||||
}
|
||||
|
||||
fn swap [i j @argv]{
|
||||
local:l = (-p $@argv)
|
||||
local:c = (count $l)
|
||||
var l = (-p $@argv)
|
||||
var c = (count $l)
|
||||
i j = (-i $i $c) (-i $j $c)
|
||||
if (or (== $i $c) (== $j $c)) {
|
||||
fail 'Index out of range'
|
||||
|
@ -291,7 +291,7 @@ fn swap [i j @argv]{
|
|||
}
|
||||
|
||||
fn less [cb]{
|
||||
local:l = [a b]{ < ($cb $a $b) 0 }
|
||||
var l = [a b]{ < ($cb $a $b) 0 }
|
||||
put $l
|
||||
}
|
||||
fn sort [cb @argv]{ order &less-than=(less $cb) (-p $@argv) }
|
||||
|
|
Loading…
Reference in New Issue