Skip to content

Instantly share code, notes, and snippets.

View tos-kamiya's full-sized avatar
:octocat:
one by one

Toshihiro Kamiya tos-kamiya

:octocat:
one by one
View GitHub Profile
@tos-kamiya
tos-kamiya / gist:3284803
Created August 7, 2012 12:02
finally for C++0x
// reference: http://stackoverflow.com/questions/6167515/simulating-finally-block-in-c0x
#include <functional>
class finally
{
std::function<void (void)> const action;
finally(const finally&) = delete;
public:
@tos-kamiya
tos-kamiya / unittest_rps.rb
Created August 10, 2012 07:29
rps-game unittest
require 'test/unit'
require 'rps'
class RPSTestCase < Test::Unit::TestCase
def test_simple_win_lose
# :rock loses :paper
turn_person = rps_game([[:rock], [:paper]])
assert_equal(turn_person, [0, 1])
# :rock wins :scissors
@tos-kamiya
tos-kamiya / jojo.py
Created August 11, 2012 06:47
Jojo problem
# coding: utf-8
import sys
# For each integer 1..n, print "jojo" if it is a prime number,
# else print the number.
def jojo_iter(num):
if 1 <= num:
yield 1
@tos-kamiya
tos-kamiya / gist:3605079
Created September 2, 2012 22:11
Use GitHub Markdown API with curl
curl -u "GIT-ACCOUNT-NAME:PASS" --data '{"text":"# Hello","mode":"gfm" }' https://api.github.com/markdown
@tos-kamiya
tos-kamiya / jquery.snippet.js
Created September 18, 2012 06:20
add start line number feature to http://www.steamdev.com/snippet/
/*
* Snippet :: jQuery Syntax Highlighter v2.0.0
* http://steamdev.com/snippet
*
* Copyright 2011, SteamDev
* Released under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* Date: Wed Jan 19, 2011
*/
@tos-kamiya
tos-kamiya / n-tagram.html
Created October 14, 2012 13:09
HTML5 Canvas Drawing of Pentagram, Heptagram, ...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>n-tagram</title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
</head>
<body>
<label for="pointsSpinner">Points: </label>
@tos-kamiya
tos-kamiya / expand_zip_to_filename_directory.hs
Created November 28, 2012 01:21
expand a zip file's contents to a directory which has the same name to the zip file.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
import Control.Monad (forM_)
import qualified Data.Text.Lazy as LT (append, pack, Text)
import Filesystem.Path (append, basename, filename)
import Shelly
import System.Environment (getArgs)
default (LT.Text)
@tos-kamiya
tos-kamiya / Prettify.hs
Last active December 10, 2015 05:08
A pretty-printing for Haskell in Haskell. Almost same as http://code.haskell.org/haskell-src-exts/examples/Prettify.hs .
-- references
-- http://code.haskell.org/haskell-src-exts/Test/examples/HaskellParser.hs
-- http://code.haskell.org/haskell-src-exts/examples/Prettify.hs
import qualified Language.Haskell.Exts.Annotated as P
import qualified Language.Haskell.Exts.Extension as Ext
import Language.Haskell.Exts.Pretty (prettyPrint)
import System.Environment (getArgs)
parse originalFileName input = P.parseModuleWithMode parseMode input
@tos-kamiya
tos-kamiya / komachi2013chk.hs
Created January 1, 2013 18:55
Komachi-zan 2013. Run as follows: $ runghc komachi2013gen.hs | runghc komachi2013chk.hs
{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-}↲
import Control.Monad↲
import Text.Peggy↲
[peggy|↲
expr :: Double↲
= expr "+" fact { $1 + $2 }↲
/ expr "-" fact { $1 - $2 }↲
/ fact↲
import Data.List
import qualified Data.Set as S
import Data.String.Utils
genEquivalentClass :: Ord a => [(a, a)] -> [[a]]
genEquivalentClass pairs =
sort . map (sort . S.toList) $ foldr eqcs [] pairs
where
eqcs (n, m) cs = case (lookupClass n cs, lookupClass m cs) of
(Nothing, Nothing) -> S.fromList [n, m] : cs