Skip to content

Instantly share code, notes, and snippets.

func curry<A, B, C>(f: (A, B) -> C) -> A -> B -> C {
return { a in { b in f(a, b) } }
}
let add = curry(+)
let xs = 1...100
let x = xs.map(add(2)) // [3, 4, 5, 6, etc]
@H-Ghadirian
H-Ghadirian / forward-pipe.swift
Created June 23, 2018 10:56
Forward Pipe |> operator
infix operator |> { precedence 50 associativity left }
// Pipe forward: transform "x |> f" to "f(x)" and "x |> f |> g |> h" to "h(g(f(x)))"
public func |> <T,U>(lhs: T, rhs: T -> U) -> U {
return rhs(lhs)
}
//Without the forward pipe
let convertedImage = convertToGrayscale(image: adjustColors(image: image))
struct User {
let name: String?
let surname: String?
let nickname: String?
}
//Our goal is to build a human-readable string in a:
//Name Surname, "Nickname"
//format.
//The example output would be:
struct Novella {
let name: String
}
struct Novellas {
let novellas: [Novella]
}
struct NovellasIterator: IteratorProtocol {
{
"bannerCategory":{
"name":"",
"apps":[
{
"ImageName":"enhancedWith3dTouch"
},
{
"ImageName":"drSeussBanner"
},
{
"stream_url":"http://184.72.239.149/vod/smil:BigBuckBunny.smil/playlist.m3u8",
"main_title":"",
"second_title":"",
"preview_image":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTPsJRF7eUSmp5z8r76MxwR1MEn3l7H_L1OaAjkFOfowOC87RCJyQ",
"type":"",
"thumbnails":[
{
"count_x":"0",
"count_y":"0",
{
"main_title": "عنوان اصلی",
"second_title": "عنوان فرعی",
"type": "vod",
"stream_url": "",
"thumbnails": [
{
"count_x": 5,
"count_y": 5,
"width": 160,
enum HTTPMethod: String {
case get = "GET"
}
class HTTPLayer {
let baseURL = URL(string: "https://hamed-gh.com")!
func request(at endpoint: Endpoint, completion: @escaping (Data?, Error?) -> Void) {
let url = baseURL.appendingPathComponent(endpoint.path)
//A standard phone number pad has the following layout.
//1 2 3
//4 5 6
//7 8 9
// 0
//Using this layout and a starting digit we can generate numbers as follows: The first digit is the
//starting digit and each subsequent digit is directly left, right, above or below the previous digit on
//the number pad. For example if the starting digit is 1, 1256 is a valid number, but 1289 is not
//valid because 8 is not directly next to 2. Write a function that takes a starting digit d and an
//integer n as input and returns a list of all unique numbers of length n generated in this way.