Skip to content

Commit 2fe0756

Browse files
authored
Port before_v0.60/fun folder (issue #221) (#835)
This PR is part of porting all old scripts #221 and ports `fun` folder <details><summary>Summary</summary> ### star.nu ```yaml from: before_v0.60/fun/star.nu to: modules/fun/star.nu ``` ### spark.nu The script has already been ported. I've removed old version. ```yaml from: before_v0.60/fun/spark.nu to: sourced/fun/spark.nu functions: spark: sourced/fun/spark.nu:1:spark ``` ### life.nu ```yaml from: before_v0.60/fun/life.nu to: modules/fun/life.nu ``` ### lisp_mode.nu There is a problem with this module because `-` is not allowed in names anymore, however `def "-"` is a valid syntax, but you will be unable to call this function. ```yaml from: before_v0.60/fun/lisp_mode.nu to: sourced/fun/lisp_mode.nu ``` ### nyancat.nu I also fixed animation frames from https://github.com/klange/nyancat ```yaml from: before_v0.60/fun/nyancat.nu to: modules/fun/nyancat.nu ``` </details>
1 parent b40ead9 commit 2fe0756

File tree

7 files changed

+194
-236
lines changed

7 files changed

+194
-236
lines changed

before_v0.60/fun/life.nu

-82
This file was deleted.

before_v0.60/fun/lisp_mode.nu

-35
This file was deleted.

before_v0.60/fun/spark.nu

-23
This file was deleted.

modules/fun/life.nu

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
def alive [x_pos: int, y_pos: int, $grid] {
2+
let width = ($grid | get width)
3+
let height = ($grid | get height)
4+
let data = ($grid | get data)
5+
6+
let $left_x = (if $x_pos == 0 { $width - 1} else { $x_pos - 1})
7+
let $right_x = (if $x_pos == ($width - 1) { 0 } else { $x_pos + 1})
8+
let $up_y = (if $y_pos == 0 { $height - 1} else { $y_pos - 1})
9+
let $down_y = (if $y_pos == ($height - 1) { 0 } else { $y_pos + 1})
10+
let $n = ($data | get ($x_pos + $up_y * $width) | into int)
11+
let $nw = ($data | get ($left_x + $up_y * $width) | into int)
12+
let $w = ($data | get ($left_x + $y_pos * $width) | into int)
13+
let $sw = ($data | get ($left_x + $down_y * $width) | into int)
14+
let $s = ($data | get ($x_pos + $down_y * $width) | into int)
15+
let $se = ($data | get ($right_x + $down_y * $width) | into int)
16+
let $e = ($data | get ($right_x + $y_pos * $width) | into int)
17+
let $ne = ($data | get ($right_x + $up_y * $width) | into int)
18+
19+
let total = $n + $nw + $w + $sw + $s + $se + $e + $ne
20+
21+
let $curr = ($data | get ($x_pos + $y_pos * $width))
22+
23+
if ($total == 3) or ($total == 2 and $curr) {
24+
true
25+
} else {
26+
false
27+
}
28+
}
29+
30+
def generation [$grid] {
31+
let width = ($grid | get width)
32+
let height = ($grid | get height)
33+
let data = ($grid | get data)
34+
35+
let next_generation = (
36+
0..<$height | each {|y|
37+
0..<$width | each {|x|
38+
alive $x $y $grid
39+
}
40+
}
41+
) | flatten
42+
{ width: $width, height : $height, data: $next_generation }
43+
}
44+
45+
def print-grid [$grid] {
46+
$grid.data | flatten | group ($grid | get width) | each {|x|
47+
$x | each {|item|
48+
if $item {
49+
"*"
50+
} else {
51+
"."
52+
}
53+
} | append (char nl) | str join
54+
} | str join ""
55+
}
56+
57+
def main [] {
58+
let width = 15
59+
let height = 15
60+
61+
let data = (0..<($width * $height) | each {
62+
random bool
63+
})
64+
65+
let grid = { width: $width, height : $height, data: $data }
66+
67+
print (print-grid $grid)
68+
69+
1..100 | reduce --fold ($grid) {|it acc|
70+
let next_grid = (generation $acc)
71+
print $"(char -u '1b')[2J" (print-grid $next_grid)
72+
$next_grid
73+
} | ignore
74+
}
75+

0 commit comments

Comments
 (0)