Skip to content

Instantly share code, notes, and snippets.

View petitviolet's full-sized avatar
🕶️
😃

petitviolet petitviolet

🕶️
😃
View GitHub Profile
@petitviolet
petitviolet / c024.hs
Last active October 3, 2015 12:26
Paiza_C024.hs
import Control.Monad
import Data.List.Split
type Pair = (Int, Int)
-- |
-- >>> set (0, 0) 1 2
-- (2,0)
set :: Pair -> Int -> Int -> Pair
set (n1, n2) 1 x = (x, n2)
@petitviolet
petitviolet / ActorTest.scala
Created October 4, 2015 10:47
Sample for Akka-Actor
import akka.actor.{Actor, Props, ActorSystem}
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.util.{Success,Failure}
import scala.concurrent.ExecutionContext.Implicits.global
case class Message(val msg: String)
case class Reply(val content: String)
@petitviolet
petitviolet / Factorial.scala
Created October 4, 2015 14:29
recursive fact
import scala.annotation._
object Factorial {
// @tailrec
def fact(n: Int) : BigInt = n match {
case n if n <= 1 => 1
case _ => n * fact(n - 1)
}
@tailrec
@petitviolet
petitviolet / result_free.hs
Last active October 22, 2015 12:06
Free Monad for switching side effects
import Control.Monad.Free
data Result s = Get s | Fail deriving Show
instance Functor Result where
fmap f (Get x) = Get (f x)
fmap f Fail = Fail
liftF' :: Functor f => f r -> Free f r
liftF' cmd = Free (fmap Pure cmd)
@petitviolet
petitviolet / implicit_test.scala
Last active November 5, 2015 09:20
time spent seeking object from List
import scala.App
import scala.collection.breakOut
object ImplicitTest extends App {
val TIME = 10000
case class Hoge(val id: Long, val name: String)
implicit class MappedList(val list: Seq[Hoge]) extends AnyVal {
def get(id: Long): Option[Hoge] = {
@petitviolet
petitviolet / view.scala
Created November 12, 2015 06:27
performance normal, view and breakOut
import scala.App
object View extends App {
def now = System.currentTimeMillis
val NUM = 1000000
var start = now
var end = now
println("Start not view")
@petitviolet
petitviolet / sparse_euclidean_distance.py
Created December 20, 2012 11:52
2つの疎行列(1*N)のユークリッド距離を求める関数
# -*- encoding: utf-8 -*-
import scipy.spatial.distance as dis
import scipy.sparse as sp
import numpy as np, scipy.io as io
import math
def sparse_distance(v1, v2):
"""1*Nのベクトル間のユークリッド距離を求める
args:
@petitviolet
petitviolet / DuckTyping.scala
Created December 17, 2015 08:48
DuckTyping in Scala and Python
import scala.language.{postfixOps, reflectiveCalls}
// https://twitter.github.io/scala_school/advanced-types.html#structural
// http://twitter.github.io/effectivescala/#Object%20oriented%20programming-Structural%20typing
object DuckTyping extends App {
sealed abstract class AwesomeData(id: Int)
case class Nice(id: Int) extends AwesomeData(id) {
def size: Int = id * 100
}
@petitviolet
petitviolet / search.py
Last active December 17, 2015 13:49
YahooApiを叩いた検索エンジン。上位20件のみを返すだけの簡単なもの。Pythonを.pyファイルのまま動かすのが難しかった...
#!/usr/local/bin/python
# -*- encoding:utf-8 -*-
'''YahooApiを用いた検索エンジン
'''
from urllib import quote
from urllib2 import build_opener
import cgi
from split_result import split_result
import cgitb
cgitb.enable()
@petitviolet
petitviolet / split_result.py
Last active December 17, 2015 14:28
search.pyで利用する、YahooApiから返って来たxmlから検索結果1件ごとのタイトルとurlとサマリーを抽出する。 タイトルとurlは改行と空白文字を許さず、サマリーはそのまま取得する。
# -*- encoding:utf-8 -*-
import re
result_pattern = '<Result>(.+?)</Result>'
link_pattern = '<Url>(.*?)</Url>'
title_pattern = '<Title>(.*?)</Title>'
summary_pattern = '<Summary>(.*?)</Summary>'
strip_pattern = r'\\n|\s'
catch_result = re.compile(result_pattern, re.S)
catch_link = re.compile(link_pattern, re.S)
catch_title = re.compile(title_pattern, re.S)