roc-lang / examples Goto Github PK
View Code? Open in Web Editor NEWAll kinds of Roc examples
Home Page: https://www.roc-lang.org/examples
License: Creative Commons Zero v1.0 Universal
All kinds of Roc examples
Home Page: https://www.roc-lang.org/examples
License: Creative Commons Zero v1.0 Universal
Task.loop
can be intimidating, so we should have good examples for it with some explanation.
Inspiration:
https://roc.zulipchat.com/#narrow/stream/231634-beginners/topic/read.20.20file/near/392471155
https://github.com/search?q=repo%3Aroc-lang%2Froc%20%22loop%22%20path%3A*.roc&type=code
https://github.com/search?q=repo%3Aroc-lang%2Fbasic-cli%20loop%20path%3A*.roc&type=code
This came up on zulip yesterday.
"how to create stand alone Dec number":
2.1dec
I think we should do separate examples for every type. For code snippets we want to give the user what they need and nothing more.
We probably also want to add a warning here that we don't yet recommend using roc for production systems that handle real money ๐
See this message for inspiration.
This fails with the latest roc nightly, not sure why though
โฏ ./roc_nightly/roc test ./examples/PatternMatching/PatternMatching.roc
โโ EXPECT FAILED โโโโโโโโโโโโโโ ./examples/PatternMatching/PatternMatching.roc โ
This expectation failed:
55โ> # Pattern match on a list with these exact elements: Foo, Bar, and then Baz
56โ> expect
57โ> patternMatch = \input ->
58โ> when input is
59โ> [Foo, Bar, Baz] -> Bingo
60โ> _ -> Other
61โ>
62โ> (patternMatch [Foo, Bar, Baz] == Bingo)
63โ> && (patternMatch [Foo, Bar] != Bingo)
64โ> && (patternMatch [Foo, Bar, Baz, Barry] != Bingo)
1 failed and 6 passed in 197 ms.
Line 65 under the TODO works, so it can be re-enabled, #25 was merged:
# TODO: reenable line below when roc-lang/examples/issues/25 is fixed
# && (patternMatch [Foo, Bar, Baz, Barry] != Bingo)
See https://www.roc-lang.org/tutorial#opaque-types for inspiration
see https://www.roc-lang.org/tutorial#debugging for inspiration
for example, roc code that prints a string like this: "this is a number: (Num.toStr someNumber)"
Also mention these constraints:
roc-pg is likely useful when looking at this issue.
Perhaps we should keep the folder names CamelCase to make it easy to distinguish when we are using folders?
imports [
"some-file" as someStr : Str,
"some-file" as someBytes : List U8,
]
Because I want examples to be what users are looking to achieve and nothing more I think "importing as Str" and "importing as List U8
" should be two different examples. But they should link to each other, for example: "Importing a list of bytes may be bettere suited for your case. Read about it here"
We already have this record builder example: https://github.com/roc-lang/basic-cli/blob/main/examples/record-builder.roc
I suppose it's ok to duplicate it in this repo.
I believe this is blocked on roc-lang/roc#5660
Knowing when to use which is important in Roc!
This no longer works with the latest roc nightly, I think the json package json: "https://github.com/lukewilliamboswell/roc-package-explorations/releases/download/0.0.1/cdKMia6cwdRG6Gb0SfXP8cgGF7yTz-i959FV6ZfuS0E.tar.br"
needs to be updated.
> ./roc_nightly/roc run ./examples/json-basic/main.roc
Downloading https://github.com/roc-lang/basic-cli/releases/download/0.3.2/tE4xS_zLdmmxmHwHih9kHWQ7fsXtJr7W7h3425-eZFk.tar.br
into /home/runner/.cache/roc/packages
Downloading https://github.com/lukewilliamboswell/roc-package-explorations/releases/download/0.0.1/cdKMia6cwdRG6Gb0SfXP8cgGF7yTz-i959FV6ZfuS0E.tar.br
into /home/runner/.cache/roc/packages
โโ INCOMPLETE ABILITY IMPLEMENTATION โ ...SfXP8cgGF7yTz-i959FV6ZfuS0E/Core.roc โ
This type does not fully implement the EncoderFormatting ability:
41โ> EncoderFormatting {
42โ> u8: encodeU8,
43โ> u16: encodeU16,
44โ> u32: encodeU32,
45โ> u64: encodeU64,
46โ> u128: encodeU128,
47โ> i8: encodeI8,
48โ> i16: encodeI16,
49โ> i32: encodeI32,
50โ> i64: encodeI64,
51โ> i128: encodeI128,
52โ> f32: encodeF32,
53โ> f64: encodeF64,
54โ> dec: encodeDec,
55โ> bool: encodeBool,
56โ> string: encodeString,
57โ> list: encodeList,
58โ> record: encodeRecord,
59โ> tag: encodeTag,
60โ> },
The following necessary members are missing implementations:
tuple
โโ INCOMPLETE ABILITY IMPLEMENTATION โ ...SfXP8cgGF7yTz-i959FV6ZfuS0E/Core.roc โ
This type does not fully implement the DecoderFormatting ability:
61โ> DecoderFormatting {
62โ> u8: decodeU8,
63โ> u16: decodeU16,
64โ> u32: decodeU32,
65โ> u64: decodeU64,
66โ> u128: decodeU128,
67โ> i8: decodeI8,
68โ> i16: decodeI16,
69โ> i32: decodeI32,
70โ> i64: decodeI64,
71โ> i128: decodeI128,
72โ> f32: decodeF32,
73โ> f64: decodeF64,
74โ> dec: decodeDec,
75โ> bool: decodeBool,
76โ> string: decodeString,
77โ> list: decodeList,
78โ> record: decodeRecord,
79โ> },
The following necessary members are missing implementations:
tuple
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2 errors and 0 warnings found in 3057 ms.
Tuples are not mentioned in the tutorial so it would be nice to have a basic example demonstrating how they are commonly used.
See here for inspiration.
Deconstructing records with optional fields can be shown here as well:
clamp = \x, config ->
{ min ? x, max ? x } = config
min + max
Many people will be unfamiliar with Roc error handling so I think we need a good example for it, some suggestions:
% roc test examples/PatternMatching/PatternMatching.roc
ld: warning: -undefined dynamic_lookup may not work with chained fixups
0 failed and 7 passed in 572 ms.
See writeUtf8
See readUtf8
.
This example should also mention this example as a better alternative in certain situations.
See conversation here
We should make an example for this once we figure it out ourselves :p
Example of a problematic case:
{} <- log "Extracting current rust version from rust-toolchain.toml..." |> await
{ stable: currentStable, nightly: currentNightly } <- getCurrentVersionsFromToml |> await
{} <- log "Found versions:\n\t- stable: \(versionToStr currentStable)\n\t- nightly with date: \(dateToStr currentNightly)" |> await
# TODO Task.map to avoid repeated `{} <-`?
{} <- log "Searching for nightly matching stable-\(newRustVersion)..." |> await
newNightly <-
findMatchingNigthly (versionFromStr newRustVersion) currentNightly
|> await
{} <- log "New nightly: nightly-\(dateToStr newNightly)" |> await
TODO can we avoid boiler plate of `{} <-` and `await` here?
{} <-
Path.fromStr "rust-toolchain.toml"
|> updateToml newRustVersion
|> await
{} <-
Path.fromStr "examples/platform-switching/rust-platform/rust-toolchain.toml"
|> updateToml newRustVersion
|> await
{} <-
Path.fromStr "Earthfile"
|> updateEarthFile newRustVersion
|> await
The example could show an imperative c-style for loop and the equivalent in Roc.
In line with the goal "The code snippet of the example should concisely provide the code that the user is looking for.", readargs should not be demonstrated in the Arithmetic example. We should just use fixed values instead of cli args.
We should have another example to demonstrate command line args: #39
Inspiration https://www.roc-lang.org/tutorial#tasks
We should include several examples of how the desugaring works.
Run ./roc_nightly/roc run main.roc -- examples build
๐จ Rebuilding platform...
Processing 47 input files...
/home/runner/work/examples/examples/examples/CommandLineArgs/README.md -> /home/runner/work/examples/examples/build/CommandLineArgs/README.html
/home/runner/work/examples/examples/examples/TowersOfHanoi/README.md -> /home/runner/work/examples/examples/build/TowersOfHanoi/README.html
/home/runner/work/examples/examples/examples/GraphTraversal/README.md -> /home/runner/work/examples/examples/build/GraphTraversal/README.html
/home/runner/work/examples/examples/examples/PatternMatching/README.md -> /home/runner/work/examples/examples/build/PatternMatching/README.html
/home/runner/work/examples/examples/examples/Arithmetic/README.md -> /home/runner/work/examples/examples/build/Arithmetic/README.html
/home/runner/work/examples/examples/examples/HelloWorld/README.md -> /home/runner/work/examples/examples/build/HelloWorld/README.html
/home/runner/work/examples/examples/examples/RandomNumbers/README.md -> /home/runner/work/examples/examples/build/RandomNumbers/README.html
/home/runner/work/examples/examples/examples/TaskUsage/README.md -> /home/runner/work/examples/examples/build/TaskUsage/README.html
/home/runner/work/examples/examples/examples/index.md -> /home/runner/work/examples/examples/build/index.html
/home/runner/work/examples/examples/examples/LeastSquares/README.md -> /home/runner/work/examples/examples/build/LeastSquares/README.html
/home/runner/work/examples/examples/examples/Tuples/README.md -> /home/runner/work/examples/examples/build/Tuples/README.html
/home/runner/work/examples/examples/examples/IngestFiles/README.md -> /home/runner/work/examples/examples/build/IngestFiles/README.html
/home/runner/work/examples/examples/examples/Parser/README.md -> /home/runner/work/examples/examples/build/Parser/README.html
/home/runner/work/examples/examples/examples/FizzBuzz/README.md -> /home/runner/work/examples/examples/build/FizzBuzz/README.html
/home/runner/work/examples/examples/examples/Json/README.md -> /home/runner/work/examples/examples/build/Json/README.html
Processed 47 files with 15 successes and 0 errors
/home/runner/work/_temp/ea8097f9-5ff1-4662-9f07-4ff810955e2d.sh: line 1: 2975 Segmentation fault (core dumped) ./roc_nightly/roc run main.roc -- examples build
Just going through the examples, thank you so much for the effort you are making here.
I am reading through the Arithmetic example and I think there's the obvious edge case about dividing by zero.
According to roc's current div documentation:
Division by zero is undefined in mathematics. As such, you should make sure never to pass zero as the denomaintor to this function! Calling div on a Dec denominator of zero will cause a panic.
Possible solutions:
Adjust the example text to state that if the second argument is 0
, we can skip the integer quotient
operation;
Adjust the example text to state that zero is not a valid input for the second argument, and add an explicit check on the readArgs
function for this case
Adjust the example text to state that an input of 0 on the second argument is allowed to crash with a Floating point exception
My preference would be between solution 1 or 2, or a smarter one :)
Let me know your thoughts and once again, thank you for this amazing effort!
{ min, max } = config
Tag should say EndswithFoo
# Pattern match on a list that ends with a Foo tag
expect
patternMatch = \input ->
when input is
[.., Foo] -> StartsWithFoo
_ -> Other
patternMatch [Baz, Bar, Foo] == StartsWithFoo &&
patternMatch [Foo, Bar, Baz] != StartsWithFoo
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.