Skip to content

Commit

Permalink
lib: once, add type parameter mutate
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellilltokiwa committed Sep 6, 2024
1 parent 99f1255 commit 137a7b5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
83 changes: 43 additions & 40 deletions lib/Sequence.fz
Original file line number Diff line number Diff line change
Expand Up @@ -650,46 +650,48 @@ public Sequence(public T type) ref is
T : property.equatable
=>

make(t Sequence T, r option (Node T)) =>
n option (Node T) =>
if t.is_empty then nil else make (t.drop 1) (step r t[0])
r_star =>
if t.is_empty
r
else if is_match r t[0]
r.get.rest
find_lm.instate_self ()->

make(t Sequence T, r option (Node T)) =>
n option (Node T) =>
if t.is_empty then nil else make (t.drop 1) (step r t[0])
r_star =>
if t.is_empty
r
else if is_match r t[0]
r.get.rest
else
r
Node t (once find_lm (option (Node T)) ()->n) r_star

init := make pattern nil

step(acc option (Node T), x T) =>
match acc
nil => init
n Node => if is_match acc x then n.next.get else step n.rest x

is_done (option (Node T))->bool => (acc)->
match acc
nil => false
n Node => n.top.is_empty

is_match(acc option (Node T), x T) =>
match acc
nil => false
n Node => !n.top.is_empty && n.top[0] = x

fold_until(acc option (Node T), step (option (Node T), T)->option (Node T), data Sequence T) option i32 =>
if is_done acc
Sequence.this.count - data.count - pattern.count
else if data.is_empty
nil
else
r
Node t (once (option (Node T)) ()->n) r_star

init := make pattern nil

step(acc option (Node T), x T) =>
match acc
nil => init
n Node => if is_match acc x then n.next.get else step n.rest x

is_done (option (Node T))->bool => (acc)->
match acc
nil => false
n Node => n.top.is_empty

is_match(acc option (Node T), x T) =>
match acc
nil => false
n Node => !n.top.is_empty && n.top[0] = x

fold_until(acc option (Node T), step (option (Node T), T)->option (Node T), data Sequence T) option i32 =>
if is_done acc
Sequence.this.count - data.count - pattern.count
else if data.is_empty
nil
else
acc_star =>
fold_until.this.step acc data[0]
fold_until acc_star fold_until.this.step (data.drop 1)
acc_star =>
fold_until.this.step acc data[0]
fold_until acc_star fold_until.this.step (data.drop 1)

fold_until init step Sequence.this
fold_until init step Sequence.this



Expand Down Expand Up @@ -900,6 +902,7 @@ public Sequence(public T type) ref is
public type.AS_STRING_NON_FINITE_MAX_ELEMENTS => 10


# helper feature for knuth morris pratt algorithm
# helper features for knuth morris pratt algorithm
#
Node(T type, top Sequence T, next once (option (Node T)), rest option (Node T)) ref is
find_lm : mutate is
Node(T type, top Sequence T, next once find_lm (option (Node T)), rest option (Node T)) ref is
4 changes: 2 additions & 2 deletions lib/once.fz
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
# executes `f` only the first time when
# calling `get` caching its result.
#
public once(T type, f Lazy T) is
public once(LM type : mutate, T type, f Lazy T) is

cache := mut (option T) nil
cache := LM.env.new (option T) nil

# get the result of `f`
#
Expand Down

0 comments on commit 137a7b5

Please sign in to comment.