みたいなのが挙げられます。これが話題になった時にSNSで見かける言説が「十進小数 (decimal) 型ならこういう問題はない」です。 ですが、decimal型は十進小数を正確に表現できるという話でしかなく、全ての実数を正確に表現できるわけではありません。例えば、 1.0 / 3.0 * 3.0 の計算を考えてみましょう。数学的には、これはちょうど 1.0 になるはずです。 C#の場合 C#には標準の decimal 型があります。これで 1.0 / 3.0 * 3.0 を計算してみましょう。
ジェネリクスとは「抽象化されたデータ型」を表現する機能です。 同じ様な関数だが、型違いの場合に、引数である型を抽象的に使う時に便利な機能です。 ジェネリクスを使わない例では、 let getStringArray = (value: string): string[] => { return [value, value, value]; }; let getNumberArray = (value: number): number[] => { return [value, value, value]; }; 冗長ですよね。 関数の受け取る引数が違うだけなのに、複数の関数を定義している例です。 だめな解決策としては、型の部分をany型にするという解決策がありますが、それではTypeScriptで静的型付けをしている意味が全くありません。 そこで登場するのが、 ジェネリクス という機能です。
概要 去年の8月に社会人エンジニアになり、今年の2月までAPIサーバーの開発をRuby on Railsで行なっていた新卒Rubyistです。 3月にサーバーサイドを全てGolangで行う会社に転職しました。今までRailsでの業務が多かったため、interface{}という概念がどうしても身につきませんでしたが、ある程度業務に慣れ、戦い続けて学んだことをまとめようと思いました。 Golangのinterfaceについては知見は多くあるのですが、今年はアウトプットを大事にしていきたいという目標から、記事にまとめました。 下に読む前に Golangのインタフェースは型の1つです。 string や int64 が関数を持っているように、インタフェースで型を宣言してその型に関数を持たせることができます。 構造体がインタフェースで宣言されているGetRadius関数を持つと、この構造体の型はCi
var c complex128 = complex(1, 2) // 複素数に変換 var fr float64 = real(c) // 実数に戻す var fi float64 = imag(c) // 実数に戻す fmt.Println(fr, fi) //表示[1 2] 関数みたいに書くと ・実数→複素数 complex(real, imag float)complex ・複素数→実数 real(c complex)float imag(c complex)float といった感じです (float32,64はそれぞれcomplex64,128に対応します) 標準パッケージ https://golang.org/pkg/math/cmplx/ import "math/cmplx" これで標準パッケージが使えます。 mathパッケージにもあるような便利な関数が入っています。 cm
JavaScriptでも静的な型が求められるようになりTypeScriptやFlowが使われるようになってきました。 しかし、それらが無かったころのJavaScriptのコードやトランスパイラを使わずに開発している人はいてると思います。 この記事はコードの変更なしでJSDocのみで型検査をする方法を紹介します。 TL;DR JSDocの型定義で型チェックをする TypeScriptのallowJsとcheckJsを使う 必要に応じてd.tsをインストールする JSDocの型定義 TypeScriptやFlowが登場するより前からJSDocは存在します。JavadocやPHPDocのJavaScript版のようなものです。 そのJSDocは型定義が書けるようになっています。 エディタによってはJSDocの型定義で型のチェックを行ってくれることもあります。 JSDocについては@use JSD
アノテーションを実際に行っているのは以下の部分になります。 name: str: 引数nameが、str型であることをアノテート -> str: 関数greetingの返り値の型がstrであることをアノテート また、Type Hintsでは変数宣言における型コメントについても言及されています。 こちらは構文ではなく本当にコメントの拡張になりますが、現在既にこうした型に関するコメントを付けているのであれば、上記の記法に乗っ取っておけば将来的に何かしらのツールで型チェックを行えるようになる可能性があります。 これがPythonに導入された、型のある世界・・・になります。 なお、付与されたアノテーションは、実行時にはチェックされません。端的に言えばコメントの延長となります。 そのため強制力はありませんが、実行時に何もしないためパフォーマンスに影響を与えることもありません。 よって原則的には静的解
0は性別に関する情報が得られない場合に使います。性別に関する情報はあるのだけど1とも2とも言えない場合は9を使います。要は「0でもなくて1でも2でもなければ9」です。 これを知っていればMだとかFだとかを議論をせずに済みますね。 国際規格に従うべき理由 国際規格に従うことは色々と利点があります。まず、どうしてそういうコード体系にしたのかを説明しやすいです。また多言語対応する際も規格通りに書けば伝わるはずなので迷わずに済みます。別システムへのデータの移行や、異なるシステム間でのデータの統合もコード体系が同じならラクラクです。もしかしたら別のプロジェクトで書いたコードをそのまま使いまわせるかもしれません。技術者に対するトレーニングも不要です。 対して、わざわざ国際規格に反する実装をする場合は上記のメリットがそのままひっくり返ってデメリットになりはしますが、もちろん、それなりの理由があれば規格と
function isInteger(x) { return Math.round(x) === x; } > isInteger(1) true > isInteger(0) true > isInteger(1.111) false > isInteger("1") false > isInteger("") false > isInteger(null) false > isInteger(NaN) false > isInteger(undefined) false Register as a new user and use Qiita more conveniently You get articles that match your needsYou can efficiently read back useful informationYou can use dark th
Ruby on Rails 5.2.0.beta2が出て久しい。 僕はRails大好きっ子なので 社内の新規事業や、社外のお手伝いしているベンチャー企業では もちろん、Rails5.2を使って開発をしている。 すでに、新しいRailsに関する記事はいくつか挙がっているが 個人的に、一番インパクトが大きかったのはActiveModel::Attributesが導入されたことである。 待望のActiveModel::Attributes ActiveModel::Attributesでなにが変わったの?というと、 いままでActiveRecordでしか使えなかった一部の機能が、ActiveModelでも使えるようになっただけである。 いや、しかし! 今までのActiveModelの最大の弱点は まさにこの ActiveModel::Attributes が無かったことだと思う。 それが、Rai
declare -a array=( Java Ruby Python ) echo ${array[0]} # => Java echo ${#array[*]} #=> 3(配列の要素数) for e in ${array[*]} do echo $e # => Java, Ruby, Pythonの順に出力 done for i in ${!array{*}} do echo ${array[i]} # => Java, Ruby, Pythonの順位出力 done declare -A hash=([Jack]=11 [Queen]=12 [King]=13) echo ${hash['King']} # => 13 for v in ${hash[*]} do echo $v # => Jack, Queen, King(順序は任意) done for k in ${!hash[
TypeScriptのissueでたまたまDiff型に対する議論を発見したので現段階での表現方法を紹介します。 Add support for literal type subtraction · Issue #12215 · Microsoft/TypeScript 以下ではissueで紹介されている型の名前と定義を若干変えて紹介します。 Diff型とは何か Diff型とはAとBの型の差分を推論する型です。FlowではUtility Typesとして提供されています。 $Diff - Utility Types | Flow type A = { a: number, b: string } type B = { a: number } type T = $Diff<A, B> // { b: string } & { a?: number } type DiffKey<T extend
let str = "x\\yz"; let t = str.replace(/\\(.)/, (m, s) => s.toUpperCase()); // s は any 型なので、仮に s.toUpper() と書いてもエラーにならない TypeScript は静的型がウリの言語なので、もうちょっとやりようはあるのではないか。 目標:型付き正規表現 これらの問題を解決するために、キャプチャーの型の情報を埋め込んだ型 TypedRegExp<captures> を作りたい。キャプチャーの型とは、具体的には string: 通常のキャプチャー string | undefined: 後ろに ? や * がついたもの、あるいは | の一方に含まれるもの undefined: 否定先読み (?! .. ) に含まれるキャプチャー のいずれかである。例えば、 /(a)(b)?/ という正規表現の
この記事はLisp Advent Calendar 2016の24日目の記事です。 年末に入りとうとうクリスマスイブですが皆様いかがお過ごしでしょうか。 Common Lispは動的型付けの言語ですが、最適化のための型宣言機構が規格に組み込まれています。 また、SBCLやCMUCLでは、この型宣言を利用し、型エラーをコンパイル時に指摘する機能がついています。 型宣言と最適化の関係については、 最適化 - Common LISP users jp Cより高速なCommon Lispコードを書く Common Lispの最適化に挑戦してみる これらの記事によくまとまっています。 この記事は、実際にどのような記法で型宣言をするのかについて、学習のためにまとめたものになります。 type specifier 型は type specifier (typespec) という方式で記述されます。 ty
function foo(str) { console.assert(typeof str === 'string'); console.assert(str.length > 0); // 何かの処理 str.charAt(0); } assertを使って事前条件を書いて開発し、unassertを使ってプロダクションにリリースするときに削除スタイルは最近良く見かけるようになりましたね。 動的型付けなJavaScriptで安全なコードを書けるようになるassert最高!!と言いたいところですが、実行時にしかチェックができないんですよね。 そこで、せっかくflowtypeで静的な型付けを行っているので、ここは事前にチェックできるように型で解決を行いたいと思います。 1. 型アノテーションを付与する まずは先のコードに型アノテーションを付与して一つ目のassertを削除します。 functio
こういうコンパイルエラーにぶち当たった。 Interfaceだとコンパイルエラーになったのが、Typeなら問題なく通る。 declare function numMapToVoid(obj: {[key: string]: number}): void; // interfaceだとコンパイルエラー interface IObj { a: number } const iObj: IObj = {a: 1}; numMapToVoid(iObj); // エラー // typeだと問題なし type TObj = { a: number }; const tObj: TObj = {a: 1}; numMapToVoid(tObj); // OK これは不思議...というのがきっかけで、TypeScriptのInterfaceとTypeの機能上の違いを調べてみた。 比較まとめ TypeSc
新生活応援期間中とのことですので,"pandas"の使い方,特に日付フォーマットについて取り上げたいと思います.「Pythonはデータ分析に強い」という評判がありますが,これは以下のような「定番」パッケージによって実現されています. "pandas" for representing and analyzing data "NumPy" for basic numeriacal computation "SciPy" for scientific computation including statistics "StatsModels" for regression and other statistical analysis "matplotlib" for visualization (以上,"Think Stats" より引用.) pandas は,Seriesオブジェクト(1次元
はじめに 某記事で、 implicitsなんて呼ばないで、各機能ごとに適切な名称で呼ぼう。あと、重要なのは型クラスだからそれだけ注意すればOK、という趣旨のことを書きましたが、どうやって使えばいいかは投げっぱなしだったのでその導入編だけでも書こうかと思います。この記事での目的は簡単で、 Scalaのコレクションのsumメソッド(とproductメソッド)を自作の有理数クラスに適用できるようにしよう ということになります。実際のところ、sumメソッドを整数リストに対して呼び出す、たとえば でも、型クラスを「使って」はいるのですが、実際に型クラスに新しいインスタンスを追加していないので、使った実感がわかないと思います。そこで、有理数クラスです。実装が比較的簡単である上に、四則演算への対応が容易という点でこれを選ぶことにしました。 まずは、有理数クラス(Rational)の実装です。ここは、本題
このドキュメントの目的 自分は趣味でFlowをずっと使っていて、またプロダクションでも今まで3プロジェクトほどにFlowを導入した。その知見。 「Flow は便利そうだけど、怖い」「いれてみたら色々ハマったからクソ」「わからん、なにもかも…」という人に対し、自分がいままで出くわしたパターンや、聞かれた疑問について、メジャーな解法を提示する。 なぜFlowを導入するか Babel から段階的に導入することが出来る React の JSX にも推論を入れることができる 部分的に適用できる ASTがES準拠であり、ESLintなどがツールが使える(TSは独自AST) それ自身ランタイムに全く影響はないので落とすのも簡単 実際にはReactと一緒に使うのが、エコシステムもユースケースも揃っていて、一番効果を発揮するだろう。それか、小さい npm モジュールを自分で書くとき。 型のメリット/デメリッ
class Animal { kind: string } class Dog extends Animal { dog_type: string } function showDogType(dog: Dog) { console.log(dog.dog_type) } // 代入不可のコンパイルエラーになってほしい const someAnimalFunc: (animal: Animal) => void = showDogType // 実際はここでランタイムエラー someAnimalFunc(new Animal()) このコードがコンパイルエラーにならず, 実行時エラーになってしまうことについて. TL;DR 型システムの健全性 (型の矛盾が生じないこと) vs 利便性のトレードオフから, 現在の TypeScript は健全性を諦めている 関数型はパラメータについて biv
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く