Skip to content

Instantly share code, notes, and snippets.

View milanpanchal's full-sized avatar
🏠
Working from home

Milan Panchal milanpanchal

🏠
Working from home
View GitHub Profile
@milanpanchal
milanpanchal / FunctionalCodingExample.swift
Created April 30, 2020 20:58
From the given array, calculate the sum of, the square of all the even numbers using Functional style.
let numbers = Array(1...20)
func isEven(x: Int) -> Bool {
return x % 2 == 0
}
func square(x: Int) -> Int {
return x * x
}
@milanpanchal
milanpanchal / ImperativeExample.swift
Created April 30, 2020 20:54
From the given array, calculate the sum of, the square of all the even numbers using Imperative style.
let numbers = Array(1...20)
var sum = 0
for val in numbers {
if val % 2 == 0 {
sum += (val * val)
}
}
@milanpanchal
milanpanchal / higherOrderFunction_Reduce.swift
Last active April 30, 2020 18:22
Swift ➞ Higher orders function reduce(_:_:) example
import UIKit
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
//var sum = 0
//
//for number in numbers {
// sum += number
//}
@milanpanchal
milanpanchal / higherOrderFunction_Map.swift
Last active April 30, 2020 18:23
Swift ➞ Higher orders function map(_:) example
import UIKit
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
//var newNumbers = [Int]()
//
//for number in numbers {
// let newNumber = number * number
// newNumbers.append(newNumber)
//}
@milanpanchal
milanpanchal / higherOrderFunction_Filter.swift
Last active April 30, 2020 18:23
Swift ➞ Higher orders function filter(_:) example
import UIKit
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
//var evenNumbers = [Int]()
//
//for number in numbers {
// if (number % 2 == 0) {
// evenNumbers.append(number)
// }
@milanpanchal
milanpanchal / findAverage.swift
Created April 30, 2020 08:37
Find average of two numbers
// Functional Programming: Find avg. of two numbers
let findAverage: (Float, Float) -> Float = { x, y in
return (x + y) / 2
}
let firstValue:Float = 24.0
let secondValue:Float = 7.0
let avg = findAverage(firstValue, secondValue)
@milanpanchal
milanpanchal / vowelConsonantCount.swift
Created April 4, 2020 12:41
Count the number of vowel and consonant from the given string
import Foundation
var str = "Hello, playground"
extension String {
func vowelConsonantCount() -> (vowels: Int, consonants: Int) {
var numOfVow = 0, numOfCon = 0
self.lowercased().forEach { (char) in
if "aeiou".contains(char) { numOfVow += 1 }
@milanpanchal
milanpanchal / numberOfVowels.swift
Last active April 4, 2020 12:47
Count number of vowels in to given string
import Foundation
var str = "Hello, playground"
extension String {
var numberOfVowels: Int {
var numOfVow = 0
self.lowercased().forEach { (char) in
if "aeiou".contains(char) {
numOfVow += 1
@milanpanchal
milanpanchal / UserViewModel.swift
Created March 20, 2020 10:28
MVVM Design Pattern - UserViewModel
//
// UserViewModel.swift
// MVVM
//
// Created by Milan Panchal on 19/03/20.
// Copyright © 2020 Jeenal Infotech. All rights reserved.
//
import UIKit
// 20200320153014
// https://randomuser.me/api/?results=20
{
"results": [
{
"gender": "male",
"name": {
"title": "Mr",
"first": "Victor",