Skip to content

Instantly share code, notes, and snippets.

@nfunato
nfunato / robots.forth
Last active April 13, 2024 18:35
Robot game in FORTH
anew =============================robots=============================
\ refactored the following a bit
\ https://gist.github.com/nfunato/39490e1e5d41a9a2d8b0f614a46feeea#file-02a_robots-fs
\ -------------------------------------------------------------------
\ General utils
: not ( v -- f ) 0= ;
: 3dup ( a b c -- a b c a b c ) dup 2over rot ;
  • 01_robots.lisp
    古典的Robot Game.
    本(Land of Lisp)にあるオリジナル(http://landoflisp.com/robots.lisp) は、
    わずか42行の圧縮気味のコードなのを、読みやすく refactorしたもの.

  • 02a_robots.fs, 02b_prelude.fs
    上記 01_robots.lisp を GForthに移植したもの.

  • 04_robots.go

;;; anaphoras and the like
(defmacro aif (tst thn &optional els) `(let ((it ,tst)) (if it ,thn ,els)))
(defmacro awhen (tst &body body) `(let ((it ,tst)) (when it ,@body)))
(defmacro aprog1 (f . fs) `(let ((it ,f)) ,@fs it))
(defmacro acond (&rest cls)
(if (null cls) nil
(let ((cl1 (car cls)) (sym (gensym)))
`(let ((,sym ,(car cl1)))
;; based on https://marui.hatenablog.com/entry/2022/12/13/222030
(defun mapfn-into (arr fn &rest args)
(loop for i below (array-total-size arr)
do (setf (row-major-aref arr i) (apply fn args))
finally (return arr)))
(defun box-muller-random-sampling ()
"Return a pair of independent, standard, normally distributed
(zero expectation, unit variance) random number, using a random
------------------------------------------------------------------
以下の翻訳文は、Felix Winkelmann の Thoughts on Forth Programming
(http://call-with-current-continuation.org/articles/forth.txt) の
翻訳であり、原著者の許可を得て公開するものです。
2021-09-01 Nobuhiko FUNATO (nfunato @ acm . org)
更新履歴:
2021-09-02(rev10): Shiro Kawaiさんにご指摘いただいた誤訳訂正/改善を反映
2021-09-02(rev09): 公開初版
------------------------------------------------------------------
;;; dunm.scm
;;; - cwd傘下のnode_modulesディレクトリのdisk usageを調べる
;;; - usageに基づいて、(確認後に)消せるようにする
;;; (report-stats [#f])
;;; (report-stats #t)
;;; 傘下のnode_modulesディレクトリのdisk usageをプリントする。
;;; arg1が#fのときは、1ヶ月以上アクセスしていないもののみを対象とする。
;;; 結果の情報は、変数 *result* に格納されている。
@nfunato
nfunato / fizzbuzz.fs
Last active July 2, 2022 10:59
お嬢FORTH on top of GFORTH
\ FizzBuzz in OJ-FORTH ( on top of GFORTH )
include oj-forth.fs
\ FORTHには、ほぼ文法が無い(ワードしかない)ということがよく分かると思う
\ なお以下の例で使っているワード 「を」 「まで」 「から」 は、純粋に飾り(NOP)である
わたくし で割り切れるの ( i n -- b ) を
mod 0=
と定義しましてよ
@nfunato
nfunato / fizzbuzz.fth
Last active June 24, 2022 23:09
FizzBuzz in gforth (for code golf)
\ FizzBuzz in Forth by @nfunato on 2015-04-30
\ http://golf.shinh.org/p.rb?FizzBuzz#Forth
\ Execution rule:
\ 1. invoke shell
\ 2. exec gforth this-file -e bye
\ : x
\ 101 1 do
\ 1
@nfunato
nfunato / ColorProvider.tsx
Last active June 14, 2022 00:28
about typing react18 context object in typescript
import React, { createContext, useState, FC } from "react";
import uuidv4 from "uuidv4";
import { getInitialColorDataList } from "./ColorDataList";
export type ColorDataTyp = {
id: string;
title: string;
color: string;
rating: number;
};

Revision History

このgist entryには、 ここ(zenn) にある記事の参照用コードが含まれています。

内容