Giter VIP home page Giter VIP logo

shnarazk / learn-bqn Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 2.4 MB

配列指向プログラミング言語BQN の始め方(日本語版) -- Learn and write an introduction of BQN, an APL descendant (in Japanese)

Home Page: https://shnarazk.github.io/learn-bqn/

License: Creative Commons Zero v1.0 Universal

APL 94.13% Shell 0.09% Rust 0.94% CSS 0.42% Nix 4.42%
bqn learning-by-doing tutorial japanese

learn-bqn's Introduction

My setup scheme of CBQN with Nix

  • a crash course to install CBQN devel on Darnwin (checked at 2023-08-22)
nix shell nixpkgs#libffi^dev; export C_INCLUDE_PATH=/nix/store/*libffi-*-dev/include
# or flakify with `pkgs.libffi`
make o3n has='AVX2 BMI2'        # or make o3 on aarch64
cp BQN ~/.local/bin
make shared-o3
mkdir -p ~/.local/lib
cp libcbqn.so ~/.local/lib
cp libcbqn.so ~/.local/lib/BQN
ln -sf ~/.local/bin/BQN ~/.local/bin/cbqn

How to code

learn-bqn's People

Contributors

shnarazk avatar

Watchers

 avatar  avatar  avatar

learn-bqn's Issues

要素が逐次的に追加されるクソ長いリストをどう実現するか > 言語仕様を考慮したプログラミングの節が必要

https://github.com/shnarazk/advent-of-code/blob/0f7da33b50078b373133cc1380b2ff9ccd506bf1/bqn/2018/day14.bqn#L5-L15

以下の発言は全くの間違い。in-place updatingの条件を満たしていなかっただけ。
言い方を変えると引数のin-place updatingはできていない。あるいはwhileで引数を残しておく必要があるのだろうか。

F ← {𝕊 𝕩 :
  𝕩 function-using-𝕩 ↩   # ここがfunction-not-using-xでも同じ
  𝕩
}
F •_while_ P something

はin-place updatingを実現して いても要素数が1Mを超えてくると全く歯が立たない。 いない。
chunkingしろと言うのをAOC y2018 day14で学んだ。
F の引数にわたる値が名前空間ならうまくいく。あるいは引数として渡すのをやめる。

While the Low-stack version seems checking only ....

another quiz

Convert ⟨"abcd","efghi","jkl"⟩ to

┌─
╵ "abcd"
  "efghi"
  "jkl"
          ┘

without Reshape '⥊'

Coding style for HowToBQN-ja

見やすさにちょっと振ってみた方がいいかも

  • 関数適用以外の構文要素には両方に空白を置く

    • 代入文 -- a ← 1
    • header delimiterの:, ; -- {𝕊x : ÷x ; w𝕊x : w÷x}
    • 条件分岐の? -- {a ? 𝕨 ; 𝕩}
    • 文のdelimiterの, ⚠️ 文のdelimiterとしての, -- a ← 1 ⋄ b ← 2
  • brace系はopen/closeが同じものには外側に空白を置く、そうでないものには空白を置かない

    • 0{𝕨+𝕩}⟨⟨1,2⟩,⟨2,1⟩⟩ # {}にも⟨⟩にも置かない
    • "aba"∾"xx" # 結果として∾は孤立させられる、という方針はやめる
    • '固定長で出現するので例外扱いとする上記の原則でカバーされている
    • ⚠️ 文のdelimiterではない,
  • 関数名と𝕨(または𝕩)の間に空白が必要なら𝕩(または𝕨)の間にも空白を置く

  • 特別名と識別子との列が エディタ上で 一つの単語として扱われるなら間には必ず空白を置く (のがいいか例外なく置くのがいいか)

  • 文字列の特別ルール:二重引用符を除いた場合にトークンがつながってしまう場合には空白を置く。文字列の内容は識別子構成文字だとみなす。

    • 例1: Fun"string"Funstringが1トークンになってしまうのでFun "strign"
    • 例2: ⌽"string"は繋がらないのでこのまま空白は置かない
    • 例3: Fun"/string"/がないとしたらFunstringの1トークンになってしまうのでFun "/strign"
    • 例4: ⌽"¨"は置かない

ISSUES

  • ,の扱いが明示されていない。両方に空白を置かない理由がない。

Runtime of Under

Show (⊏˘) 33 ⥊ ↕9
Error: ⁼: Inverse not found
test.bqn:1:
  •Show ⌽⌾(⊏˘) 3‿3 ⥊ ↕9
        ^^^^^^

Question about `Select`

https://mlochbaum.github.io/BQN/doc/control.html#switch-case

REPL

Switch  {c⊑𝕩  ma<˘21𝕩  (⊑aC)◶m@}
value  "double"
v  33
Switchvalue
  "increment"  {𝕤 v+1}
  "decrement"  {𝕤 v-1}
  "double"     {𝕤 v×2}
  "halve"      {𝕤 v÷2}
⟩
𝕨⊑𝕩: Index out of range

Switch ← {c←⊑𝕩 ⋄ m‿a←<˘⍉∘‿2⥊1↓𝕩 ⋄ (⊑a⊐C)◶m@}
                                  ^^^^^^^^

Switch ⟨value
^^^^^^
Switch  {c⊑𝕩  ma<˘21𝕩  (⊑(m⊸⊐)<C)◶a@}

REPL

The base of relative path

directory structure for test

  • /private/tmp/sandbox
    • src/p.bqn (shown below)
    • data/day1.txt (contains "The elves are in day1.txt. ")
#!/usr/bin/env bqn
•Show "wdpath: "∾•wdpath
•Show "  path: "∾•path
•Show •file.Lines ⊑•args

reproduction steps

$ cd /private/tmp/sandbox/src

