Skip to content

Instantly share code, notes, and snippets.

type UserModel struct {
}
// UserModel をレシーバとして関数を定義
func (d *UserModel) CreateUser(user UserInformation) {
// DBAccessor を直接インスタンス化
dba := DBAccessor.New()
dba.InsertUser(user.User)
dba.InsertUserEmail(user.Email)
}
type mockDBAccessor struct {
}
func (m mockDBAccessor) InsertUser(user User) {
}
func (m mockDBAccessor) InsertUserEmail(email UserEmail) {
}
func TestCreateUser(t *testing.T) {
// UserModel が依存するクラスの *インターフェース* を全て指定
type UserModel struct {
dba DBAccessorIface
}
// UserModel をレシーバとして関数を定義
func (d *UserModel) CreateUser(user UserInformation) {
d.dba.InsertUser(user.User)
d.dba.InsertUserEmail(user.Email)
}
expect_any_instance_of(DBAccessor).to receive(:insert_user).and_return("Success")
expect_any_instance_of(DBAccessor).to receive(:insert_user_email).and_return("Success")
UserModel.new.create_user(user_information)
class UserModel
def create_user(user_information)
dba = DBAccessor.new
dba.insert_user(user_information.user)
dba.insert_user_email(user_information.email)
end
end
use std::io;
fn solve(line: &String, s: usize, t: usize) -> u32 {
if line.as_bytes()[s] != b'[' {
return line[s..t].parse::<u32>().unwrap() / 2 + 1
}
let mut children = Vec::new();
let mut i = s;
while i < t {
let ns = i + 1;
@na-o-ys
na-o-ys / co_contravariance.hs
Last active February 14, 2017 09:50
About covariance / contravariance in Haskell
{-# LANGUAGE ExistentialQuantification #-}
-- C1 :> C2 :> C3
class C1 a where
f1 :: a -> String
class (C1 a) => C2 a where
f2 :: a -> String
class (C2 a) => C3 a where
f3 :: a -> String
data D1 = D1 -- instance of C1
@na-o-ys
na-o-ys / type_level_fizzbuzz.ts
Last active July 24, 2018 10:18
A type level FizzBuzz in TypeScript.
type Zero = 0
type Num = Zero | { 0: Num }
const any: any = null
function succ<T>(v: T): { 0: T } {
return any
}
type FizzNum = Zero | {0:{0:{0: FizzNum }}}
@na-o-ys
na-o-ys / type_level_xor.ts
Created February 9, 2017 09:06
A type level (compile time) NAND and XOR evaluation in TypeScript
type T = 1
type F = 0
function nand(v1: T, v2: T): F;
function nand(v1: T, v2: F): T;
function nand(v1: F, v2: T): T;
function nand(v1: F, v2: F): T;
function nand(v1, v2) {
return 0 // dummy
}
@na-o-ys
na-o-ys / src.ts
Last active November 29, 2016 00:31
async/await と generator
// compile: tsc src.ts --lib es2017,dom -t es2015
// 1. Async/Await
async function concatPromises(): Promise<string> {
const v0 = getValue()
const v1 = await getPromise1()
const v2 = await getPromise2()
return v0 + v1 + v2
}
concatPromises().then(console.log) //=> Value: helloworld