Skip to content

Commit

Permalink
Rename foldLeft to foldl_List (#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek authored Dec 11, 2024
1 parent b2248d0 commit 869c30b
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def operator ||(x, y):
def keep(i):
(i % 3 == 0) || (i % 5 == 0)
def sum(as): as.foldLeft(0, add)
def sum(as): as.foldl_List(0, add)
# here is the python version:
# >>> sum(i for i in xrange(1000) if keep_fn(i))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ClangGenTest extends munit.FunSuite {
To inspect the code, change the hash, and it will print the code out
*/
testFilesCompilesToHash("test_workspace/Ackermann.bosatsu")(
"fbc7ea8d5b01c1f0eac1c80652f16534"
"89743844829c5b4266b0c4a17ab21c52"
)
}
}
10 changes: 5 additions & 5 deletions core/src/main/resources/bosatsu/predef.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export (
empty_Dict,
eq_Int,
flat_map_List,
foldLeft,
foldl_List,
foldr_List,
gcd_Int,
get_key,
Expand Down Expand Up @@ -145,7 +145,7 @@ def foldr_List(list: List[a], fn: (a, b) -> b, acc: b) -> b:
# we can rewrite: foldr_List(build_List(g), f, x) => g(f, x)
# see "A Shortcut to Deforestation" by Gill et. al.

def foldLeft(lst: List[a], item: b, fn: (b, a) -> b) -> b:
def foldl_List(lst: List[a], item: b, fn: (b, a) -> b) -> b:
# make the loop function as small as possible
def loop(lst, item):
recur lst:
Expand All @@ -154,7 +154,7 @@ def foldLeft(lst: List[a], item: b, fn: (b, a) -> b) -> b:
loop(lst, item)

def reverse_concat(front: List[a], back: List[a]) -> List[a]:
foldLeft(front, back, (tail, h) -> [h, *tail])
foldl_List(front, back, (tail, h) -> [h, *tail])

def reverse(as: List[a]) -> List[a]:
reverse_concat(as, [])
Expand All @@ -165,10 +165,10 @@ def concat(front: List[a], back: List[a]) -> List[a]:
case _: reverse_concat(reverse(front), back)

def map_List(lst: List[a], fn: a -> b) -> List[b]:
lst.foldLeft([], (t, a) -> [fn(a), *t]).reverse()
lst.foldl_List([], (t, a) -> [fn(a), *t]).reverse()

def flat_map_List(lst: List[a], fn: a -> List[b]) -> List[b]:
lst.foldLeft([], (t, a) -> fn(a).reverse_concat(t)).reverse()
lst.foldl_List([], (t, a) -> fn(a).reverse_concat(t)).reverse()

def replicate_List[a](item: a, cnt: Int) -> List[a]:
int_loop(cnt, EmptyList, (i, acc) -> (i.sub(1), NonEmptyList(item, acc)))
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/org/bykn/bosatsu/SourceConverter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,12 @@ final class SourceConverter(
case ListLang.Comprehension(KVPair(k, v), binding, in, filter) =>
/*
* { x: y for p in z} ==
* z.foldLeft(empty_Dict(stringOrder), \dict, p ->
* z.foldl_List(empty_Dict(stringOrder), \dict, p ->
* dict.add_key(x, y)
* )
*
* { x: y for p in z if w } =
* z.foldLeft(empty_Dict(stringOrder), \dict, p ->
* z.foldl_List(empty_Dict(stringOrder), \dict, p ->
* if w: dict.add_key(x, y)
* else: dict
* )
Expand All @@ -655,7 +655,7 @@ final class SourceConverter(
val newBound = binding.names
val pn = PackageName.PredefName
val opExpr: Expr[Declaration] =
Expr.Global(pn, Identifier.Name("foldLeft"), l)
Expr.Global(pn, Identifier.Name("foldl_List"), l)
val dictSymbol = unusedNames(decl.allNames).next()
val init: Expr[Declaration] = Expr.Local(dictSymbol, l)
val added = (withBound(k, newBound), withBound(v, newBound)).mapN(
Expand Down
42 changes: 21 additions & 21 deletions core/src/test/scala/org/bykn/bosatsu/EvaluationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,10 @@ package Foo
three = [1, 2]
def sum(ls):
ls.foldLeft(0, add)
ls.foldl_List(0, add)
sum0 = sum(three)
sum1 = three.foldLeft(0, (x, y) -> add(x, y))
sum1 = three.foldl_List(0, (x, y) -> add(x, y))
same = sum0.eq_Int(sum1)
"""),
Expand All @@ -401,8 +401,8 @@ package Foo
three = [1, 2]
sum0 = three.foldLeft(0, add)
sum1 = three.foldLeft(0, \x, y -> add(x, y))
sum0 = three.foldl_List(0, add)
sum1 = three.foldl_List(0, \x, y -> add(x, y))
same = sum0.eq_Int(sum1)
"""),
Expand Down Expand Up @@ -474,7 +474,7 @@ def same_items(items, eq):
(a, b) = p
eq(a, b)
items.foldLeft(True, (res, t) -> and(res, test(t)))
items.foldl_List(True, (res, t) -> and(res, test(t)))
def eq_list(a, b, fn):
same_items(zip(a, b), fn)
Expand Down Expand Up @@ -981,7 +981,7 @@ package A
big_list = range(3_000)
main = big_list.foldLeft(0, add)
main = big_list.foldl_List(0, add)
"""),
"A",
VInt((0 until 3000).sum)
Expand Down Expand Up @@ -1070,7 +1070,7 @@ main = len([1, 2, 3], 0)
List("""
package A
main = [x for x in range(4)].foldLeft(0, add)
main = [x for x in range(4)].foldl_List(0, add)
"""),
"A",
VInt(6)
Expand All @@ -1079,7 +1079,7 @@ main = [x for x in range(4)].foldLeft(0, add)
List("""
package A
main = [*[x] for x in range(4)].foldLeft(0, add)
main = [*[x] for x in range(4)].foldl_List(0, add)
"""),
"A",
VInt(6)
Expand All @@ -1091,7 +1091,7 @@ package A
doub = [(x, x) for x in range(4)]
main = [x.times(y) for (x, y) in doub].foldLeft(0, add)
main = [x.times(y) for (x, y) in doub].foldl_List(0, add)
"""),
"A",
VInt(1 + 4 + 9)
Expand All @@ -1100,7 +1100,7 @@ main = [x.times(y) for (x, y) in doub].foldLeft(0, add)
List("""
package A
main = [x for x in range(4) if x.eq_Int(2)].foldLeft(0, add)
main = [x for x in range(4) if x.eq_Int(2)].foldl_List(0, add)
"""),
"A",
VInt(2)
Expand All @@ -1110,7 +1110,7 @@ main = [x for x in range(4) if x.eq_Int(2)].foldLeft(0, add)
List("""
package A
main = [*[x, x] for x in range(4) if x.eq_Int(2)].foldLeft(0, add)
main = [*[x, x] for x in range(4) if x.eq_Int(2)].foldl_List(0, add)
"""),
"A",
VInt(4)
Expand Down Expand Up @@ -2774,7 +2774,7 @@ main = Foo(1, "2")
"""
|package Err
|
|from Bosatsu/Predef import foldLeft
|from Bosatsu/Predef import foldl_List
|
|main = 1
|""".stripMargin
Expand Down Expand Up @@ -2948,8 +2948,8 @@ package QueueTest
struct Queue[a](front: List[a], back: List[a])
def fold_Queue(Queue(f, b): Queue[a], binit: b, fold_fn: (b, a) -> b) -> b:
front = f.foldLeft(binit, fold_fn)
b.reverse().foldLeft(front, fold_fn)
front = f.foldl_List(binit, fold_fn)
b.reverse().foldl_List(front, fold_fn)
test = Assertion(Queue([1], [2]).fold_Queue(0, add).eq_Int(3), "foldQueue")
"""),
Expand Down Expand Up @@ -3334,7 +3334,7 @@ package Foo
export comp, ignore
def f(fn: forall a. List[a] -> List[a]) -> Int:
fn([1]).foldLeft(0, (x, _) -> x.add(1))
fn([1]).foldl_List(0, (x, _) -> x.add(1))
def g(b: Bool) -> (forall a. List[a] -> List[a]):
match b:
Expand All @@ -3347,10 +3347,10 @@ def id(a): a
def single(a): [a]
def foo1(fn) -> Int:
fn.foldLeft(0, (x, _) -> x.add(1))
fn.foldl_List(0, (x, _) -> x.add(1))
def foo2(fn: List[forall a. a -> a]) -> Int:
fn.foldLeft(0, (x, _) -> x.add(1))
fn.foldl_List(0, (x, _) -> x.add(1))
count0 = foo1(single(id))
count = foo2(single(id))
Expand Down Expand Up @@ -4044,7 +4044,7 @@ main = foo
List("""
package P
from Bosatsu/Predef import foldLeft
from Bosatsu/Predef import foldl_List
main = Assertion(True, "")
"""),
Expand All @@ -4056,7 +4056,7 @@ main = Assertion(True, "")
List("""
package P
from Bosatsu/Predef import foldLeft as fl
from Bosatsu/Predef import foldl_List as fl
res = [].fl(True, (_, _) -> False)
Expand All @@ -4069,15 +4069,15 @@ main = Assertion(res, "")
package P
from Bosatsu/Predef import concat as fl
from Bosatsu/Predef import foldLeft as fl
from Bosatsu/Predef import foldl_List as fl
res = [].fl(True, (_, _) -> False)
main = Assertion(res, "")
""")) { case kie @ PackageError.DuplicatedImport(_, _) =>
val message = kie.message(Map.empty, Colorize.None)
assert(
message == "duplicate import in <unknown source> package P\n\tfrom Bosatsu/Predef import concat as fl\n\tfrom Bosatsu/Predef import foldLeft as fl\n"
message == "duplicate import in <unknown source> package P\n\tfrom Bosatsu/Predef import concat as fl\n\tfrom Bosatsu/Predef import foldl_List as fl\n"
)
()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ int main(int argc, char** argv) {
}""")
}

test("check foldLeft and reverse_concat") {
assertPredefFns("foldLeft", "reverse_concat")("""#include "bosatsu_runtime.h"
test("check foldl_List and reverse_concat") {
assertPredefFns("foldl_List", "reverse_concat")("""#include "bosatsu_runtime.h"
#include <stdlib.h>
#include "gc.h"
Expand Down Expand Up @@ -144,7 +144,7 @@ BValue __bsts_t_closure0(BValue* __bstsi_slot,
return __bsts_a_1;
}
BValue ___bsts_g_Bosatsu_l_Predef_l_foldLeft(BValue __bsts_b_lst0,
BValue ___bsts_g_Bosatsu_l_Predef_l_foldl__List(BValue __bsts_b_lst0,
BValue __bsts_b_item0,
BValue __bsts_b_fn0) {
BValue __bsts_l_captures2[1] = { __bsts_b_fn0 };
Expand All @@ -160,7 +160,7 @@ BValue __bsts_t_lambda3(BValue __bsts_b_tail0, BValue __bsts_b_h0) {
BValue ___bsts_g_Bosatsu_l_Predef_l_reverse__concat(BValue __bsts_b_front0,
BValue __bsts_b_back0) {
return ___bsts_g_Bosatsu_l_Predef_l_foldLeft(__bsts_b_front0,
return ___bsts_g_Bosatsu_l_Predef_l_foldl__List(__bsts_b_front0,
__bsts_b_back0,
alloc_boxed_pure_fn2(__bsts_t_lambda3));
}
Expand Down
6 changes: 3 additions & 3 deletions docs/src/main/paradox/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def operator ||(x, y):
def keep(i):
(i % 3 == 0) || (i % 5 == 0)
def sum(as): as.foldLeft(0, add)
def sum(as): as.foldl_List(0, add)
# here is the pytthon version:
# >>> sum(i for i in xrange(1000) if keep_fn(i))
# here is the python version:
# >>> sum(i for i in xrange(1000) if keep(i))
# 233168
#
# bosatsu version here
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/AvlTree.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def log2(i):
rr = n.div(2)
(rr, cnt.add(1)))

def all_n(n): range(n).foldLeft(Empty, add_i)
def all_n(n): range(n).foldl_List(Empty, add_i)

height_tests =(
# h < c log_2(n + 2) + b, c ~= 1.44, b ~= -1.33
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/BinNat.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def toBinNat(n: Int) -> BinNat:
(dec(n), fns)
)
# Now apply all the transformations
fns.foldLeft(Zero, (n, fn) -> fn(n))
fns.foldl_List(Zero, (n, fn) -> fn(n))

def cmp_BinNat(a: BinNat, b: BinNat) -> Comparison:
recur a:
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/List.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def for_all(xs: List[a], fn: a -> Bool) -> Bool:
else: False

def sum(as: List[Int]) -> Int:
as.foldLeft(0, add)
as.foldl_List(0, add)

def exists(xs: List[a], fn: a -> Bool) -> Bool:
recur xs:
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/Prog.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ to_run = Main(
read_env.flat_map(args ->
(
_ <- count_down(10).await()
arg_str = args.foldLeft("", (s, item) -> match s:
arg_str = args.foldl_List("", (s, item) -> match s:
case "": item
case _: "${s}, ${item}")
_ <- println("args = ${arg_str}").await()
Expand Down
4 changes: 2 additions & 2 deletions test_workspace/Queue.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def pop(queue: Queue[a]) -> Queue[a]:
None: empty

def fold_Queue(Queue(f, b): Queue[a], init: b, fold_fn: (b, a) -> b) -> b:
front = f.foldLeft(init, fold_fn)
b.reverse().foldLeft(front, fold_fn)
front = f.foldl_List(init, fold_fn)
b.reverse().foldl_List(front, fold_fn)

def reverse_Queue(Queue(f, b): Queue[a]) -> Queue[a]:
Queue(b, f)
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/TreeList.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get(TreeList(trees): TreeList[a], idx: Int) -> Option[a]:
item

def from_List(list: List[a]) -> TreeList[a]:
list.reverse().foldLeft(empty, \lst, h -> cons(h, lst))
list.reverse().foldl_List(empty, \lst, h -> cons(h, lst))

def fold(TreeList(trees): TreeList[a], init: b, fn: (b, a) -> b) -> b:
def loop(trees, init):
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/bazel_deps_api.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def merge_deps(left: Dict[String, Dict[String, Artifact]], right: Dict[String, D
merge_inner = \l, r -> merge_Dict(l, r, merge_artifact)
merge_Dict(left, right, merge_inner)

def merge_dep_List(deps): deps.foldLeft({}, merge_deps)
def merge_dep_List(deps): deps.foldl_List({}, merge_deps)

standard_scala_replacements = {
'org.scala-lang': {
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/dicttools.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package DictTools
export merge

def merge(left: Dict[k, v], right: Dict[k, v], fn: (v, v) -> v) -> Dict[k, v]:
right.items().foldLeft(left, (d, (k, v)) ->
right.items().foldl_List(left, (d, (k, v)) ->
match d.get_key(k):
None: d.add_key(k, v)
Some(v0): d.add_key(k, fn(v0, v))
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/euler1.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def operator ||(x, y):
def keep(i):
(i % 3 == 0) || (i % 5 == 0)

def sum(as): as.foldLeft(0, add)
def sum(as): as.foldl_List(0, add)

# >>> sum(i for i in xrange(1000) if keep_fn(i))
# 233168
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/euler2.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ from Bosatsu/List import sum
# so n = 35 is enough

def fib(n):
range(n).foldLeft([], \revFib, _ ->
range(n).foldl_List([], \revFib, _ ->
match revFib:
[]: [1]
[h]: [2, h]
Expand Down
2 changes: 1 addition & 1 deletion test_workspace/euler7.bosatsu
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def none_divide(primes, x):
def ith_prime(total):
max_size = range(total.times(total))
# we could do int_loop or int_loop up, this was a bit simpler
(ps, _) = max_size.foldLeft(([], 0), \prime_cnt, i ->
(ps, _) = max_size.foldl_List(([], 0), \prime_cnt, i ->
#2 is the first prime
candidate = i.add(2)
(primes, cnt) = prime_cnt
Expand Down

0 comments on commit 869c30b

Please sign in to comment.