// This is a TypeScript comment/*Need more space?No problem!This is a multi-line TypeScript comment :)*//*** In TS, you write documentation like this for TypeDoc*/const id = a => a
-- This is a Haskell comment{-Need more space?No problem!This is a multi-line Haskell comment :)-}--| Add a pipe for a short function description-- You can keep talking about is afterwards-- These will turn into formatted documentation with Haddockid a = a{-| You can use a multi-line comment for Haddock, tooThe same rules apply.-}swap (a, b) -> (b, a)
const hi = "Hello World"
hi = "Hello World"
const hi: string = 'Hello World'
hi :: Texthi = "Hello World"
hi = ("Hello World" :: Text)
a => a * 2
\a -> a * 2-- NOTE: `\` is an easier to type lambda `λ`
(*2)
const double = (a : number) => a * 2
double :: Int -> Intdouble a = a * 2
double :: Int -> Intdouble = (*2)
// Does not exist
(*&) :: Int -> String -> Stringnum *& str = show num ++ str-- NOTE: `show` turns values into strings
const result = doTheThing(1, 2, ["a", "b"], 1.1)
result = doTheThing 1 2 ["a", "b"] 1.1
fireTheMissiles()
fireTheMissiles
const withInner = (toLog: string): void => {const innerValue = 'That is the question!'console.log(`${toLog}? Or not ${toLog}? ${innerValue}`)}
withInner' toLog =letinnerValue = "That is the question!"msg = toLog <> "? Or not " <> toLog <> "? " <> innerValueinlogInfo msg
withInner toLog = logInfo msgwheremsg = toLog <> "? Or not " <> toLog <> "? " <> innerValueinnerValue = "That is the question!"
const result = triple(prod(1, double(1), 3))/* i.e.const two = double(1)const six = prod(1, two, 3)const result = triple(six)*/
result = triple (prod 1 (double 1) 3)
result = triple $ prod 1 (double 1) 3
result = 3& prod 1 (double 1)& triple
const timesSix = (a: number): number => triple(double(a))
timesSix :: Int -> InttimeSix = triple . double
const math = (n: number, m: number): number =triple(prod(10, double(1), n), m)
math :: Int -> Int -> Intmath n m = triple (prod 10 (double 1) n) m
math :: Int -> Int -> Intmath = triple . prod 10 (double 1)
import * from "./GetEverything"import * as HL from "./HelperLib";import { foo, bar } from "./SomeLib"const id = a => aconst swap = ([a, b]) => [b, a]const hiddenValue = HL.downcase("not exported")export {id,swap}export { toReExport } from "./ExternalLib"
module MyModule( id, swap, toReExport) whereimport GetEverythingimport SomeLib (foo, bar)import ExternalLib (toReExport)import qualified HelperLib as HLid a = aswap (a, b) = (b, a)hiddenValue = HL.downcase "not exported"
type Name = stringtype Balance = number
type Name = Texttype Balance = Int
type TrafficLight = 'RED' | 'YELLOW' | 'GREEN'
data TrafficLight= Red| Yellow| Green
type GearRatio = [number, number] // [big gear, little gear]type Bike = [string, GearRatio] // [bikeName, gear ratio]
data GearRatio = GearRatio Int Intdata Bike = Bike Text GearRatio
interface GearRatio {big: numberlittle: number}interface Bike {name: stringgear: GearRatio}
class Geared {big: numberlittle: numberconstructor(gbr: number, lgr: number) {big = bgrlittle = lgr}}class Bike extends Geared {name: stringconstructor(bgr: number, lgr: number, bikeName: string) {name = bikeNamesuper(bgr, lgr)}}
data Gear = GearRatio{ _big :: Natural, _little :: Natural}data Bike = Bike{ _name :: Text, _gear :: Gear}
const makeGear = (big, little): GearRatio => ({ big, little })const makeBike = (big, little, name): Bike => ({name,gear = makeGear(big, little)})
/*Included in the `class` delcaration. For example:constructor(bgr: number, lgr: number) {big = bgrlittle = lgr}*/
{-Constructors are the capitalized word onthe right of the `=` in the `data` declarationIt auto-generates constructor functionswith the folling type signatures:GearRatio :: Natural -> Natural -> GearedBike :: Text -> GearRatio -> Bike-}
const myBike = makeBike(2, 5, '10 Speeder')
const myBike = new Bike(2, 5, '10 Speeder')
myBike = Bike "10 Speeder" (GearRatio 2 5)
myBike = Bike{ _name = "10 Speeder" -- Use named fields, _gear = GearRatio 2 5 -- Or just ordered values}
myBike :: BikemyBike =let_big = 2_little = 5_name = "10 Speeder"_gear = GearRatio {..}inBike {..}
const big: number = myGears.bigconst little = myBike.gear.little
const { bigGear } = myGearsconst getBig = ({ big }) => bigconst ratio = ({ big, little }) => big / little
GearRatio big _ = myGearsgetBig (GearRatio {_big}) = _bigratio (GearRatio {_big, _little}) = _big / _little
ratio (GearRatio {..}) = _big / _little
import Control.Lens.TH$(makeLenses ''Gear)$(makeLenses ''Bike)myGear ^. big -- NOTE: lenses have no underscores!-- NestedmyBike ^. gear . little
// Preambleconst myGear = new GearRatio(2, 5)const myBike = new Bike('10 Speeder', myGear)// Mutable UpdatemyGear.littleGear = 6myBike.name = '12 Speeder'// Immutable Updateconst upgradedGear = Object.assign({}, myGear)const upgradedBike = Object.assign({}, myBike, {gear: upgradedGear})
-- PreamblemyGear = GearRatio 2 5myBike = Bike "10 Speeder" myGear-- Immutable UpdateupgradedBike = myBike{ _name = "12 Speeder", _gear = myGear { _littleGear = 6 }}
-- Preambleimport Control.Lens.THmakeLenses ''GearmakeLenses ''BikemyGear = GearRatio 2 5myBike = Bike "10 Speeder" myGear-- Immutable UpdateupgradedBike = myBike & name .~ "12 Speeder"& gear.littleGear .~ 6
if (condition) {branchA} else {branchB}
condition ? branchA : branchB
if conditionthen branchAelse branchB
if (percent >= 90 || student.paidBribe) {return GRADES.A} else if (percent >= 75) {return GRADES.B} else if (percent >= 60) {return GRADES.C} else if (percent >= 50) {return GRADES.D} else {return GRADES.F}
if | percent >= 90 || paidBribe student = A| percent >= 75 = B| percent >= 60 = C| percent >= 50 = D| otherwise = F
let nextActionswitch (lightState) {case LIGHT.RED:nextAction = ACTION.STOPbreakcase LIGHT.YELLOW:nextAction = ACTION.SLOWbreakcase LIGHT.GREEN:nextAction = ACTION.DRIVEbreakdefault:nextAction = currentAction}
nextAction =case lightState ofRed -> StopYellow -> SlowGreen -> Drive_ -> currentAction