\( \newcommand{\N}{\mathbb{N}} \newcommand{\R}{\mathbb{R}} \newcommand{\C}{\mathbb{C}} \newcommand{\Q}{\mathbb{Q}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\P}{\mathcal P} \newcommand{\B}{\mathcal B} \newcommand{\F}{\mathbb{F}} \newcommand{\E}{\mathcal E} \newcommand{\brac}[1]{\left(#1\right)} \newcommand{\abs}[1]{\left|#1\right|} \newcommand{\matrixx}[1]{\begin{bmatrix}#1\end {bmatrix}} \newcommand{\vmatrixx}[1]{\begin{vmatrix} #1\end{vmatrix}} \newcommand{\lims}{\mathop{\overline{\lim}}} \newcommand{\limi}{\mathop{\underline{\lim}}} \newcommand{\limn}{\lim_{n\to\infty}} \newcommand{\limsn}{\lims_{n\to\infty}} \newcommand{\limin}{\limi_{n\to\infty}} \newcommand{\nul}{\mathop{\mathrm{Nul}}} \newcommand{\col}{\mathop{\mathrm{Col}}} \newcommand{\rank}{\mathop{\mathrm{Rank}}} \newcommand{\dis}{\displaystyle} \newcommand{\spann}{\mathop{\mathrm{span}}} \newcommand{\range}{\mathop{\mathrm{range}}} \newcommand{\inner}[1]{\langle #1 \rangle} \newcommand{\innerr}[1]{\left\langle #1 \right \rangle} \newcommand{\ol}[1]{\overline{#1}} \newcommand{\toto}{\rightrightarrows} \newcommand{\upto}{\nearrow} \newcommand{\downto}{\searrow} \newcommand{\qed}{\quad \blacksquare} \newcommand{\tr}{\mathop{\mathrm{tr}}} \newcommand{\bm}{\boldsymbol} \newcommand{\cupp}{\bigcup} \newcommand{\capp}{\bigcap} \newcommand{\sqcupp}{\bigsqcup} \newcommand{\re}{\mathop{\mathrm{Re}}} \newcommand{\im}{\mathop{\mathrm{Im}}} \newcommand{\comma}{\text{,}} \newcommand{\foot}{\text{。}} \)

Saturday, September 21, 2019

Javascript Library that Swap <div> Elements

I am responsible for creating a simple game that involve an interchange of div objects. I record an approach modified from a source code from the net:

In the function defined in property onDragEnd the saveSortOrder is created such that upon each drag event, an array is created to store the current position of each div element, for example, [1, 0, 3 , 2, 4] means div of id 1, 0, 3, 2, 4 are at position 0, 1, 2, 3, 4 respectively.

Friday, September 20, 2019

Scene Loader in Unity

For loading scenes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    [SerializeField] int startScene = 0;

    public void LoadNextScene()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex + 1);
    }

    public void ReturnStartScene()
    {
        SceneManager.LoadScene(startScene);
    }

    public void QuitGame()
    {
        Application.Quit();
    }

    public void LoadGameOver()
    {
        SceneManager.LoadScene(2);
    }
}

Sunday, September 1, 2019

.reduce

之前有人給我這様的一道題,設 A, B 為 string,編寫一個程序使得,當 B 的字母都在 A 出現過時,傳出 true,否則 false。例如 A = "abcde", B = "abc", 因為 B 中的 a, b, c 都在 A 出現過,因此我們的 funtion 要傳出 true。

因為沒有 .every 或 .reduce 等等的概念,第一次做的時候寫了兩個 for loop。然後答案使用 .every,嗯,知道多一個 method 也是好的。

最近在學習 .reduce,只要是需要對整個 array 的 entry 作 "consecutive action" (一個一個順序作一些 action),都可以用 reduce 來解決。例如剛剛講到的 function 除了用 .every 外,還可以這様寫:
const containment = (string1, string2) => {
  const string1Array = string1.toLowerCase().split("");
  const string2Array = string2.toLowerCase().split("");

  return string2Array.reduce((A, char) => {
    return A && string1Array.indexOf(char) > -1;
  }, true);
};
好像有點太複雜 w? 我們來看看更簡單的例子,我們編寫 function maximum 來取得一個 number array 最大的數值:
const maximum = (...numbers) => {
  return numbers.reduce((A, b) => {
    return A > b ? A : b;
  });
};
這様一來 maximum(2, 3, 11, -2) 會 return 11。還是看不懂?我們現在就來講講 reduce 的概念﹗為了講得更仔細,無可避免要用數學一點的精確語言來解析。