Skip to content

Instantly share code, notes, and snippets.

@plaster
plaster / convtest.hs
Created February 17, 2013 15:18
ISO-2022-JP な行と UTF-8 な行の混ざったファイルを読んでみるテスト
import Codec.Text.IConv as IConv
import Codec.Binary.UTF8.String as UTF8
import Data.ByteString as B
import Data.ByteString.Lazy as BL
import System.IO as IO
convertDetect :: [ IConv.EncodingName ] -> IConv.EncodingName -> BL.ByteString ->
Either BL.ByteString (IConv.EncodingName, BL.ByteString)
convertDetect [] _ src = Left src
convertDetect (srcEnc:srcEncs) dstEnc src = case IConv.convertStrictly srcEnc dstEnc src of
@plaster
plaster / LibZip'.hs
Last active December 14, 2015 12:09
LibZip を ByteString と String に両対応させようとしてコンパイルが通らない何か
{- | Polymorphic version of @Codec.Archive.LibZip@.
Codec.Archive.Libzip is with useful and simple API, and a high-leveled library to deal with zip archives.
...Although, in the library, specification way of the path to a file inside a zip archive is `String`.
This limitation is inconvenient especially when dealing with zip archived file
of which path contains foreign system's multi-byte characters.
This module provides APIs of polymorphic version,
which can specify and receive archived file's path as String or Data.ByteString (as you like).
#! /usr/bin/env python
import sys
def zigzag(sig, steps):
while True:
yield (sig, steps)
sig *= -1
steps += 1
def locus(x, motion):
i = 0
@plaster
plaster / gcj2013-r1a-b-small.scm
Last active December 16, 2015 23:49
Google Code Jam 2013 Round 1A Problem B solution core
(use srfi-42)
(define (dfs E0 E R vs)
(match vs
[ () 0]
[ (v . vs)
(max-ec (:range e 0 (+ E 1))
(+ (* v e)
(dfs E0
(min E0 (+ E R (- e)))
--- a/src/mzscheme/src/bool.c Sat Jun 01 00:04:45 2013 +0900
+++ b/src/mzscheme/src/bool.c Sat Jun 01 00:12:44 2013 +0900
@@ -124,6 +124,9 @@
return SAME_OBJ(obj1, obj2);
}
+#ifdef MZ_XFORM
+START_XFORM_SKIP;
+#endif
XFORM_NONGCING static MZ_INLINE int double_eqv(double a, double b)
@plaster
plaster / command
Last active December 17, 2015 23:29
precompでparentの入るinternal defineの例
% gosh -V
Gauche scheme shell, version 0.9.3 [utf-8,pthreads], x86_64-unknown-linux-gnu
% gosh precomp -e -P -o even--odd even-odd.scm
@plaster
plaster / fizzbuzz.hs
Created June 15, 2013 17:58
Eitherの練習。リスト脳。
type FizzBuzz = Either Int String
hit :: String -> FizzBuzz -> FizzBuzz
hit s (Left _) = Right s
hit s (Right s') = Right $ s ++ s'
fizz :: [ FizzBuzz -> FizzBuzz ]
fizz = cycle [ id, id, hit "fizz" ]
buzz :: [ FizzBuzz -> FizzBuzz ]
buzz = cycle [ id, id, id, id, hit "buzz" ]
fizzbuzz :: [ FizzBuzz -> FizzBuzz ]
@plaster
plaster / hello-world.scm
Last active December 22, 2015 09:29
Hello, world! without {number,character,string}-literals
#!/usr/bin/env gosh
(for-each (.$ display ucs->char length)
'#0=(#1=(#0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0# .
#2=(#0##0##0##0##0##0##0##0##0##0##0# .
#3=(#0# . #4=(#0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0# . #0#))))
#5=(#0# . #6=(#0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0##0# . #1#))
#7=(#0##0##0##0##0##0##0# . #5#)
#7##8=(#0##0##0# . #7#)
#2##4##9=(#0##0##0##0##0# . #10=(#0##0##0# . #8#)) #8##10##7##6##3#))
use utf8;
use strict;
use IO::File;
use IPC::Open2;
my $ofh = IO::File->new_tmpfile;
for (my $i = 1; $i < 20; ++$i) {
print $ofh ('x' x $i), "\n";
}
use utf8;
use strict;
use IO::File;
use IPC::Open2;
my $ifh0 = IO::Handle->new;
my $ifh1 = IO::File->new_tmpfile;
my $ofh0 = IO::Handle->new;
my $pid0 = open2($ifh0, $ofh0, qw(cat -n));