mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-14 10:15:41 +00:00
Revert "Avoid defining unsized platform Tasks"
This reverts commit d3732b4440.
This commit is contained in:
parent
d3732b4440
commit
ca7d1cb3fa
18 changed files with 105 additions and 109 deletions
|
|
@ -5,9 +5,7 @@ import pf.PlatformTasks
|
||||||
# adapted from https://github.com/koka-lang/koka/blob/master/test/bench/haskell/cfold.hs
|
# adapted from https://github.com/koka-lang/koka/blob/master/test/bench/haskell/cfold.hs
|
||||||
main : Task {} []
|
main : Task {} []
|
||||||
main =
|
main =
|
||||||
{ value, isError } =
|
{ value, isError } = PlatformTasks.getInt!
|
||||||
PlatformTasks.getInt
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
inputResult =
|
inputResult =
|
||||||
if isError then
|
if isError then
|
||||||
Err GetIntError
|
Err GetIntError
|
||||||
|
|
@ -25,11 +23,9 @@ main =
|
||||||
|> Str.concat " & "
|
|> Str.concat " & "
|
||||||
|> Str.concat (Num.toStr optimized)
|
|> Str.concat (Num.toStr optimized)
|
||||||
|> PlatformTasks.putLine
|
|> PlatformTasks.putLine
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Err GetIntError ->
|
Err GetIntError ->
|
||||||
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Expr : [
|
Expr : [
|
||||||
Add Expr Expr,
|
Add Expr Expr,
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,7 @@ IO a : Task a []
|
||||||
|
|
||||||
main : Task {} []
|
main : Task {} []
|
||||||
main =
|
main =
|
||||||
{ value, isError } =
|
{ value, isError } = PlatformTasks.getInt!
|
||||||
PlatformTasks.getInt
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
inputResult =
|
inputResult =
|
||||||
if isError then
|
if isError then
|
||||||
Err GetIntError
|
Err GetIntError
|
||||||
|
|
@ -29,7 +27,6 @@ main =
|
||||||
|
|
||||||
Err GetIntError ->
|
Err GetIntError ->
|
||||||
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
nestHelp : I64, (I64, Expr -> IO Expr), I64, Expr -> IO Expr
|
nestHelp : I64, (I64, Expr -> IO Expr), I64, Expr -> IO Expr
|
||||||
nestHelp = \s, f, m, x ->
|
nestHelp = \s, f, m, x ->
|
||||||
|
|
@ -170,7 +167,5 @@ deriv = \i, f ->
|
||||||
Num.toStr (i + 1)
|
Num.toStr (i + 1)
|
||||||
|> Str.concat " count: "
|
|> Str.concat " count: "
|
||||||
|> Str.concat (Num.toStr (count fprime))
|
|> Str.concat (Num.toStr (count fprime))
|
||||||
PlatformTasks.putLine line
|
PlatformTasks.putLine! line
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Task.ok fprime
|
Task.ok fprime
|
||||||
|
|
|
||||||
|
|
@ -11,4 +11,3 @@ main =
|
||||||
Issue2279Help.asText 42
|
Issue2279Help.asText 42
|
||||||
|
|
||||||
PlatformTasks.putLine text
|
PlatformTasks.putLine text
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,7 @@ import pf.PlatformTasks
|
||||||
|
|
||||||
main : Task {} []
|
main : Task {} []
|
||||||
main =
|
main =
|
||||||
{ value, isError } =
|
{ value, isError } = PlatformTasks.getInt!
|
||||||
PlatformTasks.getInt
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
inputResult =
|
inputResult =
|
||||||
if isError then
|
if isError then
|
||||||
Err GetIntError
|
Err GetIntError
|
||||||
|
|
@ -18,11 +16,9 @@ main =
|
||||||
queens n # original koka 13
|
queens n # original koka 13
|
||||||
|> Num.toStr
|
|> Num.toStr
|
||||||
|> PlatformTasks.putLine
|
|> PlatformTasks.putLine
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Err GetIntError ->
|
Err GetIntError ->
|
||||||
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
ConsList a : [Nil, Cons a (ConsList a)]
|
ConsList a : [Nil, Cons a (ConsList a)]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ hosted PlatformTasks
|
||||||
exposes [putLine, putInt, getInt]
|
exposes [putLine, putInt, getInt]
|
||||||
imports []
|
imports []
|
||||||
|
|
||||||
putLine : Str -> Task {} {}
|
putLine : Str -> Task {} *
|
||||||
|
|
||||||
putInt : I64 -> Task {} {}
|
putInt : I64 -> Task {} *
|
||||||
|
|
||||||
getInt : Task { value : I64, isError : Bool } {}
|
getInt : Task { value : I64, isError : Bool } *
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,7 @@ import Quicksort
|
||||||
|
|
||||||
main : Task.Task {} []
|
main : Task.Task {} []
|
||||||
main =
|
main =
|
||||||
{ value, isError } =
|
{ value, isError } = PlatformTasks.getInt!
|
||||||
PlatformTasks.getInt
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
inputResult =
|
inputResult =
|
||||||
if isError then
|
if isError then
|
||||||
Err GetIntError
|
Err GetIntError
|
||||||
|
|
@ -27,11 +25,9 @@ main =
|
||||||
sort unsortedList
|
sort unsortedList
|
||||||
|> Quicksort.show
|
|> Quicksort.show
|
||||||
|> PlatformTasks.putLine
|
|> PlatformTasks.putLine
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Err GetIntError ->
|
Err GetIntError ->
|
||||||
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
sort : List I64 -> List I64
|
sort : List I64 -> List I64
|
||||||
sort = \list ->
|
sort = \list ->
|
||||||
|
|
|
||||||
|
|
@ -39,9 +39,7 @@ fold = \f, tree, b ->
|
||||||
|
|
||||||
main : Task {} []
|
main : Task {} []
|
||||||
main =
|
main =
|
||||||
{ value, isError } =
|
{ value, isError } = PlatformTasks.getInt!
|
||||||
PlatformTasks.getInt
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
inputResult =
|
inputResult =
|
||||||
if isError then
|
if isError then
|
||||||
Err GetIntError
|
Err GetIntError
|
||||||
|
|
@ -61,15 +59,12 @@ main =
|
||||||
val
|
val
|
||||||
|> Num.toStr
|
|> Num.toStr
|
||||||
|> PlatformTasks.putLine
|
|> PlatformTasks.putLine
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Nil ->
|
Nil ->
|
||||||
PlatformTasks.putLine "fail"
|
PlatformTasks.putLine "fail"
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Err GetIntError ->
|
Err GetIntError ->
|
||||||
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
insert : Tree (Num k) v, Num k, v -> Tree (Num k) v
|
insert : Tree (Num k) v, Num k, v -> Tree (Num k) v
|
||||||
insert = \t, k, v -> if isRed t then setBlack (ins t k v) else ins t k v
|
insert = \t, k, v -> if isRed t then setBlack (ins t k v) else ins t k v
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,7 @@ ConsList a : [Nil, Cons a (ConsList a)]
|
||||||
|
|
||||||
main : Task {} []
|
main : Task {} []
|
||||||
main =
|
main =
|
||||||
{ value, isError } =
|
{ value, isError } = PlatformTasks.getInt!
|
||||||
PlatformTasks.getInt
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
inputResult =
|
inputResult =
|
||||||
if isError then
|
if isError then
|
||||||
Err GetIntError
|
Err GetIntError
|
||||||
|
|
@ -29,11 +27,9 @@ main =
|
||||||
val
|
val
|
||||||
|> Num.toStr
|
|> Num.toStr
|
||||||
|> PlatformTasks.putLine
|
|> PlatformTasks.putLine
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Err GetIntError ->
|
Err GetIntError ->
|
||||||
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
PlatformTasks.putLine "Error: Failed to get Integer from stdin."
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
boom : Str -> a
|
boom : Str -> a
|
||||||
boom = \_ -> boom ""
|
boom = \_ -> boom ""
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ main =
|
||||||
tree
|
tree
|
||||||
|> show
|
|> show
|
||||||
|> PlatformTasks.putLine
|
|> PlatformTasks.putLine
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
show : RedBlackTree I64 {} -> Str
|
show : RedBlackTree I64 {} -> Str
|
||||||
show = \tree -> showRBTree tree Num.toStr (\{} -> "{}")
|
show = \tree -> showRBTree tree Num.toStr (\{} -> "{}")
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,7 @@ import AStar
|
||||||
|
|
||||||
main : Task {} []
|
main : Task {} []
|
||||||
main =
|
main =
|
||||||
PlatformTasks.putLine (showBool test1)
|
PlatformTasks.putLine! (showBool test1)
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
showBool : Bool -> Str
|
showBool : Bool -> Str
|
||||||
showBool = \b ->
|
showBool = \b ->
|
||||||
|
|
|
||||||
|
|
@ -8,19 +8,10 @@ IO a : Task a []
|
||||||
main : IO {}
|
main : IO {}
|
||||||
main =
|
main =
|
||||||
when Base64.fromBytes (Str.toUtf8 "Hello World") is
|
when Base64.fromBytes (Str.toUtf8 "Hello World") is
|
||||||
Err _ ->
|
Err _ -> PlatformTasks.putLine "sadness"
|
||||||
PlatformTasks.putLine "sadness"
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Ok encoded ->
|
Ok encoded ->
|
||||||
PlatformTasks.putLine (Str.concat "encoded: " encoded)
|
PlatformTasks.putLine! (Str.concat "encoded: " encoded)
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
when Base64.toStr encoded is
|
when Base64.toStr encoded is
|
||||||
Ok decoded ->
|
Ok decoded -> PlatformTasks.putLine (Str.concat "decoded: " decoded)
|
||||||
PlatformTasks.putLine (Str.concat "decoded: " decoded)
|
Err _ -> PlatformTasks.putLine "sadness"
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Err _ ->
|
|
||||||
PlatformTasks.putLine "sadness"
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,6 @@ hosted PlatformTasks
|
||||||
exposes [putLine, getLine]
|
exposes [putLine, getLine]
|
||||||
imports []
|
imports []
|
||||||
|
|
||||||
putLine : Str -> Task {} {}
|
putLine : Str -> Task {} *
|
||||||
|
|
||||||
getLine : Task Str {}
|
getLine : Task Str *
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,8 @@ import pf.PlatformTasks
|
||||||
|
|
||||||
main : Task {} []
|
main : Task {} []
|
||||||
main =
|
main =
|
||||||
line =
|
line = PlatformTasks.getLine!
|
||||||
PlatformTasks.getLine
|
PlatformTasks.putLine! "You entered: $(line)"
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
PlatformTasks.putLine! "It is known"
|
||||||
PlatformTasks.putLine "You entered: $(line)"
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
PlatformTasks.putLine "It is known"
|
|
||||||
|> Task.mapErr! \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
Task.ok {}
|
Task.ok {}
|
||||||
|
|
|
||||||
|
|
@ -5,25 +5,18 @@ import pf.PlatformTasks
|
||||||
Handle := U64
|
Handle := U64
|
||||||
|
|
||||||
line : Handle -> Task Str *
|
line : Handle -> Task Str *
|
||||||
line = \@Handle handle ->
|
line = \@Handle handle -> PlatformTasks.getFileLine handle
|
||||||
PlatformTasks.getFileLine handle
|
|
||||||
|> Task.mapErr \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
chunk : Handle -> Task (List U8) *
|
chunk : Handle -> Task (List U8) *
|
||||||
chunk = \@Handle handle ->
|
chunk = \@Handle handle -> PlatformTasks.getFileBytes handle
|
||||||
PlatformTasks.getFileBytes handle
|
|
||||||
|> Task.mapErr \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
open : Str -> Task Handle *
|
open : Str -> Task Handle *
|
||||||
open = \path ->
|
open = \path ->
|
||||||
PlatformTasks.openFile path
|
PlatformTasks.openFile path
|
||||||
|> Task.map @Handle
|
|> Task.map @Handle
|
||||||
|> Task.mapErr \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
close : Handle -> Task.Task {} *
|
close : Handle -> Task.Task {} *
|
||||||
close = \@Handle handle ->
|
close = \@Handle handle -> PlatformTasks.closeFile handle
|
||||||
PlatformTasks.closeFile handle
|
|
||||||
|> Task.mapErr \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
withOpen : Str, (Handle -> Task {} a) -> Task {} a
|
withOpen : Str, (Handle -> Task {} a) -> Task {} a
|
||||||
withOpen = \path, callback ->
|
withOpen = \path, callback ->
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,20 @@ hosted PlatformTasks
|
||||||
exposes [openFile, closeFile, withFileOpen, getFileLine, getFileBytes, putLine, putRaw, getLine, getChar]
|
exposes [openFile, closeFile, withFileOpen, getFileLine, getFileBytes, putLine, putRaw, getLine, getChar]
|
||||||
imports []
|
imports []
|
||||||
|
|
||||||
openFile : Str -> Task U64 {}
|
openFile : Str -> Task U64 *
|
||||||
|
|
||||||
closeFile : U64 -> Task {} {}
|
closeFile : U64 -> Task {} *
|
||||||
|
|
||||||
withFileOpen : Str, (U64 -> Task ok err) -> Task {} {}
|
withFileOpen : Str, (U64 -> Task ok err) -> Task {} *
|
||||||
|
|
||||||
getFileLine : U64 -> Task Str {}
|
getFileLine : U64 -> Task Str *
|
||||||
|
|
||||||
getFileBytes : U64 -> Task (List U8) {}
|
getFileBytes : U64 -> Task (List U8) *
|
||||||
|
|
||||||
putLine : Str -> Task {} {}
|
putLine : Str -> Task {} *
|
||||||
|
|
||||||
putRaw : Str -> Task {} {}
|
putRaw : Str -> Task {} *
|
||||||
|
|
||||||
getLine : Task Str {}
|
getLine : Task Str *
|
||||||
|
|
||||||
getChar : Task U8 {}
|
getChar : Task U8 *
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,8 @@ module [
|
||||||
|
|
||||||
import pf.PlatformTasks
|
import pf.PlatformTasks
|
||||||
|
|
||||||
line : {} -> Task Str *
|
line : Task Str *
|
||||||
line = \{} ->
|
line = PlatformTasks.getLine
|
||||||
PlatformTasks.getLine
|
|
||||||
|> Task.mapErr \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
char : {} -> Task U8 *
|
char : Task U8 *
|
||||||
char = \{} ->
|
char = PlatformTasks.getChar
|
||||||
PlatformTasks.getChar
|
|
||||||
|> Task.mapErr \_ -> crash "unreachable"
|
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,7 @@ module [line, raw]
|
||||||
import pf.PlatformTasks
|
import pf.PlatformTasks
|
||||||
|
|
||||||
line : Str -> Task {} *
|
line : Str -> Task {} *
|
||||||
line = \text ->
|
line = PlatformTasks.putLine
|
||||||
PlatformTasks.putLine text
|
|
||||||
|> Task.mapErr \_ -> crash "unreachable"
|
|
||||||
|
|
||||||
raw : Str -> Task {} *
|
raw : Str -> Task {} *
|
||||||
raw = \text ->
|
raw = PlatformTasks.putRaw
|
||||||
PlatformTasks.putRaw text
|
|
||||||
|> Task.mapErr \_ -> crash "unreachable"
|
|
||||||
|
|
|
||||||
53
sort.roc
Normal file
53
sort.roc
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
app [main] { cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.13.0/nW9yMRtZuCYf1Oa9vbE5XoirMwzLbtoSgv7NGhUlqYA.tar.br" }
|
||||||
|
|
||||||
|
import cli.Stdout
|
||||||
|
import cli.Task
|
||||||
|
import cli.Arg
|
||||||
|
import cli.File
|
||||||
|
import cli.Utc
|
||||||
|
import cli.Task exposing [Task]
|
||||||
|
|
||||||
|
dataPath : U64 -> Str
|
||||||
|
dataPath = \day ->
|
||||||
|
"data/day$(if day < 10 then "0" else "")$(Num.toStr day).txt"
|
||||||
|
|
||||||
|
loadData : U64 -> Task Str _
|
||||||
|
loadData = \day ->
|
||||||
|
day
|
||||||
|
|> dataPath
|
||||||
|
|> File.readUtf8
|
||||||
|
|> Task.mapErr UnableToReadFile
|
||||||
|
|
||||||
|
solutions : List (Str -> Str, Str -> Str)
|
||||||
|
solutions = [
|
||||||
|
(\i -> i,\i -> i),
|
||||||
|
]
|
||||||
|
|
||||||
|
runSolution : (Str -> Str), U64, Str -> Task {} []_
|
||||||
|
runSolution = \solution, index, input ->
|
||||||
|
Stdout.line! "Part $(Num.toStr index):"
|
||||||
|
startTime = Utc.now!
|
||||||
|
result = solution input
|
||||||
|
endTime = Utc.now!
|
||||||
|
delta = Utc.deltaAsMillis startTime endTime |> Num.toStr
|
||||||
|
Stdout.line! "$(result) ($(delta)ms)"
|
||||||
|
|
||||||
|
checkDay : U64 -> Task {} [InvalidDay Str]_
|
||||||
|
checkDay = \day ->
|
||||||
|
if day < 1 || day > 25 then Task.err (InvalidDay "Must be between 1 and 25") else Task.ok {}
|
||||||
|
|
||||||
|
runDay : Str -> Task {} []_
|
||||||
|
runDay = \dayArg ->
|
||||||
|
day = Str.toU64 dayArg |> Task.fromResult!
|
||||||
|
checkDay! day
|
||||||
|
Stdout.line! "Day $(Num.toStr day)"
|
||||||
|
input = loadData! day
|
||||||
|
(part1, part2) = List.get solutions (day - 1) |> Task.fromResult!
|
||||||
|
runSolution! part1 1 input
|
||||||
|
runSolution! part2 2 input
|
||||||
|
|
||||||
|
main =
|
||||||
|
args = Arg.list! {}
|
||||||
|
daysStr = if List.len args < 2 then List.range { start: At 1, end: At 25 } |> List.map Num.toStr else args |> List.dropFirst 1
|
||||||
|
_ = daysStr |> List.map runDay |> List.reverse |> Task.sequence!
|
||||||
|
Task.ok {}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue