Skip to content

Instantly share code, notes, and snippets.

View zaneli's full-sized avatar
🤔
...?

Shunsuke Otani zaneli

🤔
...?
View GitHub Profile
@zaneli
zaneli / URIParser.scala
Created September 8, 2017 15:32
Scala関西Summit 2017 LT
package com.zaneli.parser
import java.net.{URI, URISyntaxException, URLEncoder}
import scala.io.Codec
import scala.util.control.Exception
import scala.util.parsing.combinator.RegexParsers
object URIParser extends RegexParsers {
override val skipWhitespace = false
@zaneli
zaneli / Main.scala
Last active March 9, 2017 04:32
call `scalikejdbc.config.DBs.setup(dbName)` multiple times in another threads.
package com.zaneli
import scalikejdbc._
import scalikejdbc.config.DBs
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
object Main extends App {
@zaneli
zaneli / cat.rs
Last active January 15, 2017 07:58
Rust入門者向けハンズオン #2 https://rust.connpass.com/event/43893/
extern crate getopts;
use std::env;
use std::io;
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;
use getopts::Options;
fn main() {
@zaneli
zaneli / 2-1_hostname.hs
Created September 17, 2016 02:26
Haskell Day 2016 演習
#!/usr/bin/env stack
-- stack --install-ghc runghc --package turtle
import Turtle
main = hostname >>= echo
@zaneli
zaneli / p40.clj
Created March 14, 2015 13:30
「ClojureでNinety-Nine Lisp Problems(P40, 41)」ブログ用
(declare my-primes)
(defn my-goldbach
"Goldbach's conjecture."
[n]
(if (or (odd? n) (== n 2))
nil
(loop [ps (my-primes n)]
(let [[p' & ps'] ps, m (first (drop-while #(> n (+ % p')) ps))]
(if (and (not (nil? m)) (== (+ p' m) n))
@zaneli
zaneli / euler67.hs
Last active August 29, 2015 14:16
「HaskellでProject Euler(Problem 67~69)」ブログ用
main = do triangle <- readFile "triangle.txt"
print $ maximumPath $ map toInts $ reverse $ lines triangle
maximumPath :: (Ord a, Num a) => [[a]] -> a
maximumPath nss = head $ foldl1 (zipWith (+) . largerList) nss
toInts :: String -> [Int]
toInts line = map read $ words line
largerList :: Ord b => [b] -> [b]
@zaneli
zaneli / p37.clj
Created March 8, 2015 06:26
「ClojureでNinety-Nine Lisp Problems(P37~39)」ブログ用
(declare my-prime-factors-mult count-quot)
(defn my-totient-phi-improved
"Calculate Euler's totient function phi(m) (improved)."
[m]
(reduce * (map (fn [[p m]] (* (dec p) (Math/pow p (dec m)))) (my-prime-factors-mult m))))
(defn my-prime-factors-mult
"Determine the prime factors of a given positive integer (2)."
[n]
@zaneli
zaneli / p34.clj
Last active August 29, 2015 14:16
「ClojureでNinety-Nine Lisp Problems(P34~36)」ブログ用
(declare my-coprime? my-gcd)
(defn my-totient-phi
"Calculate Euler's totient function phi(m)."
[m]
(count (filter #(my-coprime? m %) (range 1 m))))
(defn my-coprime?
"Determine whether two positive integer numbers are coprime."
[n m]
@zaneli
zaneli / euler64.hs
Last active August 29, 2015 14:16
「HaskellでProject Euler(Problem 64~66)」ブログ用
import Data.List (unfoldr)
main = print $ length $ filter (odd . length . snd . sqrts) [1..10000]
sqrts :: Integer -> (Integer, [Integer])
sqrts n | m^2 == n = (n, [])
| otherwise = (m, unfoldr f $ (1, -m, Nothing))
where
m = truncate $ sqrt $ fromInteger n
f (x, y, Just z) | (x, y) == z = Nothing
@zaneli
zaneli / p31.clj
Created February 24, 2015 13:21
「ClojureでNinety-Nine Lisp Problems(P31~33)」ブログ用
(defn my-prime?
"Determine whether a given integer number is prime."
([n]
(cond
(<= n 1) false
(== n 2) true
(== (rem n 2) 0) false
(some #(== (rem n %) 0) (range 3 (inc (Math/sqrt n)) 2)) false
:else true)))