Skip to content

Instantly share code, notes, and snippets.

View ngsw-taro's full-sized avatar

Taro Nagasawa ngsw-taro

View GitHub Profile
@ngsw-taro
ngsw-taro / gist:6365287
Created August 28, 2013 12:07
ClojureでSlideshareのAPIを叩く。まずはSHA-1求める関数を作る
(import 'java.security.MessageDigest)
(defn sha1 [s]
(let [md (MessageDigest/getInstance "SHA-1")]
(do
(. md update (. s getBytes))
(apply str (map #(format "%02x" %) (. md digest))))))
@ngsw-taro
ngsw-taro / Main.scala
Last active December 21, 2015 20:49
Scalaの練習。SlideshareのAPI叩いてみた
// 使用例
import com.taroid.slideshare4s.{SortOrder, SlideShare, Query}
object Main {
def main(args: Array[String]) {
val apiKey = System.getenv("SLIDESHARE_API_KEY")
val sharedSecret = System.getenv("SLIDESHARE_SHARED_SECRET")
val ss = SlideShare(apiKey, sharedSecret)
val query = Query(words = "kotlin", itemsPerPage = 10, language = "ja", sortOrder = SortOrder.LATEST)
@ngsw-taro
ngsw-taro / Slidepuzzle.scala
Created August 26, 2013 09:30
Scalaの練習
object Main {
def main(args: Array[String]) {
val puzzle = new SlidePuzzle(3, List(3, 1, 2, 4, 7, 5, 6, 0, 8), 0)
val solvedPuzzleHistory = SlidePuzzle.solve(puzzle)
assert(solvedPuzzleHistory(3).cells == List(3, 1, 2, 4, 7, 5, 6, 0, 8))
assert(solvedPuzzleHistory(2).cells == List(3, 1, 2, 4, 0, 5, 6, 7, 8))
assert(solvedPuzzleHistory(1).cells == List(3, 1, 2, 0, 4, 5, 6, 7, 8))
assert(solvedPuzzleHistory(0).cells == List(0, 1, 2, 3, 4, 5, 6, 7, 8))
}
@ngsw-taro
ngsw-taro / gist:6149089
Last active December 20, 2015 14:48
KotlinでDBアクセス
// 原始的でシンプルな方法
package com.taroid.sample
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement
import java.sql.Connection
fun main(args: Array<String>) {
@ngsw-taro
ngsw-taro / gist:5988944
Last active December 19, 2015 17:09
Clojureの練習
(defn index-to-pos [index width] [(int (/ index width)) (rem index width)])
(defn pos [{width :width cells :cells} cell] (index-to-pos (.indexOf cells cell) width))
(defn distance [[r1 c1] [r2 c2]] (+ (Math/abs (- r1 r2)) (Math/abs (- c1 c2))))
(defn win-distance [{width :width cells :cells :as board} cell]
(distance (pos board cell) (pos {:width width :cells (range 0 (count cells))} cell)))
(defn total-win-distance [{cells :cells :as board}]
@ngsw-taro
ngsw-taro / hit_and_blow.rb
Last active December 11, 2015 08:08
Rubyの学習のため、ヒット&ブローを作ってみた。 Rubyよくわからないので、指摘しまくってください。
# ヒット&ブローを表すクラス。
# このクラスのオブジェクトはイミュータブル。
class HitAndBlow
NUMBERS_LENGTH = 4
NUMBER_RANGE = (1..9)
# 指定された配列が、正解の数の形式として妥当か判定する。
# 数の重複がなく、所定の範囲ないの値を持ち、長さがNUMBERS_LENGTHの配列ならばtrueを、それ以外ならfalseを返す。
def self.valid_numbers? submitted_numbers
@ngsw-taro
ngsw-taro / divisor.kt
Created December 8, 2012 07:17 — forked from necoco/divisor.kt
宿題
fun divisor(val a: Int,val b: Int) : Int = if(b == 0) a else divisor(b, a % b)
fun main(args: Array<String>){
test(1, divisor(1, 1))
test(2, divisor(2, 4))
test(2, divisor(4, 2))
test(6, divisor(24, 18))
test(6, divisor(18, 24))
test(1, divisor(13, 19))
test(1, divisor(19, 13))
@ngsw-taro
ngsw-taro / MainActivity.java
Created December 6, 2012 11:09
AndroidAdventCalendar2012
// Android(サーバ側)
package com.taroid.advent;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
@ngsw-taro
ngsw-taro / gist:3930225
Created October 22, 2012 08:00
ImmutableCollection in Kotlin
// 不変コレクションを作る場合は ImmutableArrayListBuilderクラス を使おう
// このテストは失敗する
package com.taroid.kt.sample
import kotlin.test.assertEquals
import org.junit.Test as test
class ImmutableCollectionTest() {
@ngsw-taro
ngsw-taro / Prime.hs
Created August 26, 2012 01:26
はすけるで素数判定
module Prime(
prime
) where
prime :: Int -> Bool
prime x | x < 0 = error "the argument should be larger than zero."
| x == 1 = False
| x == 2 = True
| mod x 2 == 0 = False
| otherwise = all (\y -> mod x y /= 0 || x == y) (take (x - 3) [3, 5..])