Skip to content

Commit

Permalink
- prerelease version bump
Browse files Browse the repository at this point in the history
- implemented Map.fold_with_list
  • Loading branch information
christianschmitz committed Aug 7, 2024
1 parent 2b1489e commit b004d18
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helios-lang/compiler",
"version": "0.17.0-17",
"version": "0.17.0-18",
"description": "Helios is a Domain Specific Language that compiles to Plutus-Core (i.e. Cardano on-chain validator scripts). Helios is a non-Haskell alternative to Plutus. With this library you can compile Helios scripts and build Cardano transactions, all you need to build 100% client-side dApps for Cardano.",
"main": "src/index.js",
"types": "types/index.d.ts",
Expand Down
27 changes: 27 additions & 0 deletions src/codegen/makeRawFuncs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5770,6 +5770,33 @@ export function makeRawFunctions(simplify, isTestnet) {
}`
)
)
add(
new RawFunc(
`__helios__map[${TTPP}0@${TTPP}1]__fold_with_list[${FTPP}0@${FTPP}1]`,
`(self) -> {
(fn, z0, list) -> {
recurse = (map, list) -> {
__core__chooseList(
map,
() -> {
z0
},
() -> {
z = recurse(__core__tailList(map), __core__tailList(list));
item = ${FTPP}1__from_data(__core__headList(list));
key_value = __core__headList(map);
key = ${TTPP}0__from_data(__core__fstPair(key_value));
value = ${TTPP}1__from_data(__core__sndPair(key_value));
fn(z, key, value, item)
}
)()
};
recurse(self, list)
}
}`
)
)
add(
new RawFunc(
`__helios__map[${TTPP}0@${TTPP}1]__for_each`,
Expand Down
2 changes: 1 addition & 1 deletion src/program/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.17.0-17"
export const VERSION = "0.17.0-18"
23 changes: 23 additions & 0 deletions src/typecheck/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,29 @@ export const MapType = new ParametricType({
)
)
})(),
fold_with_list: (() => {
const a = new Parameter("a", `${FTPP}0`, new AnyTypeClass())
const b = new Parameter(
"b",
`${FTPP}1`,
new DefaultTypeClass()
)

return new ParametricFunc(
[a, b],
new FuncType(
[
new FuncType(
[a.ref, keyType, valueType, b.ref],
a.ref
),
a.ref,
ListType$(b.ref)
],
a.ref
)
)
})(),
for_each: new FuncType(
[new FuncType([keyType, valueType], new VoidType())],
new VoidType()
Expand Down
53 changes: 52 additions & 1 deletion test/map.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe } from "node:test"
import { compileAndRunMany, True, map, int, False } from "./utils.js"
import { compileAndRunMany, True, map, int, False, list } from "./utils.js"

describe("Map", () => {
const mapEqScript = `testing map_eq
Expand Down Expand Up @@ -27,6 +27,13 @@ describe("Map", () => {
a.any_value((k_: Int) -> {k_ == k})
}`

const mapFoldWithListScript = `testing map_fold_with_list
func main(a: Map[Int]Int, z0: Int, b: []Int) -> Int {
a.fold_with_list((z: Int, key: Int, value: Int, item: Int) -> {
z + (key + value)*item
}, z0, b)
}`

compileAndRunMany([
{
description: "{1: 1} == {1: 1} is true",
Expand Down Expand Up @@ -125,6 +132,50 @@ describe("Map", () => {
int(3)
],
output: True
},
{
description:
"fold_with_list throws an error if the list is shorter",
main: mapFoldWithListScript,
inputs: [
map([
[int(1), int(1)],
[int(2), int(2)],
[int(3), int(3)]
]),
int(0),
list(int(1), int(1))
],
output: { error: "" }
},
{
description: "fold_with_list correctly sums",
main: mapFoldWithListScript,
inputs: [
map([
[int(1), int(1)],
[int(2), int(2)],
[int(3), int(3)]
]),
int(0),
list(int(1), int(1), int(1))
],
output: int(12)
},
{
description:
"fold_with_list correctly sums even if list is too long",
main: mapFoldWithListScript,
inputs: [
map([
[int(1), int(1)],
[int(2), int(2)],
[int(3), int(3)]
]),
int(0),
list(int(1), int(1), int(1), int(1))
],
output: int(12)
}
])
})

0 comments on commit b004d18

Please sign in to comment.