Skip to content

Commit

Permalink
fix the tutorial (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
konnov authored Oct 10, 2023
1 parent fe613f2 commit bf28c76
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
18 changes: 9 additions & 9 deletions tutorials/lesson4-sets/sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,20 @@ themselves.
pure def someCoinsExample = Set("ATOM", "ETH", "USDT")
// which coins can be bought by swapping a coin from `someCoins` once?
val buyableVia1Swap(someCoins) =
pure def buyableVia1Swap(someCoins) =
availableUnorderedPairs
.filter(p => p.intersect(someCoins).size() == 1)
.flatten()
.exclude(someCoins)
// which coins can be bought by swapping a coin from `someCoins` exactly twice?
val buyableVia2Swaps(someCoins) =
pure def buyableVia2Swaps(someCoins) =
someCoins
.buyableVia1Swap()
.buyableVia1Swap()
// which coins can be bought by swapping a coin from `someCoins` twice?
val buyableVia1or2Swaps(someCoins) =
pure def buyableVia1or2Swaps(someCoins) =
buyableVia1Swap(someCoins)
.union(buyableVia2Swaps(someCoins))
```
Expand Down Expand Up @@ -447,7 +447,7 @@ echo 'buyableVia1or2Swaps(Set("ATOM"))' | quint -r sets.qnt::sets
```bluespec
// which coins can we buy via n swaps when starting from `someCoins`
val buyableNSwaps(someCoins, n) =
pure def buyableNSwaps(someCoins, n) =
1.to(n).fold(someCoins,
(prevCoins, i) => buyableVia1Swap(prevCoins))
```
Expand Down Expand Up @@ -530,26 +530,26 @@ echo 'buyableNSwaps(Set("ATOM"), 5)' | quint -r sets.qnt::sets
```bluespec
// inSet.exists(myFun) can be expressed via `fold`, when `inSet` is finite
val myExists(inSet: Set[a], pred: a => bool): bool =
pure def myExists(inSet: Set[a], pred: a => bool): bool =
inSet.fold(false, (result, elem) => result or pred(elem))
// inSet.forall(myFun) can be expressed via `fold`, when `inSet` is finite
val myForall(inSet: Set[a], pred: a => bool): bool =
pure def myForall(inSet: Set[a], pred: a => bool): bool =
inSet.fold(true, (result, elem) => result and pred(elem))
// inSet.filter(myFun) can be expressed via `fold`, when `inSet` is finite
val myFilter(inSet: Set[a], pred: a => bool): Set[a] =
pure def myFilter(inSet: Set[a], pred: a => bool): Set[a] =
inSet.fold(Set(),
(result, elem) =>
if (pred(elem)) result.union(Set(elem)) else result)
// inSet.map(myFun) can be expressed via `fold`, when `inSet` is finite
val myMap(inSet: Set[a], mapper: a => b): Set[b] =
pure def myMap(inSet: Set[a], mapper: a => b): Set[b] =
inSet.fold(Set(),
(result, elem) => result.union(Set(mapper(elem))))
// flatten(inSet) can be expressed via `fold`, when `inSet` is finite
val myFlatten(inSet: Set[Set[a]]): Set[a] =
pure def myFlatten(inSet: Set[Set[a]]): Set[a] =
inSet.fold(Set(), (result, elem) => result.union(elem) )
```

Expand Down
18 changes: 9 additions & 9 deletions tutorials/lesson4-sets/sets.qnt
Original file line number Diff line number Diff line change
Expand Up @@ -53,49 +53,49 @@ module sets {
pure def someCoinsExample = Set("ATOM", "ETH", "USDT")

// which coins can be bought by swapping a coin from `someCoins` once?
val buyableVia1Swap(someCoins) =
pure def buyableVia1Swap(someCoins) =
availableUnorderedPairs
.filter(p => p.intersect(someCoins).size() == 1)
.flatten()
.exclude(someCoins)

// which coins can be bought by swapping a coin from `someCoins` exactly twice?
val buyableVia2Swaps(someCoins) =
pure def buyableVia2Swaps(someCoins) =
someCoins
.buyableVia1Swap()
.buyableVia1Swap()

// which coins can be bought by swapping a coin from `someCoins` twice?
val buyableVia1or2Swaps(someCoins) =
pure def buyableVia1or2Swaps(someCoins) =
buyableVia1Swap(someCoins)
.union(buyableVia2Swaps(someCoins))

// which coins can we buy via n swaps when starting from `someCoins`
val buyableNSwaps(someCoins, n) =
pure def buyableNSwaps(someCoins, n) =
1.to(n).fold(someCoins,
(prevCoins, i) => buyableVia1Swap(prevCoins))

// inSet.exists(myFun) can be expressed via `fold`, when `inSet` is finite
val myExists(inSet: Set[a], pred: a => bool): bool =
pure def myExists(inSet: Set[a], pred: a => bool): bool =
inSet.fold(false, (result, elem) => result or pred(elem))

// inSet.forall(myFun) can be expressed via `fold`, when `inSet` is finite
val myForall(inSet: Set[a], pred: a => bool): bool =
pure def myForall(inSet: Set[a], pred: a => bool): bool =
inSet.fold(true, (result, elem) => result and pred(elem))

// inSet.filter(myFun) can be expressed via `fold`, when `inSet` is finite
val myFilter(inSet: Set[a], pred: a => bool): Set[a] =
pure def myFilter(inSet: Set[a], pred: a => bool): Set[a] =
inSet.fold(Set(),
(result, elem) =>
if (pred(elem)) result.union(Set(elem)) else result)

// inSet.map(myFun) can be expressed via `fold`, when `inSet` is finite
val myMap(inSet: Set[a], mapper: a => b): Set[b] =
pure def myMap(inSet: Set[a], mapper: a => b): Set[b] =
inSet.fold(Set(),
(result, elem) => result.union(Set(mapper(elem))))

// flatten(inSet) can be expressed via `fold`, when `inSet` is finite
val myFlatten(inSet: Set[Set[a]]): Set[a] =
pure def myFlatten(inSet: Set[Set[a]]): Set[a] =
inSet.fold(Set(), (result, elem) => result.union(elem) )

// all combinations of unordered pairs that have exactly 4 pairs
Expand Down
18 changes: 9 additions & 9 deletions tutorials/lesson4-sets/sets.template.qnt
Original file line number Diff line number Diff line change
Expand Up @@ -306,20 +306,20 @@ themselves.
pure def someCoinsExample = Set("ATOM", "ETH", "USDT")

// which coins can be bought by swapping a coin from `someCoins` once?
val buyableVia1Swap(someCoins) =
pure def buyableVia1Swap(someCoins) =
availableUnorderedPairs
.filter(p => p.intersect(someCoins).size() == 1)
.flatten()
.exclude(someCoins)

// which coins can be bought by swapping a coin from `someCoins` exactly twice?
val buyableVia2Swaps(someCoins) =
pure def buyableVia2Swaps(someCoins) =
someCoins
.buyableVia1Swap()
.buyableVia1Swap()

// which coins can be bought by swapping a coin from `someCoins` twice?
val buyableVia1or2Swaps(someCoins) =
pure def buyableVia1or2Swaps(someCoins) =
buyableVia1Swap(someCoins)
.union(buyableVia2Swaps(someCoins))
/*!
Expand Down Expand Up @@ -395,7 +395,7 @@ Also easy: Just take the set union of what is possible in one or two swaps:
!*/

// which coins can we buy via n swaps when starting from `someCoins`
val buyableNSwaps(someCoins, n) =
pure def buyableNSwaps(someCoins, n) =
1.to(n).fold(someCoins,
(prevCoins, i) => buyableVia1Swap(prevCoins))
/*!
Expand Down Expand Up @@ -465,26 +465,26 @@ Now @KryptoCoffeeCat can compute what they can buy via various numbers of swaps:
!*/

// inSet.exists(myFun) can be expressed via `fold`, when `inSet` is finite
val myExists(inSet: Set[a], pred: a => bool): bool =
pure def myExists(inSet: Set[a], pred: a => bool): bool =
inSet.fold(false, (result, elem) => result or pred(elem))

// inSet.forall(myFun) can be expressed via `fold`, when `inSet` is finite
val myForall(inSet: Set[a], pred: a => bool): bool =
pure def myForall(inSet: Set[a], pred: a => bool): bool =
inSet.fold(true, (result, elem) => result and pred(elem))

// inSet.filter(myFun) can be expressed via `fold`, when `inSet` is finite
val myFilter(inSet: Set[a], pred: a => bool): Set[a] =
pure def myFilter(inSet: Set[a], pred: a => bool): Set[a] =
inSet.fold(Set(),
(result, elem) =>
if (pred(elem)) result.union(Set(elem)) else result)

// inSet.map(myFun) can be expressed via `fold`, when `inSet` is finite
val myMap(inSet: Set[a], mapper: a => b): Set[b] =
pure def myMap(inSet: Set[a], mapper: a => b): Set[b] =
inSet.fold(Set(),
(result, elem) => result.union(Set(mapper(elem))))

// flatten(inSet) can be expressed via `fold`, when `inSet` is finite
val myFlatten(inSet: Set[Set[a]]): Set[a] =
pure def myFlatten(inSet: Set[Set[a]]): Set[a] =
inSet.fold(Set(), (result, elem) => result.union(elem) )
/*!
<step>
Expand Down

0 comments on commit bf28c76

Please sign in to comment.