2020年1月18日土曜日

Custom reduce function to Javascript Allay class

learn how to use Javascript prototype.
let's make a reduce() function of  Allay class using  javascript prototype.


Array.prototype.myReduce = function(callback, initVal) { 
 let acc = initVal; 
  for (i = 0; i < this.length; i++) { 
   acc = callback(acc, this[i]); 
  }; 
 return acc
}


let myArray = [1,2,3]
myArray.myReduce((acc, elem) => { return acc + elem; }, 0)
6


What is reduce() function 

from mozira document of reduce

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));

// expected output: 15



2020年1月16日木曜日

ios customise tabbar with UITabBarController

Here is a sample of creating your own UITabBarController and  customise tab bar behaviour on ios. not only customising tint colour of icon or icon itself but also adding more functionality when switching tab, it required to make your own class inheriting UITabBarController but you want to use the views and tab bar controller scene from story board.
  1. start from tab application default
  2. remove segure from story board connected to views from tab bar scene
  3. set story boad ID to each scene which would be navigated by the tab bar. (FirstView, SecondView for example)
  4. create a new class MyTabBarController
    ```
    class MyTabBarController: UITabBarController, UITabBarControllerDelegate {
    var firstViewController: FirstViewController!
    var secondViewController: SecondViewController!
    override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    firstViewController = storyboard?.instantiateViewController(withIdentifier: "FirstView") as? FirstViewController
    secondViewController = storyboard?.instantiateViewController(withIdentifier: "SecondView") as? SecondViewController
    firstViewController.tabBarItem.image = UIImage(named: "customFirstImage")
    secondViewController.tabBarItem.image = UIImage(named: "customFirstImage")
    viewControllers = [firstViewController, secondViewController]
    selectedIndex = 0
    }
    /*
    // MARK: - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
    }
    */
    }
    ```
  5. set MyTabBarController as the class of tab bar controller in the story board

state property swift memo

what is @state property

Overview

SwiftUI manages the storage of any property you declare as a state. When the state value changes, the view invalidates its appearance and recomputes the body. Use the state as the single source of truth for a given view.
State instance isn’t the value itself; it’s a means of reading and mutating the value. To access a state’s underlying value, use its value property.
Only access a state property from inside the view’s body (or from functions called by it). For this reason, you should declare your state properties as private, to prevent clients of your view from accessing it.
You can get a binding from a state with the `binding` property, or by using the `$` prefix operator.

@Environment

Environment

A dynamic view property that reads a value from the view’s environment.

EnvironmentObject

A dynamic view property that uses a bindable object supplied by an ancestor view to invalidate the current view whenever the bindable object changes.

2020年1月10日金曜日

Swift Dictionary Grouping


     There is the comment in the Dictionary souce code.

    /// Creates a new dictionary whose keys are the groupings returned by the

    /// given closure and whose values are arrays of the elements that returned
    /// each key.
    ///
    /// The arrays in the "values" position of the new dictionary each contain at
    /// least one element, with the elements in the same order as the source
    /// sequence.
    ///
    /// The following example declares an array of names, and then creates a
    /// dictionary from that array by grouping the names by first letter:


let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
let studentsByLetter = Dictionary(grouping: students, by: { $0.first! })

EnvironmentObject Swift

EnvironmentObject in Swiftaccording to apple's Document A dynamic view property that uses a bindable object supplied by an ancestor view to invalidate the current view whenever the bindable object changes.

Declaration

@frozen @propertyWrapper struct EnvironmentObject<ObjectTypewhere ObjectType : ObservableObject

what does that mean ?


my understanding is that variable wrapped @EnvironmentObject can be shared 
across all views and update view when the variable is updated.
This is like the way React/Redux store doing.
final class UserData: ObservableObject  {
    @Published var showFavoritesOnly = false
    @Published var landmarks = landmarkData
}
struct LandmarkList: View {

    @EnvironmentObject var userData: UserData

    var body: some View {
        NavigationView{
            List {
                Toggle(isOn: $userData.showFavoritesOnly  , label: {Text("Favorites only")})
                ForEach(userData.landmarks) { landdata in
                    if (!self.userData.showFavoritesOnly || landdata.isFavorite) {
                        NavigationLink(destination: ContentView(landmark:   landdata)) {
                            LandmarkRow(landmark: landdata)
                            }.navigationBarTitle(Text("Landmark"))
                        }
                    }
                }
        }
    }
}


2020年1月7日火曜日

Book for functional Analysis

I fond this book
It is a math book , not for functional programming.
Introductory Functional Analysis With Applications