$ ./p.bqn ../data/day1.txt
"wdpath: /private/tmp/sandbox/src"
"  path: /private/tmp/sandbox/src"
⟨ "The elves are in day1.txt. " ⟩

$ cd /private/tmp/sandbox

$ src/p.bqn /private/tmp/sandbox/data/day1.txt
"wdpath: /private/tmp/sandbox"
"  path: /private/tmp/sandbox/src"
⟨ "The elves are in day1.txt. " ⟩

$ src/p.bqn data/day1.txt
"wdpath: /private/tmp/sandbox"
"  path: /private/tmp/sandbox/src"
ENOENT: no such file or directory, open '/private/tmp/sandbox/src/data/day1.txt'

•Show •file.Lines ⊑•args
            ^^^^^

# switch to CBQN
$ src/p.bqn data/day1.txt
"wdpath: /private/tmp/sandbox"
"  path: /private/tmp/sandbox/src"
Error: Couldn't read file "/private/tmp/sandbox/src/data/day1.txt"
src/p.bqn:4:
  •Show •file.Lines ⊑•args
              ^^^^^

solution

Build a valid path by yourself.

•wdpath∾ "/"∾ ⊑•args

Bellman Ford algorithm

fn BellmanFord(list vertices, list edges, vertex source) {
    // This implementation takes in a graph, represented as
    // lists of vertices (represented as integers [0..n-1]) and edges,
    // and fills two arrays (distance and predecessor), holding
    // the shortest path from the source to each vertex
    distance ← 0⌾⊑ n⥊∞
    predecessor ← n⥊¯∞

    // Relax edges repeatedly  
    for _ in 0..|V| {
        for (u, v): edge with weight w in edges {
            if distance[u] + w < distance[v] {
                distance[v] = distance[u] + w
                predecessor[v] = u
             }
         }
    }
    (distance, predecessor)

Benchmarking with pi

Another idea:

{𝕊 pipq: p 4+ q 4+pi+2÷p×q,p,q⟩}n 0¯3¯1

or

{𝕊 pipq: pq 4+pi+2÷×´pq,pq⟩}n 0‿(¯3¯1)

Porting APL Sudoku solver(s)

Sudoku{                                         Solution vector for square Sudoku puzzle ⍵.
    BoxNos   {/ *2}                     box numbers for a puzzle of size ⍵*2
    RowColBoxNos   {(),¨BoxNos*0.5}         row/column/box numbers
    ContentionMap  {[2] 1¨.=}             contention map for puzzle ⍵
    CMAP  ContentionMap RowColBoxNos          fixed contention map for current puzzle

    Available   {()~×CMAP}              list of available numbers at position ⍺ for puzzle ⍵
    EmptyCells  {(,=0)/,}                   row & column indices of empty cells
    Placements  {( Available )@()¨}      puzzle ⍵ with all available numbers at position ⍺
    AllPlacements  {,/Placements¨}          all possible Placements at ⍺ based on partial solutions ⍵
    Solutions  {AllPlacements/(EmptyCells ),⊂⊂}   solution vector

    Solutions 
}

Optimize y2016 day11

1st pass

# 1t1 ~/Repositories/learn-bqn/advent-of-code/2016@05-20T06:44:27 e927286@main*
37
# 1t1 ~/Repositories/learn-bqn/advent-of-code/2016@05-20T06:59:29 e927286@main*

2nd run: change loop condition

⟨ "cost" 0 0 ⟩
⟨ "cost" 1 4.76837158203125e¯07 ⟩
⟨ "cost" 2 5.960464477539062e¯06 ⟩
⟨ "cost" 3 2.598762512207031e¯05 ⟩
⟨ "cost" 4 8.511543273925781e¯05 ⟩
⟨ "cost" 5 0.00019073486328125 ⟩
⟨ "cost" 6 0.0004701614379882812 ⟩
⟨ "cost" 7 0.0009272098541259766 ⟩
⟨ "cost" 8 0.001952409744262695 ⟩
⟨ "cost" 9 0.003038644790649414 ⟩
⟨ "cost" 10 0.004402637481689453 ⟩
⟨ "cost" 11 0.006110906600952148 ⟩
⟨ "cost" 12 0.008239507675170898 ⟩
⟨ "cost" 13 0.01049399375915527 ⟩
⟨ "cost" 14 0.01288557052612305 ⟩
⟨ "cost" 15 0.01560568809509277 ⟩
⟨ "cost" 16 0.01863813400268555 ⟩
⟨ "cost" 17 0.0214385986328125 ⟩
⟨ "cost" 18 0.02423214912414551 ⟩
⟨ "cost" 19 0.02646493911743164 ⟩
⟨ "cost" 20 0.02860736846923828 ⟩
⟨ "cost" 21 0.03058600425720215 ⟩
⟨ "cost" 22 0.03217840194702148 ⟩
⟨ "cost" 23 0.0335853099822998 ⟩
⟨ "cost" 24 0.03453493118286133 ⟩
⟨ "cost" 25 0.0354468822479248 ⟩
⟨ "cost" 26 0.03598380088806152 ⟩
⟨ "cost" 27 0.03650736808776855 ⟩
⟨ "cost" 28 0.03678274154663086 ⟩
⟨ "cost" 29 0.03711628913879395 ⟩
⟨ "cost" 30 0.03728032112121582 ⟩
⟨ "cost" 31 0.03745079040527344 ⟩
⟨ "cost" 32 0.03750801086425781 ⟩
⟨ "cost" 33 0.03757834434509277 ⟩
⟨ "cost" 34 0.03761768341064453 ⟩
⟨ "cost" 35 0.03767013549804688 ⟩
⟨ "cost" 36 0.03768801689147949 ⟩
37
./day11.bqn  900.85s user 0.63s system 99% cpu 15:01.88 total

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.