Consider this Swift:
let addr: String? = person?.job?.address
In this case, if person is nil, it returns nil, otherwise it bind
s the job
, and repeats.
This same thing is expressed in Haskell as:
; ModuleID = 'fact.ltr' | |
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" | |
target triple = "x86_64-apple-darwin15.0.0" | |
; Function Attrs: nounwind readnone | |
define i64 @main() #0 { | |
entry: | |
ret i64 0 | |
} |
// note the signature => (T?, T -> U?) -> U? | |
func flatMap<T, U>(value: T?, transform: T -> U?) -> U? { | |
switch value { | |
case .None: return .None | |
case .Some(let contained): return transform(contained) | |
} | |
} | |
// different from this => (T?, T -> U) -> U? | |
func map<T, U>(value: T?, transform: T -> U) -> U? { |
Consider this Swift:
let addr: String? = person?.job?.address
In this case, if person is nil, it returns nil, otherwise it bind
s the job
, and repeats.
This same thing is expressed in Haskell as:
var str = "Hello, playground" | |
public extension String { | |
public func replacing(range: CountableClosedRange<Int>, with replacementString: String) -> String { | |
let start = characters.index(characters.startIndex, offsetBy: range.lowerBound) | |
let end = characters.index(start, offsetBy: (range.upperBound + 1) - range.lowerBound) | |
return self.replacingCharacters(in: start ..< end, with: replacementString) | |
} | |
} |
import Foundation | |
enum Relation<T> where T: Strideable { | |
case equal(T) | |
case greater(T) | |
case greaterOrEqual(T) | |
case less(T) | |
case lessOrEqual(T) | |
case between(T, T) | |
; ModuleID = '-' | |
source_filename = "-" | |
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" | |
target triple = "x86_64-apple-macosx10.9" | |
define hidden void @_TFV4test3Foo8printBarfT_T_(i64) #0 { | |
entry: | |
%1 = call %swift.type* @_TMaP_() #6 | |
%2 = call { %swift.bridge*, i8* } @_TFs27_allocateUninitializedArrayurFBwTGSax_Bp_(i64 1, %swift.type* %1) | |
%3 = extractvalue { %swift.bridge*, i8* } %2, 0 |
(source_file | |
(import_decl 'Foundation') | |
(top_level_code_decl | |
(brace_stmt | |
(pattern_binding_decl | |
(pattern_named type='CGPoint' 'a') | |
(call_expr type='CGPoint' location=point.swift:2:9 range=[point.swift:2:9 - line:2:38] nothrow arg_labels=x:y: | |
(constructor_ref_call_expr type='(Int, Int) -> CGPoint' location=point.swift:2:9 range=[point.swift:2:9 - line:2:9] nothrow | |
(declref_expr implicit type='(CGPoint.Type) -> (Int, Int) -> CGPoint' location=point.swift:2:9 range=[point.swift:2:9 - line:2:9] decl=CoreGraphics.(file).CGPoint.init(x:y:) function_ref=single specialized=no) | |
(type_expr type='CGPoint.Type' location=point.swift:2:9 range=[point.swift:2:9 - line:2:9] typerepr='CGPoint')) |
foreign type Array { | |
foreign init() -> Array | |
foreign func push(_: Any) | |
foreign subscript(_: Int) -> Any | |
foreign var length: Int | |
} | |
foreign type Math { | |
foreign static func random() -> Double | |
foreign static func floor(_: Double) -> Int |
foreign type String { | |
foreign init(_: *Int8) -> String | |
foreign var length: Int | |
foreign func charCodeAt(_: Int) -> Int8 | |
foreign static func fromCharCode(_: Int8) -> String | |
} | |
type Op { | |
let opcode: Int8 | |
var operand: Int |