Skip to content

Commit

Permalink
Add drop-while
Browse files Browse the repository at this point in the history
  • Loading branch information
Olical committed Sep 17, 2024
1 parent d30f22b commit 28677f1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/api/nfnl/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [`count`](#count)
- [`dec`](#dec)
- [`distinct`](#distinct)
- [`drop-while`](#drop-while)
- [`empty?`](#empty)
- [`even?`](#even)
- [`filter`](#filter)
Expand Down Expand Up @@ -164,6 +165,15 @@ Function signature:

Takes a sequential table of values (xs) and returns a distinct sequential table with all duplicates removed.

## `drop-while`
Function signature:

```
(drop-while f xs)
```

Drop values while (f x) returns true.

## `empty?`
Function signature:

Expand Down
16 changes: 15 additions & 1 deletion fnl/nfnl/core.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,19 @@
(set done? true))))
acc))

(fn drop-while [f xs]
"Drop values while (f x) returns true."
(local xs (seq xs))
(when xs
(var acc [])
(var done? false)
(for [i 1 (count xs) 1]
(let [v (. xs i)]
(when (or done? (not (f v)))
(table.insert acc v)
(set done? true))))
acc))

{: rand
: nil?
: number?
Expand Down Expand Up @@ -494,4 +507,5 @@
: clear-table!
: sequential?
: seq
: take-while}
: take-while
: drop-while}
11 changes: 11 additions & 0 deletions fnl/spec/nfnl/core_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,14 @@
[[:hi :world]]
(core.take-while #(= (. $1 1) :hi) {:hi :world}))
nil))))

(describe
"drop-while"
(fn []
(it "drops values while f is true"
(fn []
(assert.are.same [1 2 3] (core.drop-while #(< $1 0) [-1 -2 -3 1 2 3]))
(assert.are.same [2 3] (core.drop-while #(< $1 2) [1 2 3]))
(assert.are.same nil (core.drop-while #(> $1 0) nil))
(assert.are.same [] (core.drop-while #(= (. $1 1) :hi) {:hi :world}))
nil))))
20 changes: 19 additions & 1 deletion lua/nfnl/core.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion lua/spec/nfnl/core_spec.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 28677f1

Please sign in to comment.