Passage local: → var (3)

This commit is contained in:
Benjamin VAUDOUR 2021-08-27 22:09:05 +02:00
parent a49f17bc35
commit b5c63e6dc0
1 changed files with 26 additions and 26 deletions

View File

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