Skip to content

Instantly share code, notes, and snippets.

View sasaki-shigeo's full-sized avatar

SASAKI Shigeo sasaki-shigeo

View GitHub Profile
@sasaki-shigeo
sasaki-shigeo / parse-xml.scala
Created January 30, 2014 01:23
incomplete XML parser / Scala の parser combinator で XML の構文解析器を作ったが,まだ不完全
import scala.util.parsing.combinator._
abstract class XML
case class Element(name: String, attributes: Map[String, String], contents: Seq[XML]) extends XML
case class Letter(c: Char) extends XML
case class CDATA(text: String) extends XML
case class CharEntity(c: Char) extends XML
case class Entity(c: Char) extends XML
case class Comment(text: String) extends XML
@sasaki-shigeo
sasaki-shigeo / gist:3727427
Created September 15, 2012 11:35
Numeric Constants in Java (Java の数値定数)
import static java.lang.Math.*;
Math.PI // 円周率
Math.E // 自然対数の底
Integer.MAX_VALUE // int の最大値
Integer.MIN_VALUE // int の最小値
Long.MAX_VALUE // long の最大値
Long.MIN_VALUE // long の最小値
Byte.MAX_VALUE // byte の最大値
@sasaki-shigeo
sasaki-shigeo / gist:3743553
Created September 18, 2012 14:52
Numeric Constants and Literals in Ruby (Ruby の数値定数および表記)
# Ruby の整数は Bignum なので,上限,下限は無い。
# 数学定数
Math::PI # 円周率
Math::E       # 自然対数の底 e
# Float は,多くの実装で IEEE 754 double precision floating point number が使われる
Float.MAX # 最大値
@sasaki-shigeo
sasaki-shigeo / gist:3744549
Created September 18, 2012 17:40
Examples of Ruby Collections (Ruby のコレクションの使用例)
#
# 配列
#
xs = Array.new
xs = [1,2,4,8,16,32]
xs(0)    # => 1 ; 先頭の成分
xs.last # => 32 ; 最後の成分
xs[1..-1] # => [2,4,8,16,32] ; 先頭を除いた部分列
@sasaki-shigeo
sasaki-shigeo / gist:3744803
Created September 18, 2012 18:19
Iterative Square Method in Scheme Language (Scheme 言語による繰り返し自乗法)
;;;
;;; Algorithm: iterative square method
;;;
;;; Basic Idea: a^(2*n) = (a^2)^n (mod m)
;;; e.g. a^4 = (a^2)^2, a^8 = ((a^2)^2)^2, a^16 = (((a^2)^2)^2)^2
;;;
;;; Haskell like code that scans the exponential from LSB to MSB
;;; pow-mod a 1 m = a mod m
;;; pow-mod a 2*n m = powMod (a^2 mod m) n m
;;; pow-mod a 2*n+1 m = (powMod (a^2 mod m) n m) * a mod m
@sasaki-shigeo
sasaki-shigeo / gist:3750773
Created September 19, 2012 16:57
Example code of Scala Collections (Scala のコレクション・クラスの使用例)
//
// Scala の Array の実体は Java の配列
// - 各成分は mutable
// - 要素数の動的変更はできない
// - 必要に応じて implicite conversion で WrappedArray に変換されるため
// Seq (or IndexedSeq ≒ Vector) と同じように扱うことができる
//
var xs = Array(1,2,4,8,16,32) // 6個の成分を列挙した配列を作る
xs(3) // 丸カッコで index を与える
@sasaki-shigeo
sasaki-shigeo / gist:3844088
Created October 6, 2012 05:41
Numeric Constants and Convert Functions in Scheme
;;;
;;; 無限大と NaN
;;;
(define negative-infinity (log 0.0))
(define positive-infinity (- negative-infinity))
(define NaN (+ positive-infinity negative-infinity))
;;;
;;; 数値の変換
;;;
@sasaki-shigeo
sasaki-shigeo / end-of-file.scm
Last active December 16, 2015 01:08
How to get EOF object in Scheme (Scheme で EOF オブジェクトを得る方法)
(cond-expand
(guile (use-modules (srfi srfi-6)))
(gauche (use srfi-6))
(plt (require (lib "6.ss" "srfi")))
(srfi-6 #t))
;;; SRFI-6 provides open-input-string
(define *eof*
(let ((port (open-input-string "")))
(read port)))
@sasaki-shigeo
sasaki-shigeo / fizzbuzz.rb
Last active December 20, 2015 04:29
Fizz Buzz in Ruby (example code on if/case and for/iterators)
#
for n in 1..100
if n % 15 == 0
puts 'fizzbuzz'
elsif n % 3 == 0
puts 'fizz'
elsif n % 5 == 0
puts 'buzz'
else
class StopWatch {
public static void main(String[] args)
throws InterruptedException // thrown by Thread.sleep()
{
for (int h = 0; h < 24; h++) {
for (int m = 0; m < 60; m++) {
for (int s = 0; s < 60; s++) {
System.out.printf("%2d:%02d:%02d\n", h, m, s);
Thread.sleep(1000); // 1000 ms = 1.0 sec
}