Posts

Showing posts with the label iOS Application

Filter,Map and Reduce Higher Order Functions in Swift

Image
Let's code : Take an Input array : let inputArr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] print ( "Input Array : \ ( inputArr )" ) Perform Filter on array : let filterArr = inputArr. filter ({ return $0 % 2 == 0 }) print ( "Filtered Array (even elements) \ ( filterArr )" ) Perform Map on array   : let mapArr = inputArr. map ({ return $0 * 2 }) print ( "Map Array (*2) \ ( mapArr )" ) Perform Reduce on array : let reduce = inputArr. reduce ( 0 , +) print ( "Reduced Value (Sum of elements) \ ( reduce )" ) Here is the output :

Append and Traverse a Doubly Linked List using Swift - iOS

Image
Let's Code : OK, let's see the result first , Adding some random city names to linked list and printing the linked list : Let's create the node now : Node.swift import Foundation class Node: NSObject {     var next: Node ?     weak var previous: Node ?     var value : String ?     init (value : String ) {         self . value = value     } } Now implement linked list in another swift file DoublyLinkedList. swift  import UIKit class DoublyLinkedList: NSObject {     fileprivate var head : Node ?     private var tail : Node ?     override init () {         super . init ()     }     public var isEmpty : Bool {         return head == nil     }     public var first : Node ? {         ...