Skip to content

Instantly share code, notes, and snippets.

@dinneo
dinneo / git-tag-delete-local-and-remote.sh
Created July 19, 2018 09:15 — forked from mobilemind/git-tag-delete-local-and-remote.sh
how to delete a git tag locally and remote
# delete local tag '12345'
git tag -d 12345
# delete remote tag '12345' (eg, GitHub version too)
git push origin :refs/tags/12345
# alternative approach
git push --delete origin tagName
git tag -d tagName

Category theory(範疇論)之什麼是Functor, Applicative, Monad, Semigroup, Monoid?

之前曾經寫過一篇 什麼是Monad? ,裡面提到了對於Monad的解釋,而今天進一步解釋這些在Functional Programming中常見到的詞:Functor, Applicative, Monad, Semigroup, Monoid。 透過放在一起比較,讓我們更清楚這些之間的差別與關係。

##Functor

在提到Functor之前,請先了解一下scala中map的作用,map可以讓你傳入一個function,而透過這個function是可以從F[A]轉換成F[B],這裡的F不管他(就是代表外面的包裝不會變),而裡面的A型態轉成B型態。

例如:

@dinneo
dinneo / map-reduce-filter-flatMap.swift
Created July 24, 2018 11:24 — forked from phynet/map-reduce-filter-flatMap.swift
Examples for flatMap, Map, Reduce, Filter in Swift
//reduce
//Use reduce to combine all items in a collection to create a single new value.
let numbers = [1, 3, 5, 7, 9]
//sum all values from the array (+)
let result = numbers.reduce(0, +)
//sum all values from the array (+) plus one
let result2 = numbers.reduce(1, +)
let result3 = numbers.reduce(1, *)
@dinneo
dinneo / Precondition.swift
Created August 30, 2018 10:29 — forked from nschum/Precondition.swift
Testing precondition (or assert) in Swift
/// Our custom drop-in replacement `precondition`.
///
/// This will call Swift's `precondition` by default (and terminate the program).
/// But it can be changed at runtime to be tested instead of terminating.
func precondition(@autoclosure condition: () -> Bool, @autoclosure _ message: () -> String = "", file: StaticString = __FILE__, line: UWord = __LINE__) {
preconditionClosure(condition(), message(), file, line)
}
/// The actual function called by our custom `precondition`.
var preconditionClosure: (Bool, String, StaticString, UWord) -> () = defaultPreconditionClosure
@dinneo
dinneo / checkIfPalindrome.swift
Created October 5, 2018 09:33 — forked from alimir1/checkIfPalindrome.swift
Check if string is palindrome Swift 3
func isPalindrome(_ word: String) -> Bool {
let word = word.lowercased().characters.filter{ $0 != " " }
for (i, character) in word.enumerated() {
if character != word[word.count-i-1] {
return false
}
}
return true
}
@dinneo
dinneo / Matrix Examples.swift
Created October 9, 2018 14:17 — forked from JadenGeller/Matrix Examples.swift
Matrices in Swift
// Numerical matrix examples
let x: Matrix = [[10, 9, 8], [3, 2, 1]]
let y: Matrix = [[1, 2, 3], [4, 5, 6]]
let z: Matrix = [[1, 2], [3, 4], [5, 6]]
x + y // [[11, 11, 11], [7, 7, 7]]
x * y // [[10, 18, 24], [12, 10, 6]]
2 * x // [[20, 18, 16], [6, 4, 2]]
y ** z // [[22, 28], [49, 64]]
@dinneo
dinneo / Matrix.swift
Created October 9, 2018 15:43 — forked from proxpero/Matrix.swift
Rotating a matrix 90 degrees in place in Swift
//: Rotate a matrix 90 degrees in place
// "Write a function to rotate an NxN matrix by 90 degrees. You should rotate it in place, meaning you can't use another matrix to perform the rotation, but instead you have to use the same given structure."
// https://www.shiftedup.com/2015/05/10/programming-challenge-rotating-a-matrix-90-degrees-in-place
// This implementation of a matrix is taken from Apple's own.
// https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Subscripts.html
// The extensions are my own.
// Xcode 7.0, Swift 2.0
class ArrayImpl<T> {
var space: Int
var count: Int
var ptr: UnsafeMutablePointer<T>
init(count: Int = 0, ptr: UnsafeMutablePointer<T> = nil) {
self.count = count
self.space = count
@dinneo
dinneo / Stack.swift
Created October 30, 2018 08:15 — forked from avdyushin/Stack.swift
Stack data structure via linked lists using Swift enums
indirect enum LinkedList<T> {
case Empty
case Node(value: T, next: LinkedList<T>)
init() { self = .Empty }
}
extension LinkedList {
func cons(x: T) -> LinkedList<T> {
return .Node(value: x, next: self)
struct LinkedList<T> {
var head: Node<T>
init(head: Node<T>) {
self.head = head
}
}
indirect enum Node<T> {