Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

nim example #40

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Rust/*/target/
Nim/*/*
!Nim/*/*.*
!Nim/*/*/
Comment on lines +2 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a way to simplify this?
What paths are we trying to ignore here?

Copy link
Author

@definitelynobody definitelynobody Aug 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can get around this so we can have an out dir. I will have to investigate a bit haven't used nim in forever.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default behavior will make a Nim/fibonacci/fibonacci bin or something else if you compile to js etc

Zig/*/zig-cache/
Zig/*/zig-out/
.idea/
Expand Down
18 changes: 18 additions & 0 deletions Nim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Nim Examples



todo





For setting up your environment and installing the necessary tools, see [the Enarx documentation on using WebAssembly with Nim](https://enarx.dev/docs/WebAssembly/Nim).

nim c -r -d:release fibonacci.nim

or

nimble run
nimble run -- 10
13 changes: 13 additions & 0 deletions Nim/fibonacci/fibonacci.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Package

version = "0.1.0"
author = "Profian"
description = "Fibonacci example."
license = "Apache-2.0"
srcDir = "src"
bin = @["fibonacci"]


# Dependencies

requires "nim >= 1.6.6"
32 changes: 32 additions & 0 deletions Nim/fibonacci/src/fibonacci.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import std/os, std/parseutils, std/rdstdin, std/strformat

proc fib(n: int): int =
case n:
of 0, 1:
n
else:
fib(n - 1) + fib(n - 2)

proc main =
var args = commandLineParams()

if args.len == 0:
var line: string

if not readLineFromStdin("Enter a non-negative number: ", line):
echo ("Failed to read line")
quit QuitFailure

args.add(line)

var index: int

for arg in args:
if arg.parseInt(index) == 0:
echo fmt("Failed to parse number: {arg}")
quit QuitFailure

echo (fmt"Fibonacci sequence number at index {index} is {fib(index)}")

when isMainModule:
main()