Skip to content

Instantly share code, notes, and snippets.

@wonderful-panda
wonderful-panda / example.ts
Created December 14, 2016 00:56
keyofやっぱり難しい
interface Base {
foo: { [key: string]: any };
bar: any;
baz: any;
}
interface Hoge {
hoge: string;
fuga: string;
}
@wonderful-panda
wonderful-panda / example.ts
Created December 12, 2016 04:20
Vue: コンポーネントのemitを型付けする(できてない)
// in vue.d.ts
declare class Vue {
$emit(event: string, ...args: any[]): this;
}
// in user code
interface Emit<T> {
<K extends keyof T>(event: K, payload: T[K]): any;
}
@wonderful-panda
wonderful-panda / keyofExample.ts
Last active November 30, 2016 04:25
keyof 難しい
interface Test {
foo: string;
bar: number;
}
// これはOK (Testそのものだけど)
const t1: { [K in keyof Test]: Test[K] } = {
foo: "",
bar: 0
};
@wonderful-panda
wonderful-panda / test.ts.ejs
Created November 25, 2016 03:52
Generate typescript code by template engine
import * as Electron from "electron";
export const vuexActions = {
<%
VuexActions.methods.forEach((m, index) => {
const paramListWithType = m.parameters.map(p => `, ${p.prefix}${p.name}${p.postfix}: ${p.typeName}`).join("");
const paramList = m.parameters.map(p => `, ${p.prefix}${p.name}`).join("");
const last = index == VuexActions.methods.length - 1;
-%>
<%- m.name %>(target: Electron.WebContents<%- paramListWithType -%>): void {
@wonderful-panda
wonderful-panda / render.ts
Created October 10, 2016 13:56
Vue2.0 renderタグを提供するコンポーネント
import * as Vue from "vue";
export default {
name: "Render",
functional: true,
props: {
method: { required: true, type: Function },
args: { type: Array }
},
render(h: typeof Vue.prototype.$createElement, context: Vue.RenderContext): Vue.VNode {
import functools
def kamekoopa(f):
@functools.wraps(f)
def call(*args, **argd):
# upd-sta H.Iwata セミコロンを削除
# print("完全にマスター");
print("完全にマスター")
# upd-end H.Iwata セミコロンを削除
@wonderful-panda
wonderful-panda / test.cs
Created September 28, 2015 15:51
Expression<Func<T, TRet>>に対する部分適用
void Main()
{
var x = 4;
var y = 2;
Expression<Func<int>> exp = () => x;
Expression<Func<int, int>> twice = n => n * y;
var param = twice.Parameters[0];
var exp2 = new Visitor(exp.Body, twice.Body).Visit(exp);
var exp3 = (Expression<Func<int>>)new Visitor(param, exp.Body).Visit(exp2);
var f = exp3.Compile();
@wonderful-panda
wonderful-panda / KeyComparer.cs
Last active August 29, 2015 14:24
複数のキーで比較を行うComparer
// OrderBy(x => x.Id).ThenBy(x => x.Name) みたいな比較をするComparer
// var comparer = KeyComparer<Foo>.CreateBy(x => x.Id).ThenBy(x => x.Name); みたいに使う
public class KeyComparer<T> : IComparer<T>
{
readonly List<Comparison<T>> comparisons_;
private KeyComparer(params Comparison<T> []comparisons)
{
comparisons_ = comparisons.ToList();
}
# coding: utf-8
import ast
class _MakeFunction(ast.NodeTransformer):
def __init__(self, name, args, body):
self.name = name
self.args = args
self.body = body
def run(self):
@wonderful-panda
wonderful-panda / MainWindow.xaml
Created January 29, 2015 15:00
[WPF]ListView.SelectedItemsとListViewItem.IsSelectedの挙動を確認するテストプログラム
<Window x:Class="WpfTraning.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfTraning"
Height="300" Width="400">
<Window.DataContext>
<my:ViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>