Skip to content

Commit

Permalink
oops
Browse files Browse the repository at this point in the history
  • Loading branch information
molarmanful committed Nov 25, 2022
1 parent 3d9b98b commit 118faa6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 18 additions & 0 deletions sclin/src/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ extension (env: ENV)
)
)

def findi: ENV = env.mod2((x, y) =>
y.vec1(f => x.findIndex(a => env.evalA1(Vector(a), f).toBool).pipe(NUM(_)))
)

def uniq: ENV = env.mod2((x, y) =>
y.vec1(f =>
x.uniqByM(
Expand Down Expand Up @@ -2002,6 +2006,9 @@ extension (env: ENV)
Transposes a collection of collections matrix-style.
```sclin
[[1 2 3][4 5 6][7 8 9]] tpose
```
```sclin
[[1 2][3 4 5][6]] tpose
```
*/
case "tpose" => tpose
Expand Down Expand Up @@ -2320,12 +2327,23 @@ extension (env: ENV)
@s a f' -> _'
Finds first element of `a` where predicate `f` is truthy.
See #{fltr} for the signature of `f`.
Returns `UN` if not found.
```sclin
[5 1 2 4 3] ( 2% ! ) find
```
*/
case "find" => find
/*
@s a f' -> NUM'
Finds index of first element of `a` where predicate `f` is truthy.
See #{fltr} for the signature of `f`.
Returns `-1` if not found.
```sclin
[5 1 2 4 3] ( 2% ! ) find:
```
*/
case "find:" => findi
/*
@s a f' -> _'
Uniquifies elements of `a` with mapper `f`.
See #{map} for the signature of `f`.
Expand Down
8 changes: 7 additions & 1 deletion sclin/src/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ enum ANY:
case MAP(x) => x.find { case (a, b) => f(a, b) }
case _ => find(g)

def findIndex(f: ANY => Boolean): Int = this match
case SEQ(x) => x.indexWhere(f)
case ARR(x) => x.indexWhere(f)
case FN(p, x) => x.indexWhere(f)
case _ => toARR.findIndex(f)

def uniqBy(f: ANY => ANY): ANY = this match
case SEQ(x) => x.distinctBy(f).toSEQ
case ARR(x) => x.distinctBy(f).toARR
Expand Down Expand Up @@ -805,4 +811,4 @@ object ANY:
def transpose[A](ls: SEQW[SEQW[A]]): SEQW[SEQW[A]] =
ls.filter(_.nonEmpty) match
case LazyList() => LazyList.empty
case xs => xs.map(_.head) #:: transpose(xs.map(_.tail))
case xs => xs.map(_.head) #:: transpose(xs.map(_.tail))

0 comments on commit 118faa6

Please sign in to comment.