diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 00000000000..e69de29bb2d diff --git a/assets/6_free_software_licenses.344bcf8c.png b/assets/6_free_software_licenses.344bcf8c.png new file mode 100644 index 00000000000..98a4eb4cd96 Binary files /dev/null and b/assets/6_free_software_licenses.344bcf8c.png differ diff --git a/assets/AXI_Read_Burst.0e9ed250.png b/assets/AXI_Read_Burst.0e9ed250.png new file mode 100644 index 00000000000..523702d4c84 Binary files /dev/null and b/assets/AXI_Read_Burst.0e9ed250.png differ diff --git a/assets/AXI_Write_Burst.63cb974a.png b/assets/AXI_Write_Burst.63cb974a.png new file mode 100644 index 00000000000..9d55f15ea05 Binary files /dev/null and b/assets/AXI_Write_Burst.63cb974a.png differ diff --git a/assets/CSS3SelectorList.c7ec6bba.png b/assets/CSS3SelectorList.c7ec6bba.png new file mode 100644 index 00000000000..5f97bf55664 Binary files /dev/null and b/assets/CSS3SelectorList.c7ec6bba.png differ diff --git a/assets/CriticalRenderPath.9e821854.svg b/assets/CriticalRenderPath.9e821854.svg new file mode 100644 index 00000000000..dbf82cfcf76 --- /dev/null +++ b/assets/CriticalRenderPath.9e821854.svg @@ -0,0 +1 @@ + diff --git a/assets/DesignPatterns.df4b7baf.png b/assets/DesignPatterns.df4b7baf.png new file mode 100644 index 00000000000..7ef020645e8 Binary files /dev/null and b/assets/DesignPatterns.df4b7baf.png differ diff --git a/assets/EventLoop.30d3251e.jpg b/assets/EventLoop.30d3251e.jpg new file mode 100644 index 00000000000..2367b59a1cb Binary files /dev/null and b/assets/EventLoop.30d3251e.jpg differ diff --git a/assets/FordFulkersonAlgorithm.94aecb4b.png b/assets/FordFulkersonAlgorithm.94aecb4b.png new file mode 100644 index 00000000000..782c2ecafd7 Binary files /dev/null and b/assets/FordFulkersonAlgorithm.94aecb4b.png differ diff --git a/assets/Home.f03b71db.js b/assets/Home.f03b71db.js new file mode 100644 index 00000000000..fb80815362a --- /dev/null +++ b/assets/Home.f03b71db.js @@ -0,0 +1 @@ +import{f as t,u as e,g as a,h as s,o as i,c as o,b as l,w as n,e as c,t as r,_ as d,p as v,i as u,F as f,r as m,j as k,k as p}from"./app.6dd2a1b7.js";v("data-v-1154499e");const h={key:0,class:"home-hero"},g={key:0,class:"figure"},y={key:1,id:"main-title",class:"title"},x={key:2,class:"description"};u();var _=t({setup(t){const{site:v,frontmatter:u}=e(),f=a((()=>{const{heroImage:t,heroText:e,tagline:a,actionLink:s,actionText:i}=u.value;return t||e||a||s&&i})),m=a((()=>u.value.heroText||v.value.title));return(t,e)=>s(f)?(i(),o("header",h,[s(u).heroImage?(i(),o("figure",g,[l("img",{class:"image",src:s(n)(s(u).heroImage),alt:s(u).heroAlt},null,8,["src","alt"])])):c("v-if",!0),s(m)?(i(),o("h1",y,r(s(m)),1)):c("v-if",!0),s(u).tagline?(i(),o("p",x,r(s(u).tagline),1)):c("v-if",!0),s(u).actionLink&&s(u).actionText?(i(),o(d,{key:3,item:{link:s(u).actionLink,text:s(u).actionText},class:"action"},null,8,["item"])):c("v-if",!0),s(u).altActionLink&&s(u).altActionText?(i(),o(d,{key:4,item:{link:s(u).altActionLink,text:s(u).altActionText},class:"action alt"},null,8,["item"])):c("v-if",!0)])):c("v-if",!0)}});_.__scopeId="data-v-1154499e",v("data-v-58405b12");const I={key:0,class:"home-features"},T={class:"wrapper"},b={class:"container"},A={class:"features"},L={key:0,class:"title"},$={key:1,class:"details"};u();var j=t({setup(t){const{frontmatter:n}=e(),d=a((()=>n.value.features&&n.value.features.length>0)),v=a((()=>n.value.features?n.value.features:[]));return(t,e)=>s(d)?(i(),o("div",I,[l("div",T,[l("div",b,[l("div",A,[(i(!0),o(f,null,m(s(v),((t,e)=>(i(),o("section",{key:e,class:"feature"},[t.title?(i(),o("h2",L,r(t.title),1)):c("v-if",!0),t.details?(i(),o("p",$,r(t.details),1)):c("v-if",!0)])))),128))])])])])):c("v-if",!0)}});j.__scopeId="data-v-58405b12",v("data-v-d7c9d38c");const w={key:0,class:"footer"},C={class:"container"},F={class:"text"};u();var q=t({setup(t){const{frontmatter:a}=e();return(t,e)=>s(a).footer?(i(),o("footer",w,[l("div",C,[l("p",F,r(s(a).footer),1)])])):c("v-if",!0)}});q.__scopeId="data-v-d7c9d38c",v("data-v-46076ded");const z={class:"home","aria-labelledby":"main-title"},B={class:"home-content"};u();var D=t({setup:t=>(t,e)=>{const a=k("Content");return i(),o("main",z,[l(_),p(t.$slots,"hero",{},void 0,!0),l(j),l("div",B,[l(a)]),p(t.$slots,"features",{},void 0,!0),l(q),p(t.$slots,"footer",{},void 0,!0)])}});D.__scopeId="data-v-46076ded";export default D; diff --git a/assets/ProgrammingParadigms.04c60b43.jpg b/assets/ProgrammingParadigms.04c60b43.jpg new file mode 100644 index 00000000000..28d159115fc Binary files /dev/null and b/assets/ProgrammingParadigms.04c60b43.jpg differ diff --git a/assets/Prototype.be11f8c6.png b/assets/Prototype.be11f8c6.png new file mode 100644 index 00000000000..dc83547b3ee Binary files /dev/null and b/assets/Prototype.be11f8c6.png differ diff --git a/assets/ScriptingType.562e5309.svg b/assets/ScriptingType.562e5309.svg new file mode 100644 index 00000000000..688b89f7ad6 --- /dev/null +++ b/assets/ScriptingType.562e5309.svg @@ -0,0 +1 @@ +<script>Scripting:HTML Parser:<script defer>Scripting:HTML Parser:<script async>Scripting:HTML Parser:<script type="module">Scripting:HTML Parser:<script type="module" async>Scripting:HTML Parser:parserfetchexecutionruntime → diff --git a/assets/SearchPerformance.866c6c75.jpg b/assets/SearchPerformance.866c6c75.jpg new file mode 100644 index 00000000000..3c8d9b34258 Binary files /dev/null and b/assets/SearchPerformance.866c6c75.jpg differ diff --git a/assets/SortingPerformance.caae88a8.png b/assets/SortingPerformance.caae88a8.png new file mode 100644 index 00000000000..544c747b319 Binary files /dev/null and b/assets/SortingPerformance.caae88a8.png differ diff --git a/assets/app.6dd2a1b7.js b/assets/app.6dd2a1b7.js new file mode 100644 index 00000000000..dfdfb89b940 --- /dev/null +++ b/assets/app.6dd2a1b7.js @@ -0,0 +1 @@ +var e=Object.defineProperty,t=Object.defineProperties,n=Object.getOwnPropertyDescriptors,o=Object.getOwnPropertySymbols,r=Object.prototype.hasOwnProperty,s=Object.prototype.propertyIsEnumerable,l=(t,n,o)=>n in t?e(t,n,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[n]=o,i=(e,t)=>{for(var n in t||(t={}))r.call(t,n)&&l(e,n,t[n]);if(o)for(var n of o(t))s.call(t,n)&&l(e,n,t[n]);return e},a=(e,o)=>t(e,n(o));function c(e,t){const n=Object.create(null),o=e.split(",");for(let r=0;r!!n[e.toLowerCase()]:e=>!!n[e]}const u=c("Infinity,undefined,NaN,isFinite,isNaN,parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,BigInt"),d=c("itemscope,allowfullscreen,formnovalidate,ismap,nomodule,novalidate,readonly");function p(e){if(E(e)){const t={};for(let n=0;n{if(e){const n=e.split(h);n.length>1&&(t[n[0].trim()]=n[1].trim())}})),t}function v(e){let t="";if(T(e))t=e;else if(E(e))for(let n=0;nnull==e?"":M(e)?JSON.stringify(e,b,2):String(e),b=(e,t)=>L(t)?{[`Map(${t.size})`]:[...t.entries()].reduce(((e,[t,n])=>(e[`${t} =>`]=n,e)),{})}:$(t)?{[`Set(${t.size})`]:[...t.values()]}:!M(t)||E(t)||U(t)?t:String(t),y={},x=[],k=()=>{},_=()=>!1,w=/^on[^a-z]/,S=e=>w.test(e),C=e=>e.startsWith("onUpdate:"),N=Object.assign,B=(e,t)=>{const n=e.indexOf(t);n>-1&&e.splice(n,1)},P=Object.prototype.hasOwnProperty,A=(e,t)=>P.call(e,t),E=Array.isArray,L=e=>"[object Map]"===I(e),$=e=>"[object Set]"===I(e),O=e=>"function"==typeof e,T=e=>"string"==typeof e,j=e=>"symbol"==typeof e,M=e=>null!==e&&"object"==typeof e,R=e=>M(e)&&O(e.then)&&O(e.catch),F=Object.prototype.toString,I=e=>F.call(e),U=e=>"[object Object]"===I(e),V=e=>T(e)&&"NaN"!==e&&"-"!==e[0]&&""+parseInt(e,10)===e,D=c(",key,ref,onVnodeBeforeMount,onVnodeMounted,onVnodeBeforeUpdate,onVnodeUpdated,onVnodeBeforeUnmount,onVnodeUnmounted"),W=e=>{const t=Object.create(null);return n=>t[n]||(t[n]=e(n))},z=/-(\w)/g,H=W((e=>e.replace(z,((e,t)=>t?t.toUpperCase():"")))),q=/\B([A-Z])/g,G=W((e=>e.replace(q,"-$1").toLowerCase())),J=W((e=>e.charAt(0).toUpperCase()+e.slice(1))),Q=W((e=>e?`on${J(e)}`:"")),K=(e,t)=>e!==t&&(e==e||t==t),Y=(e,t)=>{for(let n=0;n{Object.defineProperty(e,t,{configurable:!0,enumerable:!1,value:n})},Z=e=>{const t=parseFloat(e);return isNaN(t)?e:t},ee=new WeakMap,te=[];let ne;const oe=Symbol(""),re=Symbol("");function se(e,t=y){(function(e){return e&&!0===e._isEffect})(e)&&(e=e.raw);const n=function(e,t){const n=function(){if(!n.active)return e();if(!te.includes(n)){ae(n);try{return ue.push(ce),ce=!0,te.push(n),ne=n,e()}finally{te.pop(),pe(),ne=te[te.length-1]}}};return n.id=ie++,n.allowRecurse=!!t.allowRecurse,n._isEffect=!0,n.active=!0,n.raw=e,n.deps=[],n.options=t,n}(e,t);return t.lazy||n(),n}function le(e){e.active&&(ae(e),e.options.onStop&&e.options.onStop(),e.active=!1)}let ie=0;function ae(e){const{deps:t}=e;if(t.length){for(let n=0;n{e&&e.forEach((e=>{(e!==ne||e.allowRecurse)&&i.add(e)}))};if("clear"===t)l.forEach(a);else if("length"===n&&E(e))l.forEach(((e,t)=>{("length"===t||t>=o)&&a(e)}));else switch(void 0!==n&&a(l.get(n)),t){case"add":E(e)?V(n)&&a(l.get("length")):(a(l.get(oe)),L(e)&&a(l.get(re)));break;case"delete":E(e)||(a(l.get(oe)),L(e)&&a(l.get(re)));break;case"set":L(e)&&a(l.get(oe))}i.forEach((e=>{e.options.scheduler?e.options.scheduler(e):e()}))}const me=c("__proto__,__v_isRef,__isVue"),ve=new Set(Object.getOwnPropertyNames(Symbol).map((e=>Symbol[e])).filter(j)),ge=_e(),be=_e(!1,!0),ye=_e(!0),xe=ke();function ke(){const e={};return["includes","indexOf","lastIndexOf"].forEach((t=>{e[t]=function(...e){const n=it(this);for(let t=0,r=this.length;t{e[t]=function(...e){de();const n=it(this)[t].apply(this,e);return pe(),n}})),e}function _e(e=!1,t=!1){return function(n,o,r){if("__v_isReactive"===o)return!e;if("__v_isReadonly"===o)return e;if("__v_raw"===o&&r===(e?t?Ze:Xe:t?Ye:Ke).get(n))return n;const s=E(n);if(!e&&s&&A(xe,o))return Reflect.get(xe,o,r);const l=Reflect.get(n,o,r);if(j(o)?ve.has(o):me(o))return l;if(e||fe(n,0,o),t)return l;if(ut(l)){return!s||!V(o)?l.value:l}return M(l)?e?nt(l):tt(l):l}}function we(e=!1){return function(t,n,o,r){let s=t[n];if(!e&&(o=it(o),s=it(s),!E(t)&&ut(s)&&!ut(o)))return s.value=o,!0;const l=E(t)&&V(n)?Number(n)!0,deleteProperty:(e,t)=>!0},Ne=N({},Se,{get:be,set:we(!0)}),Be=e=>M(e)?tt(e):e,Pe=e=>M(e)?nt(e):e,Ae=e=>e,Ee=e=>Reflect.getPrototypeOf(e);function Le(e,t,n=!1,o=!1){const r=it(e=e.__v_raw),s=it(t);t!==s&&!n&&fe(r,0,t),!n&&fe(r,0,s);const{has:l}=Ee(r),i=o?Ae:n?Pe:Be;return l.call(r,t)?i(e.get(t)):l.call(r,s)?i(e.get(s)):void(e!==r&&e.get(t))}function $e(e,t=!1){const n=this.__v_raw,o=it(n),r=it(e);return e!==r&&!t&&fe(o,0,e),!t&&fe(o,0,r),e===r?n.has(e):n.has(e)||n.has(r)}function Oe(e,t=!1){return e=e.__v_raw,!t&&fe(it(e),0,oe),Reflect.get(e,"size",e)}function Te(e){e=it(e);const t=it(this);return Ee(t).has.call(t,e)||(t.add(e),he(t,"add",e,e)),this}function je(e,t){t=it(t);const n=it(this),{has:o,get:r}=Ee(n);let s=o.call(n,e);s||(e=it(e),s=o.call(n,e));const l=r.call(n,e);return n.set(e,t),s?K(t,l)&&he(n,"set",e,t):he(n,"add",e,t),this}function Me(e){const t=it(this),{has:n,get:o}=Ee(t);let r=n.call(t,e);r||(e=it(e),r=n.call(t,e)),o&&o.call(t,e);const s=t.delete(e);return r&&he(t,"delete",e,void 0),s}function Re(){const e=it(this),t=0!==e.size,n=e.clear();return t&&he(e,"clear",void 0,void 0),n}function Fe(e,t){return function(n,o){const r=this,s=r.__v_raw,l=it(s),i=t?Ae:e?Pe:Be;return!e&&fe(l,0,oe),s.forEach(((e,t)=>n.call(o,i(e),i(t),r)))}}function Ie(e,t,n){return function(...o){const r=this.__v_raw,s=it(r),l=L(s),i="entries"===e||e===Symbol.iterator&&l,a="keys"===e&&l,c=r[e](...o),u=n?Ae:t?Pe:Be;return!t&&fe(s,0,a?re:oe),{next(){const{value:e,done:t}=c.next();return t?{value:e,done:t}:{value:i?[u(e[0]),u(e[1])]:u(e),done:t}},[Symbol.iterator](){return this}}}}function Ue(e){return function(...t){return"delete"!==e&&this}}function Ve(){const e={get(e){return Le(this,e)},get size(){return Oe(this)},has:$e,add:Te,set:je,delete:Me,clear:Re,forEach:Fe(!1,!1)},t={get(e){return Le(this,e,!1,!0)},get size(){return Oe(this)},has:$e,add:Te,set:je,delete:Me,clear:Re,forEach:Fe(!1,!0)},n={get(e){return Le(this,e,!0)},get size(){return Oe(this,!0)},has(e){return $e.call(this,e,!0)},add:Ue("add"),set:Ue("set"),delete:Ue("delete"),clear:Ue("clear"),forEach:Fe(!0,!1)},o={get(e){return Le(this,e,!0,!0)},get size(){return Oe(this,!0)},has(e){return $e.call(this,e,!0)},add:Ue("add"),set:Ue("set"),delete:Ue("delete"),clear:Ue("clear"),forEach:Fe(!0,!0)};return["keys","values","entries",Symbol.iterator].forEach((r=>{e[r]=Ie(r,!1,!1),n[r]=Ie(r,!0,!1),t[r]=Ie(r,!1,!0),o[r]=Ie(r,!0,!0)})),[e,n,t,o]}const[De,We,ze,He]=Ve();function qe(e,t){const n=t?e?He:ze:e?We:De;return(t,o,r)=>"__v_isReactive"===o?!e:"__v_isReadonly"===o?e:"__v_raw"===o?t:Reflect.get(A(n,o)&&o in t?n:t,o,r)}const Ge={get:qe(!1,!1)},Je={get:qe(!1,!0)},Qe={get:qe(!0,!1)},Ke=new WeakMap,Ye=new WeakMap,Xe=new WeakMap,Ze=new WeakMap;function et(e){return e.__v_skip||!Object.isExtensible(e)?0:function(e){switch(e){case"Object":case"Array":return 1;case"Map":case"Set":case"WeakMap":case"WeakSet":return 2;default:return 0}}((e=>I(e).slice(8,-1))(e))}function tt(e){return e&&e.__v_isReadonly?e:ot(e,!1,Se,Ge,Ke)}function nt(e){return ot(e,!0,Ce,Qe,Xe)}function ot(e,t,n,o,r){if(!M(e))return e;if(e.__v_raw&&(!t||!e.__v_isReactive))return e;const s=r.get(e);if(s)return s;const l=et(e);if(0===l)return e;const i=new Proxy(e,2===l?o:n);return r.set(e,i),i}function rt(e){return st(e)?rt(e.__v_raw):!(!e||!e.__v_isReactive)}function st(e){return!(!e||!e.__v_isReadonly)}function lt(e){return rt(e)||st(e)}function it(e){return e&&it(e.__v_raw)||e}function at(e){return X(e,"__v_skip",!0),e}const ct=e=>M(e)?tt(e):e;function ut(e){return Boolean(e&&!0===e.__v_isRef)}function dt(e){return function(e,t=!1){if(ut(e))return e;return new pt(e,t)}(e)}class pt{constructor(e,t=!1){this._shallow=t,this.__v_isRef=!0,this._rawValue=t?e:it(e),this._value=t?e:ct(e)}get value(){return fe(it(this),0,"value"),this._value}set value(e){e=this._shallow?e:it(e),K(e,this._rawValue)&&(this._rawValue=e,this._value=this._shallow?e:ct(e),he(it(this),"set","value",e))}}function ft(e){return ut(e)?e.value:e}const ht={get:(e,t,n)=>ft(Reflect.get(e,t,n)),set:(e,t,n,o)=>{const r=e[t];return ut(r)&&!ut(n)?(r.value=n,!0):Reflect.set(e,t,n,o)}};function mt(e){return rt(e)?e:new Proxy(e,ht)}function vt(e){const t=E(e)?new Array(e.length):{};for(const n in e)t[n]=bt(e,n);return t}class gt{constructor(e,t){this._object=e,this._key=t,this.__v_isRef=!0}get value(){return this._object[this._key]}set value(e){this._object[this._key]=e}}function bt(e,t){return ut(e[t])?e[t]:new gt(e,t)}class yt{constructor(e,t,n){this._setter=t,this._dirty=!0,this.__v_isRef=!0,this.effect=se(e,{lazy:!0,scheduler:()=>{this._dirty||(this._dirty=!0,he(it(this),"set","value"))}}),this.__v_isReadonly=n}get value(){const e=it(this);return e._dirty&&(e._value=this.effect(),e._dirty=!1),fe(e,0,"value"),e._value}set value(e){this._setter(e)}}function xt(e,t,n,o){let r;try{r=o?e(...o):e()}catch(s){_t(s,t,n)}return r}function kt(e,t,n,o){if(O(e)){const r=xt(e,t,n,o);return r&&R(r)&&r.catch((e=>{_t(e,t,n)})),r}const r=[];for(let s=0;s>>1;Dt(Ct[e])-1?Ct.splice(t,0,e):Ct.push(e),Ft()}}function Ft(){wt||St||(St=!0,Tt=Ot.then(Wt))}function It(e,t,n,o){E(e)?n.push(...e):t&&t.includes(e,e.allowRecurse?o+1:o)||n.push(e),Ft()}function Ut(e,t=null){if(Bt.length){for(jt=t,Pt=[...new Set(Bt)],Bt.length=0,At=0;AtDt(e)-Dt(t))),$t=0;$tnull==e.id?1/0:e.id;function Wt(e){St=!1,wt=!0,Ut(e),Ct.sort(((e,t)=>Dt(e)-Dt(t)));try{for(Nt=0;Nte.trim())):t&&(r=n.map(Z))}let i,a=o[i=Q(t)]||o[i=Q(H(t))];!a&&s&&(a=o[i=Q(G(t))]),a&&kt(a,e,6,r);const c=o[i+"Once"];if(c){if(e.emitted){if(e.emitted[i])return}else e.emitted={};e.emitted[i]=!0,kt(c,e,6,r)}}function Ht(e,t,n=!1){const o=t.emitsCache,r=o.get(e);if(void 0!==r)return r;const s=e.emits;let l={},i=!1;if(!O(e)){const o=e=>{const n=Ht(e,t,!0);n&&(i=!0,N(l,n))};!n&&t.mixins.length&&t.mixins.forEach(o),e.extends&&o(e.extends),e.mixins&&e.mixins.forEach(o)}return s||i?(E(s)?s.forEach((e=>l[e]=null)):N(l,s),o.set(e,l),l):(o.set(e,null),null)}function qt(e,t){return!(!e||!S(t))&&(t=t.slice(2).replace(/Once$/,""),A(e,t[0].toLowerCase()+t.slice(1))||A(e,G(t))||A(e,t))}let Gt=null,Jt=null;function Qt(e){const t=Gt;return Gt=e,Jt=e&&e.type.__scopeId||null,t}function Kt(e){Jt=e}function Yt(){Jt=null}function Xt(e,t=Gt,n){if(!t)return e;if(e._n)return e;const o=(...n)=>{o._d&&Mo(-1);const r=Qt(t),s=e(...n);return Qt(r),o._d&&Mo(1),s};return o._n=!0,o._c=!0,o._d=!0,o}function Zt(e){const{type:t,vnode:n,proxy:o,withProxy:r,props:s,propsOptions:[l],slots:i,attrs:a,emit:c,render:u,renderCache:d,data:p,setupState:f,ctx:h,inheritAttrs:m}=e;let v;const g=Qt(e);try{let e;if(4&n.shapeFlag){const t=r||o;v=Jo(u.call(t,t,d,s,f,p,h)),e=a}else{const n=t;0,v=Jo(n.length>1?n(s,{attrs:a,slots:i,emit:c}):n(s,null)),e=t.props?a:en(a)}let g=v;if(e&&!1!==m){const t=Object.keys(e),{shapeFlag:n}=g;t.length&&(1&n||6&n)&&(l&&t.some(C)&&(e=tn(e,l)),g=zo(g,e))}0,n.dirs&&(g.dirs=g.dirs?g.dirs.concat(n.dirs):n.dirs),n.transition&&(g.transition=n.transition),v=g}catch(b){$o.length=0,_t(b,e,1),v=Wo(Eo)}return Qt(g),v}const en=e=>{let t;for(const n in e)("class"===n||"style"===n||S(n))&&((t||(t={}))[n]=e[n]);return t},tn=(e,t)=>{const n={};for(const o in e)C(o)&&o.slice(9)in t||(n[o]=e[o]);return n};function nn(e,t,n){const o=Object.keys(t);if(o.length!==Object.keys(e).length)return!0;for(let r=0;r1)return n&&O(t)?t.call(o.proxy):t}}const sn={};function ln(e,t,n){return an(e,t,n)}function an(e,t,{immediate:n,deep:o,flush:r,onTrack:s,onTrigger:l}=y,i=ir){let a,c,u=!1,d=!1;if(ut(e)?(a=()=>e.value,u=!!e._shallow):rt(e)?(a=()=>e,o=!0):E(e)?(d=!0,u=e.some(rt),a=()=>e.map((e=>ut(e)?e.value:rt(e)?dn(e):O(e)?xt(e,i,2):void 0))):a=O(e)?t?()=>xt(e,i,2):()=>{if(!i||!i.isUnmounted)return c&&c(),kt(e,i,3,[p])}:k,t&&o){const e=a;a=()=>dn(e())}let p=e=>{c=v.options.onStop=()=>{xt(e,i,4)}},f=d?[]:sn;const h=()=>{if(v.active)if(t){const e=v();(o||u||(d?e.some(((e,t)=>K(e,f[t]))):K(e,f)))&&(c&&c(),kt(t,i,3,[e,f===sn?void 0:f,p]),f=e)}else v()};let m;h.allowRecurse=!!t,m="sync"===r?h:"post"===r?()=>xo(h,i&&i.suspense):()=>{!i||i.isMounted?function(e){It(e,Pt,Bt,At)}(h):h()};const v=se(a,{lazy:!0,onTrack:s,onTrigger:l,scheduler:m});return mr(v,i),t?n?h():f=v():"post"===r?xo(v,i&&i.suspense):v(),()=>{le(v),i&&B(i.effects,v)}}function cn(e,t,n){const o=this.proxy,r=T(e)?e.includes(".")?un(o,e):()=>o[e]:e.bind(o,o);let s;return O(t)?s=t:(s=t.handler,n=t),an(r,s.bind(o),n,this)}function un(e,t){const n=t.split(".");return()=>{let t=e;for(let e=0;e{dn(e,t)}));else if(U(e))for(const n in e)dn(e[n],t);return e}const pn=[Function,Array];Boolean,Boolean;function fn(e,t){const{leavingVNodes:n}=e;let o=n.get(t.type);return o||(o=Object.create(null),n.set(t.type,o)),o}function hn(e,t,n,o){const{appear:r,mode:s,persisted:l=!1,onBeforeEnter:i,onEnter:a,onAfterEnter:c,onEnterCancelled:u,onBeforeLeave:d,onLeave:p,onAfterLeave:f,onLeaveCancelled:h,onBeforeAppear:m,onAppear:v,onAfterAppear:g,onAppearCancelled:b}=t,y=String(e.key),x=fn(n,e),k=(e,t)=>{e&&kt(e,o,9,t)},_={mode:s,persisted:l,beforeEnter(t){let o=i;if(!n.isMounted){if(!r)return;o=m||i}t._leaveCb&&t._leaveCb(!0);const s=x[y];s&&Io(e,s)&&s.el._leaveCb&&s.el._leaveCb(),k(o,[t])},enter(e){let t=a,o=c,s=u;if(!n.isMounted){if(!r)return;t=v||a,o=g||c,s=b||u}let l=!1;const i=e._enterCb=t=>{l||(l=!0,k(t?s:o,[e]),_.delayedLeave&&_.delayedLeave(),e._enterCb=void 0)};t?(t(e,i),t.length<=1&&i()):i()},leave(t,o){const r=String(e.key);if(t._enterCb&&t._enterCb(!0),n.isUnmounting)return o();k(d,[t]);let s=!1;const l=t._leaveCb=n=>{s||(s=!0,o(),k(n?h:f,[t]),t._leaveCb=void 0,x[r]===e&&delete x[r])};x[r]=e,p?(p(t,l),p.length<=1&&l()):l()},clone:e=>hn(e,t,n,o)};return _}function mn(e){if(wn(e))return(e=zo(e)).children=null,e}function vn(e){return wn(e)?e.children?e.children[0]:void 0:e}function gn(e,t){6&e.shapeFlag&&e.component?gn(e.component.subTree,t):128&e.shapeFlag?(e.ssContent.transition=t.clone(e.ssContent),e.ssFallback.transition=t.clone(e.ssFallback)):e.transition=t}function bn(e,t=!1){let n=[],o=0;for(let r=0;r1)for(let r=0;r!!e.type.__asyncLoader;function kn(e){O(e)&&(e={loader:e});const{loader:t,loadingComponent:n,errorComponent:o,delay:r=200,timeout:s,suspensible:l=!0,onError:i}=e;let a,c=null,u=0;const d=()=>{let e;return c||(e=c=t().catch((e=>{if(e=e instanceof Error?e:new Error(String(e)),i)return new Promise(((t,n)=>{i(e,(()=>t((u++,c=null,d()))),(()=>n(e)),u+1)}));throw e})).then((t=>e!==c&&c?c:(t&&(t.__esModule||"Module"===t[Symbol.toStringTag])&&(t=t.default),a=t,t))))};return yn({name:"AsyncComponentWrapper",__asyncLoader:d,get __asyncResolved(){return a},setup(){const e=ir;if(a)return()=>_n(a,e);const t=t=>{c=null,_t(t,e,13,!o)};if(l&&e.suspense)return d().then((t=>()=>_n(t,e))).catch((e=>(t(e),()=>o?Wo(o,{error:e}):null)));const i=dt(!1),u=dt(),p=dt(!!r);return r&&setTimeout((()=>{p.value=!1}),r),null!=s&&setTimeout((()=>{if(!i.value&&!u.value){const e=new Error(`Async component timed out after ${s}ms.`);t(e),u.value=e}}),s),d().then((()=>{i.value=!0,e.parent&&wn(e.parent.vnode)&&Rt(e.parent.update)})).catch((e=>{t(e),u.value=e})),()=>i.value&&a?_n(a,e):u.value&&o?Wo(o,{error:u.value}):n&&!p.value?Wo(n):void 0}})}function _n(e,{vnode:{ref:t,props:n,children:o}}){const r=Wo(e,n,o);return r.ref=t,r}const wn=e=>e.type.__isKeepAlive;function Sn(e,t){Nn(e,"a",t)}function Cn(e,t){Nn(e,"da",t)}function Nn(e,t,n=ir){const o=e.__wdc||(e.__wdc=()=>{let t=n;for(;t;){if(t.isDeactivated)return;t=t.parent}e()});if(Pn(t,o,n),n){let e=n.parent;for(;e&&e.parent;)wn(e.parent.vnode)&&Bn(o,t,n,e),e=e.parent}}function Bn(e,t,n,o){const r=Pn(t,e,o,!0);jn((()=>{B(o[t],r)}),n)}function Pn(e,t,n=ir,o=!1){if(n){const r=n[e]||(n[e]=[]),s=t.__weh||(t.__weh=(...o)=>{if(n.isUnmounted)return;de(),cr(n);const r=kt(t,n,e,o);return cr(null),pe(),r});return o?r.unshift(s):r.push(s),s}}const An=e=>(t,n=ir)=>(!dr||"sp"===e)&&Pn(e,t,n),En=An("bm"),Ln=An("m"),$n=An("bu"),On=An("u"),Tn=An("bum"),jn=An("um"),Mn=An("sp"),Rn=An("rtg"),Fn=An("rtc");function In(e,t=ir){Pn("ec",e,t)}let Un=!0;function Vn(e){const t=zn(e),n=e.proxy,o=e.ctx;Un=!1,t.beforeCreate&&Dn(t.beforeCreate,e,"bc");const{data:r,computed:s,methods:l,watch:i,provide:a,inject:c,created:u,beforeMount:d,mounted:p,beforeUpdate:f,updated:h,activated:m,deactivated:v,beforeDestroy:g,beforeUnmount:b,destroyed:y,unmounted:x,render:_,renderTracked:w,renderTriggered:S,errorCaptured:C,serverPrefetch:N,expose:B,inheritAttrs:P,components:A,directives:L,filters:$}=t;if(c&&function(e,t,n=k){E(e)&&(e=Jn(e));for(const o in e){const n=e[o];M(n)?t[o]="default"in n?rn(n.from||o,n.default,!0):rn(n.from||o):t[o]=rn(n)}}(c,o,null),l)for(const k in l){const e=l[k];O(e)&&(o[k]=e.bind(n))}if(r){const t=r.call(n,n);M(t)&&(e.data=tt(t))}if(Un=!0,s)for(const E in s){const e=s[E],t=gr({get:O(e)?e.bind(n,n):O(e.get)?e.get.bind(n,n):k,set:!O(e)&&O(e.set)?e.set.bind(n):k});Object.defineProperty(o,E,{enumerable:!0,configurable:!0,get:()=>t.value,set:e=>t.value=e})}if(i)for(const k in i)Wn(i[k],o,n,k);if(a){const e=O(a)?a.call(n):a;Reflect.ownKeys(e).forEach((t=>{!function(e,t){if(ir){let n=ir.provides;const o=ir.parent&&ir.parent.provides;o===n&&(n=ir.provides=Object.create(o)),n[e]=t}}(t,e[t])}))}function T(e,t){E(t)?t.forEach((t=>e(t.bind(n)))):t&&e(t.bind(n))}if(u&&Dn(u,e,"c"),T(En,d),T(Ln,p),T($n,f),T(On,h),T(Sn,m),T(Cn,v),T(In,C),T(Fn,w),T(Rn,S),T(Tn,b),T(jn,x),T(Mn,N),E(B))if(B.length){const t=e.exposed||(e.exposed={});B.forEach((e=>{Object.defineProperty(t,e,{get:()=>n[e],set:t=>n[e]=t})}))}else e.exposed||(e.exposed={});_&&e.render===k&&(e.render=_),null!=P&&(e.inheritAttrs=P),A&&(e.components=A),L&&(e.directives=L)}function Dn(e,t,n){kt(E(e)?e.map((e=>e.bind(t.proxy))):e.bind(t.proxy),t,n)}function Wn(e,t,n,o){const r=o.includes(".")?un(n,o):()=>n[o];if(T(e)){const n=t[e];O(n)&&ln(r,n)}else if(O(e))ln(r,e.bind(n));else if(M(e))if(E(e))e.forEach((e=>Wn(e,t,n,o)));else{const o=O(e.handler)?e.handler.bind(n):t[e.handler];O(o)&&ln(r,o,e)}}function zn(e){const t=e.type,{mixins:n,extends:o}=t,{mixins:r,optionsCache:s,config:{optionMergeStrategies:l}}=e.appContext,i=s.get(t);let a;return i?a=i:r.length||n||o?(a={},r.length&&r.forEach((e=>Hn(a,e,l,!0))),Hn(a,t,l)):a=t,s.set(t,a),a}function Hn(e,t,n,o=!1){const{mixins:r,extends:s}=t;s&&Hn(e,s,n,!0),r&&r.forEach((t=>Hn(e,t,n,!0)));for(const l in t)if(o&&"expose"===l);else{const o=qn[l]||n&&n[l];e[l]=o?o(e[l],t[l]):t[l]}return e}const qn={data:Gn,props:Kn,emits:Kn,methods:Kn,computed:Kn,beforeCreate:Qn,created:Qn,beforeMount:Qn,mounted:Qn,beforeUpdate:Qn,updated:Qn,beforeDestroy:Qn,destroyed:Qn,activated:Qn,deactivated:Qn,errorCaptured:Qn,serverPrefetch:Qn,components:Kn,directives:Kn,watch:function(e,t){if(!e)return t;if(!t)return e;const n=N(Object.create(null),e);for(const o in t)n[o]=Qn(e[o],t[o]);return n},provide:Gn,inject:function(e,t){return Kn(Jn(e),Jn(t))}};function Gn(e,t){return t?e?function(){return N(O(e)?e.call(this,this):e,O(t)?t.call(this,this):t)}:t:e}function Jn(e){if(E(e)){const t={};for(let n=0;n{a=!0;const[n,o]=eo(e,t,!0);N(l,n),o&&i.push(...o)};!n&&t.mixins.length&&t.mixins.forEach(o),e.extends&&o(e.extends),e.mixins&&e.mixins.forEach(o)}if(!s&&!a)return o.set(e,x),x;if(E(s))for(let u=0;u-1,n[1]=o<0||t-1||A(n,"default"))&&i.push(e)}}}const c=[l,i];return o.set(e,c),c}function to(e){return"$"!==e[0]}function no(e){const t=e&&e.toString().match(/^\s*function (\w+)/);return t?t[1]:""}function oo(e,t){return no(e)===no(t)}function ro(e,t){return E(t)?t.findIndex((t=>oo(t,e))):O(t)&&oo(t,e)?0:-1}const so=e=>"_"===e[0]||"$stable"===e,lo=e=>E(e)?e.map(Jo):[Jo(e)],io=(e,t,n)=>{const o=Xt((e=>lo(t(e))),n);return o._c=!1,o},ao=(e,t,n)=>{const o=e._ctx;for(const r in e){if(so(r))continue;const n=e[r];if(O(n))t[r]=io(0,n,o);else if(null!=n){const e=lo(n);t[r]=()=>e}}},co=(e,t)=>{const n=lo(t);e.slots.default=()=>n};function uo(e,t,n,o){const r=e.dirs,s=t&&t.dirs;for(let l=0;l(s.has(e)||(e&&O(e.install)?(s.add(e),e.install(i,...t)):O(e)&&(s.add(e),e(i,...t))),i),mixin:e=>(r.mixins.includes(e)||r.mixins.push(e),i),component:(e,t)=>t?(r.components[e]=t,i):r.components[e],directive:(e,t)=>t?(r.directives[e]=t,i):r.directives[e],mount(s,a,c){if(!l){const u=Wo(n,o);return u.appContext=r,a&&t?t(u,s):e(u,s,c),l=!0,i._container=s,s.__vue_app__=i,u.component.proxy}},unmount(){l&&(e(null,i._container),delete i._container.__vue_app__)},provide:(e,t)=>(r.provides[e]=t,i)};return i}}let mo=!1;const vo=e=>/svg/.test(e.namespaceURI)&&"foreignObject"!==e.tagName,go=e=>8===e.nodeType;function bo(e){const{mt:t,p:n,o:{patchProp:o,nextSibling:r,parentNode:s,remove:l,insert:i,createComment:a}}=e,c=(n,o,l,i,a,m=!1)=>{const v=go(n)&&"["===n.data,g=()=>f(n,o,l,i,a,v),{type:b,ref:y,shapeFlag:x}=o,k=n.nodeType;o.el=n;let _=null;switch(b){case Ao:3!==k?_=g():(n.data!==o.children&&(mo=!0,n.data=o.children),_=r(n));break;case Eo:_=8!==k||v?g():r(n);break;case Lo:if(1===k){_=n;const e=!o.children.length;for(let t=0;t{i=i||!!t.dynamicChildren;const{type:a,props:c,patchFlag:u,shapeFlag:p,dirs:f}=t,h="input"===a&&f||"option"===a;if(h||-1!==u){if(f&&uo(t,null,n,"created"),c)if(h||!i||16&u||32&u)for(const t in c)(h&&t.endsWith("value")||S(t)&&!D(t))&&o(e,t,null,c[t]);else c.onClick&&o(e,"onClick",null,c.onClick);let a;if((a=c&&c.onVnodeBeforeMount)&&wo(a,n,t),f&&uo(t,null,n,"beforeMount"),((a=c&&c.onVnodeMounted)||f)&&on((()=>{a&&wo(a,n,t),f&&uo(t,null,n,"mounted")}),r),16&p&&(!c||!c.innerHTML&&!c.textContent)){let o=d(e.firstChild,t,e,n,r,s,i);for(;o;){mo=!0;const e=o;o=o.nextSibling,l(e)}}else 8&p&&e.textContent!==t.children&&(mo=!0,e.textContent=t.children)}return e.nextSibling},d=(e,t,o,r,s,l,i)=>{i=i||!!t.dynamicChildren;const a=t.children,u=a.length;for(let d=0;d{const{slotScopeIds:u}=t;u&&(l=l?l.concat(u):u);const p=s(e),f=d(r(e),t,p,n,o,l,c);return f&&go(f)&&"]"===f.data?r(t.anchor=f):(mo=!0,i(t.anchor=a("]"),p,f),f)},f=(e,t,o,i,a,c)=>{if(mo=!0,t.el=null,c){const t=h(e);for(;;){const n=r(e);if(!n||n===t)break;l(n)}}const u=r(e),d=s(e);return l(e),n(null,t,d,u,o,i,vo(d),a),u},h=e=>{let t=0;for(;e;)if((e=r(e))&&go(e)&&("["===e.data&&t++,"]"===e.data)){if(0===t)return r(e);t--}return e};return[(e,t)=>{if(!t.hasChildNodes())return n(null,e,t),void Vt();mo=!1,c(t.firstChild,e,null,null,null),Vt(),mo&&console.error("Hydration completed but contains mismatches.")},c]}const yo={scheduler:Rt,allowRecurse:!0},xo=on,ko=(e,t,n,o,r=!1)=>{if(E(e))return void e.forEach(((e,s)=>ko(e,t&&(E(t)?t[s]:t),n,o,r)));if(xn(o)&&!r)return;const s=4&o.shapeFlag?hr(o.component)||o.component.proxy:o.el,l=r?null:s,{i:i,r:a}=e,c=t&&t.r,u=i.refs===y?i.refs={}:i.refs,d=i.setupState;if(null!=c&&c!==a&&(T(c)?(u[c]=null,A(d,c)&&(d[c]=null)):ut(c)&&(c.value=null)),T(a)){const e=()=>{u[a]=l,A(d,a)&&(d[a]=l)};l?(e.id=-1,xo(e,n)):e()}else if(ut(a)){const e=()=>{a.value=l};l?(e.id=-1,xo(e,n)):e()}else O(a)&&xt(a,i,12,[l,u])};function _o(e){return function(e,t){const{insert:n,remove:o,patchProp:r,forcePatchProp:s,createElement:l,createText:i,createComment:a,setText:c,setElementText:u,parentNode:d,nextSibling:p,setScopeId:f=k,cloneNode:h,insertStaticContent:m}=e,v=(e,t,n,o=null,r=null,s=null,l=!1,i=null,a=!!t.dynamicChildren)=>{e&&!Io(e,t)&&(o=te(e),J(e,r,s,!0),e=null),-2===t.patchFlag&&(a=!1,t.dynamicChildren=null);const{type:c,ref:u,shapeFlag:d}=t;switch(c){case Ao:g(e,t,n,o);break;case Eo:b(e,t,n,o);break;case Lo:null==e&&_(t,n,o,l);break;case Po:T(e,t,n,o,r,s,l,i,a);break;default:1&d?C(e,t,n,o,r,s,l,i,a):6&d?j(e,t,n,o,r,s,l,i,a):(64&d||128&d)&&c.process(e,t,n,o,r,s,l,i,a,oe)}null!=u&&r&&ko(u,e&&e.ref,s,t||e,!t)},g=(e,t,o,r)=>{if(null==e)n(t.el=i(t.children),o,r);else{const n=t.el=e.el;t.children!==e.children&&c(n,t.children)}},b=(e,t,o,r)=>{null==e?n(t.el=a(t.children||""),o,r):t.el=e.el},_=(e,t,n,o)=>{[e.el,e.anchor]=m(e.children,t,n,o)},w=({el:e,anchor:t},o,r)=>{let s;for(;e&&e!==t;)s=p(e),n(e,o,r),e=s;n(t,o,r)},S=({el:e,anchor:t})=>{let n;for(;e&&e!==t;)n=p(e),o(e),e=n;o(t)},C=(e,t,n,o,r,s,l,i,a)=>{l=l||"svg"===t.type,null==e?B(t,n,o,r,s,l,i,a):L(e,t,r,s,l,i,a)},B=(e,t,o,s,i,a,c,d)=>{let p,f;const{type:m,props:v,shapeFlag:g,transition:b,patchFlag:y,dirs:x}=e;if(e.el&&void 0!==h&&-1===y)p=e.el=h(e.el);else{if(p=e.el=l(e.type,a,v&&v.is,v),8&g?u(p,e.children):16&g&&E(e.children,p,null,s,i,a&&"foreignObject"!==m,c,d),x&&uo(e,null,s,"created"),v){for(const t in v)D(t)||r(p,t,null,v[t],a,e.children,s,i,ee);(f=v.onVnodeBeforeMount)&&wo(f,s,e)}P(p,e,e.scopeId,c,s)}x&&uo(e,null,s,"beforeMount");const k=(!i||i&&!i.pendingBranch)&&b&&!b.persisted;k&&b.beforeEnter(p),n(p,t,o),((f=v&&v.onVnodeMounted)||k||x)&&xo((()=>{f&&wo(f,s,e),k&&b.enter(p),x&&uo(e,null,s,"mounted")}),i)},P=(e,t,n,o,r)=>{if(n&&f(e,n),o)for(let s=0;s{for(let c=a;c{const c=t.el=e.el;let{patchFlag:d,dynamicChildren:p,dirs:f}=t;d|=16&e.patchFlag;const h=e.props||y,m=t.props||y;let v;if((v=m.onVnodeBeforeUpdate)&&wo(v,n,t,e),f&&uo(t,e,n,"beforeUpdate"),d>0){if(16&d)O(c,t,h,m,n,o,l);else if(2&d&&h.class!==m.class&&r(c,"class",null,m.class,l),4&d&&r(c,"style",h.style,m.style,l),8&d){const i=t.dynamicProps;for(let t=0;t{v&&wo(v,n,t,e),f&&uo(t,e,n,"updated")}),o)},$=(e,t,n,o,r,s,l)=>{for(let i=0;i{if(n!==o){for(const c in o){if(D(c))continue;const u=o[c],d=n[c];(u!==d||s&&s(e,c))&&r(e,c,d,u,a,t.children,l,i,ee)}if(n!==y)for(const s in n)D(s)||s in o||r(e,s,n[s],null,a,t.children,l,i,ee)}},T=(e,t,o,r,s,l,a,c,u)=>{const d=t.el=e?e.el:i(""),p=t.anchor=e?e.anchor:i("");let{patchFlag:f,dynamicChildren:h,slotScopeIds:m}=t;h&&(u=!0),m&&(c=c?c.concat(m):m),null==e?(n(d,o,r),n(p,o,r),E(t.children,o,p,s,l,a,c,u)):f>0&&64&f&&h&&e.dynamicChildren?($(e.dynamicChildren,h,o,s,l,a,c),(null!=t.key||s&&t===s.subTree)&&So(e,t,!0)):V(e,t,o,p,s,l,a,c,u)},j=(e,t,n,o,r,s,l,i,a)=>{t.slotScopeIds=i,null==e?512&t.shapeFlag?r.ctx.activate(t,n,o,l,a):M(t,n,o,r,s,l,a):F(e,t,a)},M=(e,t,n,o,r,s,l)=>{const i=e.component=function(e,t,n){const o=e.type,r=(t?t.appContext:e.appContext)||sr,s={uid:lr++,vnode:e,type:o,parent:t,appContext:r,root:null,next:null,subTree:null,update:null,render:null,proxy:null,exposed:null,exposeProxy:null,withProxy:null,effects:null,provides:t?t.provides:Object.create(r.provides),accessCache:null,renderCache:[],components:null,directives:null,propsOptions:eo(o,r),emitsOptions:Ht(o,r),emit:null,emitted:null,propsDefaults:y,inheritAttrs:o.inheritAttrs,ctx:y,data:y,props:y,attrs:y,slots:y,refs:y,setupState:y,setupContext:null,suspense:n,suspenseId:n?n.pendingId:0,asyncDep:null,asyncResolved:!1,isMounted:!1,isUnmounted:!1,isDeactivated:!1,bc:null,c:null,bm:null,m:null,bu:null,u:null,um:null,bum:null,da:null,a:null,rtg:null,rtc:null,ec:null,sp:null};return s.ctx={_:s},s.root=t?t.root:s,s.emit=zt.bind(null,s),s}(e,o,r);if(wn(e)&&(i.ctx.renderer=oe),function(e,t=!1){dr=t;const{props:n,children:o}=e.vnode,r=ur(e);Yn(e,n,r,t),((e,t)=>{if(32&e.vnode.shapeFlag){const n=t._;n?(e.slots=it(t),X(t,"_",n)):ao(t,e.slots={})}else e.slots={},t&&co(e,t);X(e.slots,Uo,1)})(e,o);const s=r?function(e,t){const n=e.type;e.accessCache=Object.create(null),e.proxy=at(new Proxy(e.ctx,or));const{setup:o}=n;if(o){const n=e.setupContext=o.length>1?function(e){const t=t=>{e.exposed=t||{}};return{attrs:e.attrs,slots:e.slots,emit:e.emit,expose:t}}(e):null;ir=e,de();const r=xt(o,e,0,[e.props,n]);if(pe(),ir=null,R(r)){const n=()=>{ir=null};if(r.then(n,n),t)return r.then((t=>{pr(e,t)})).catch((t=>{_t(t,e,0)}));e.asyncDep=r}else pr(e,r)}else fr(e)}(e,t):void 0;dr=!1}(i),i.asyncDep){if(r&&r.registerDep(i,I),!e.el){const e=i.subTree=Wo(Eo);b(null,e,t,n)}}else I(i,e,t,n,r,s,l)},F=(e,t,n)=>{const o=t.component=e.component;if(function(e,t,n){const{props:o,children:r,component:s}=e,{props:l,children:i,patchFlag:a}=t,c=s.emitsOptions;if(t.dirs||t.transition)return!0;if(!(n&&a>=0))return!(!r&&!i||i&&i.$stable)||o!==l&&(o?!l||nn(o,l,c):!!l);if(1024&a)return!0;if(16&a)return o?nn(o,l,c):!!l;if(8&a){const e=t.dynamicProps;for(let t=0;tNt&&Ct.splice(t,1)}(o.update),o.update()}else t.component=e.component,t.el=e.el,o.vnode=t},I=(e,t,n,o,r,s,l)=>{e.update=se((function(){if(e.isMounted){let t,{next:n,bu:o,u:i,parent:a,vnode:c}=e,u=n;n?(n.el=c.el,U(e,n,l)):n=c,o&&Y(o),(t=n.props&&n.props.onVnodeBeforeUpdate)&&wo(t,a,n,c);const p=Zt(e),f=e.subTree;e.subTree=p,v(f,p,d(f.el),te(f),e,r,s),n.el=p.el,null===u&&function({vnode:e,parent:t},n){for(;t&&t.subTree===e;)(e=t.vnode).el=n,t=t.parent}(e,p.el),i&&xo(i,r),(t=n.props&&n.props.onVnodeUpdated)&&xo((()=>wo(t,a,n,c)),r)}else{let l;const{el:i,props:a}=t,{bm:c,m:u,parent:d}=e;if(c&&Y(c),(l=a&&a.onVnodeBeforeMount)&&wo(l,d,t),i&&ie){const n=()=>{e.subTree=Zt(e),ie(i,e.subTree,e,r,null)};xn(t)?t.type.__asyncLoader().then((()=>!e.isUnmounted&&n())):n()}else{const l=e.subTree=Zt(e);v(null,l,n,o,e,r,s),t.el=l.el}if(u&&xo(u,r),l=a&&a.onVnodeMounted){const e=t;xo((()=>wo(l,d,e)),r)}256&t.shapeFlag&&e.a&&xo(e.a,r),e.isMounted=!0,t=n=o=null}}),yo)},U=(e,t,n)=>{t.component=e;const o=e.vnode.props;e.vnode=t,e.next=null,function(e,t,n,o){const{props:r,attrs:s,vnode:{patchFlag:l}}=e,i=it(r),[a]=e.propsOptions;let c=!1;if(!(o||l>0)||16&l){let o;Xn(e,t,r,s)&&(c=!0);for(const s in i)t&&(A(t,s)||(o=G(s))!==s&&A(t,o))||(a?!n||void 0===n[s]&&void 0===n[o]||(r[s]=Zn(a,i,s,void 0,e,!0)):delete r[s]);if(s!==i)for(const e in s)t&&A(t,e)||(delete s[e],c=!0)}else if(8&l){const n=e.vnode.dynamicProps;for(let o=0;o{const{vnode:o,slots:r}=e;let s=!0,l=y;if(32&o.shapeFlag){const e=t._;e?n&&1===e?s=!1:(N(r,t),n||1!==e||delete r._):(s=!t.$stable,ao(t,r)),l=t}else t&&(co(e,t),l={default:1});if(s)for(const i in r)so(i)||i in l||delete r[i]})(e,t.children,n),de(),Ut(void 0,e.update),pe()},V=(e,t,n,o,r,s,l,i,a=!1)=>{const c=e&&e.children,d=e?e.shapeFlag:0,p=t.children,{patchFlag:f,shapeFlag:h}=t;if(f>0){if(128&f)return void z(c,p,n,o,r,s,l,i,a);if(256&f)return void W(c,p,n,o,r,s,l,i,a)}8&h?(16&d&&ee(c,r,s),p!==c&&u(n,p)):16&d?16&h?z(c,p,n,o,r,s,l,i,a):ee(c,r,s,!0):(8&d&&u(n,""),16&h&&E(p,n,o,r,s,l,i,a))},W=(e,t,n,o,r,s,l,i,a)=>{t=t||x;const c=(e=e||x).length,u=t.length,d=Math.min(c,u);let p;for(p=0;pu?ee(e,r,s,!0,!1,d):E(t,n,o,r,s,l,i,a,d)},z=(e,t,n,o,r,s,l,i,a)=>{let c=0;const u=t.length;let d=e.length-1,p=u-1;for(;c<=d&&c<=p;){const o=e[c],u=t[c]=a?Qo(t[c]):Jo(t[c]);if(!Io(o,u))break;v(o,u,n,null,r,s,l,i,a),c++}for(;c<=d&&c<=p;){const o=e[d],c=t[p]=a?Qo(t[p]):Jo(t[p]);if(!Io(o,c))break;v(o,c,n,null,r,s,l,i,a),d--,p--}if(c>d){if(c<=p){const e=p+1,d=ep)for(;c<=d;)J(e[c],r,s,!0),c++;else{const f=c,h=c,m=new Map;for(c=h;c<=p;c++){const e=t[c]=a?Qo(t[c]):Jo(t[c]);null!=e.key&&m.set(e.key,c)}let g,b=0;const y=p-h+1;let k=!1,_=0;const w=new Array(y);for(c=0;c=y){J(o,r,s,!0);continue}let u;if(null!=o.key)u=m.get(o.key);else for(g=h;g<=p;g++)if(0===w[g-h]&&Io(o,t[g])){u=g;break}void 0===u?J(o,r,s,!0):(w[u-h]=c+1,u>=_?_=u:k=!0,v(o,t[u],n,null,r,s,l,i,a),b++)}const S=k?function(e){const t=e.slice(),n=[0];let o,r,s,l,i;const a=e.length;for(o=0;o0&&(t[o]=n[s-1]),n[s]=o)}}s=n.length,l=n[s-1];for(;s-- >0;)n[s]=l,l=t[l];return n}(w):x;for(g=S.length-1,c=y-1;c>=0;c--){const e=h+c,d=t[e],p=e+1{const{el:l,type:i,transition:a,children:c,shapeFlag:u}=e;if(6&u)return void q(e.component.subTree,t,o,r);if(128&u)return void e.suspense.move(t,o,r);if(64&u)return void i.move(e,t,o,oe);if(i===Po){n(l,t,o);for(let e=0;ea.enter(l)),s);else{const{leave:e,delayLeave:r,afterLeave:s}=a,i=()=>n(l,t,o),c=()=>{e(l,(()=>{i(),s&&s()}))};r?r(l,i,c):c()}else n(l,t,o)},J=(e,t,n,o=!1,r=!1)=>{const{type:s,props:l,ref:i,children:a,dynamicChildren:c,shapeFlag:u,patchFlag:d,dirs:p}=e;if(null!=i&&ko(i,null,n,e,!0),256&u)return void t.ctx.deactivate(e);const f=1&u&&p;let h;if((h=l&&l.onVnodeBeforeUnmount)&&wo(h,t,e),6&u)Z(e.component,n,o);else{if(128&u)return void e.suspense.unmount(n,o);f&&uo(e,null,t,"beforeUnmount"),64&u?e.type.remove(e,t,n,r,oe,o):c&&(s!==Po||d>0&&64&d)?ee(c,t,n,!1,!0):(s===Po&&(128&d||256&d)||!r&&16&u)&&ee(a,t,n),o&&Q(e)}((h=l&&l.onVnodeUnmounted)||f)&&xo((()=>{h&&wo(h,t,e),f&&uo(e,null,t,"unmounted")}),n)},Q=e=>{const{type:t,el:n,anchor:r,transition:s}=e;if(t===Po)return void K(n,r);if(t===Lo)return void S(e);const l=()=>{o(n),s&&!s.persisted&&s.afterLeave&&s.afterLeave()};if(1&e.shapeFlag&&s&&!s.persisted){const{leave:t,delayLeave:o}=s,r=()=>t(n,l);o?o(e.el,l,r):r()}else l()},K=(e,t)=>{let n;for(;e!==t;)n=p(e),o(e),e=n;o(t)},Z=(e,t,n)=>{const{bum:o,effects:r,update:s,subTree:l,um:i}=e;if(o&&Y(o),r)for(let a=0;a{e.isUnmounted=!0}),t),t&&t.pendingBranch&&!t.isUnmounted&&e.asyncDep&&!e.asyncResolved&&e.suspenseId===t.pendingId&&(t.deps--,0===t.deps&&t.resolve())},ee=(e,t,n,o=!1,r=!1,s=0)=>{for(let l=s;l6&e.shapeFlag?te(e.component.subTree):128&e.shapeFlag?e.suspense.next():p(e.anchor||e.el),ne=(e,t,n)=>{null==e?t._vnode&&J(t._vnode,null,null,!0):v(t._vnode||null,e,t,null,null,null,n),Vt(),t._vnode=e},oe={p:v,um:J,m:q,r:Q,mt:M,mc:E,pc:V,pbc:$,n:te,o:e};let re,ie;t&&([re,ie]=t(oe));return{render:ne,hydrate:re,createApp:ho(ne,re)}}(e,bo)}function wo(e,t,n,o=null){kt(e,t,7,[n,o])}function So(e,t,n=!1){const o=e.children,r=t.children;if(E(o)&&E(r))for(let s=0;s0?Oo||x:null,$o.pop(),Oo=$o[$o.length-1]||null,jo>0&&Oo&&Oo.push(s),s}function Fo(e){return!!e&&!0===e.__v_isVNode}function Io(e,t){return e.type===t.type&&e.key===t.key}const Uo="__vInternal",Vo=({key:e})=>null!=e?e:null,Do=({ref:e})=>null!=e?T(e)||ut(e)||O(e)?{i:Gt,r:e}:e:null,Wo=function(e,t=null,n=null,o=0,r=null,s=!1){e&&e!==No||(e=Eo);if(Fo(e)){const o=zo(e,t,!0);return n&&Ko(o,n),o}l=e,O(l)&&"__vccOpts"in l&&(e=e.__vccOpts);var l;if(t){(lt(t)||Uo in t)&&(t=N({},t));let{class:e,style:n}=t;e&&!T(e)&&(t.class=v(e)),M(n)&&(lt(n)&&!E(n)&&(n=N({},n)),t.style=p(n))}const i=T(e)?1:(e=>e.__isSuspense)(e)?128:(e=>e.__isTeleport)(e)?64:M(e)?4:O(e)?2:0,a={__v_isVNode:!0,__v_skip:!0,type:e,props:t,key:t&&Vo(t),ref:t&&Do(t),scopeId:Jt,slotScopeIds:null,children:null,component:null,suspense:null,ssContent:null,ssFallback:null,dirs:null,transition:null,el:null,anchor:null,target:null,targetAnchor:null,shapeFlag:i,patchFlag:o,dynamicProps:r,dynamicChildren:null,appContext:null};Ko(a,n),128&i&&e.normalize(a);jo>0&&!s&&Oo&&(o>0||6&i)&&32!==o&&Oo.push(a);return a};function zo(e,t,n=!1){const{props:o,ref:r,patchFlag:s,children:l}=e,i=t?Yo(o||{},t):o;return{__v_isVNode:!0,__v_skip:!0,type:e.type,props:i,key:i&&Vo(i),ref:t&&t.ref?n&&r?E(r)?r.concat(Do(t)):[r,Do(t)]:Do(t):r,scopeId:e.scopeId,slotScopeIds:e.slotScopeIds,children:l,target:e.target,targetAnchor:e.targetAnchor,staticCount:e.staticCount,shapeFlag:e.shapeFlag,patchFlag:t&&e.type!==Po?-1===s?16:16|s:s,dynamicProps:e.dynamicProps,dynamicChildren:e.dynamicChildren,appContext:e.appContext,dirs:e.dirs,transition:e.transition,component:e.component,suspense:e.suspense,ssContent:e.ssContent&&zo(e.ssContent),ssFallback:e.ssFallback&&zo(e.ssFallback),el:e.el,anchor:e.anchor}}function Ho(e=" ",t=0){return Wo(Ao,null,e,t)}function qo(e,t){const n=Wo(Lo,null,e);return n.staticCount=t,n}function Go(e="",t=!1){return t?(To(),Ro(Eo,null,e)):Wo(Eo,null,e)}function Jo(e){return null==e||"boolean"==typeof e?Wo(Eo):E(e)?Wo(Po,null,e.slice()):"object"==typeof e?Qo(e):Wo(Ao,null,String(e))}function Qo(e){return null===e.el?e:zo(e)}function Ko(e,t){let n=0;const{shapeFlag:o}=e;if(null==t)t=null;else if(E(t))n=16;else if("object"==typeof t){if(1&o||64&o){const n=t.default;return void(n&&(n._c&&(n._d=!1),Ko(e,n()),n._c&&(n._d=!0)))}{n=32;const o=t._;o||Uo in t?3===o&&Gt&&(1===Gt.slots._?t._=1:(t._=2,e.patchFlag|=1024)):t._ctx=Gt}}else O(t)?(t={default:t,_ctx:Gt},n=32):(t=String(t),64&o?(n=16,t=[Ho(t)]):n=8);e.children=t,e.shapeFlag|=n}function Yo(...e){const t=N({},e[0]);for(let n=1;n!Fo(e)||e.type!==Eo&&!(e.type===Po&&!er(e.children))))?e:null}const tr=e=>e?ur(e)?hr(e)||e.proxy:tr(e.parent):null,nr=N(Object.create(null),{$:e=>e,$el:e=>e.vnode.el,$data:e=>e.data,$props:e=>e.props,$attrs:e=>e.attrs,$slots:e=>e.slots,$refs:e=>e.refs,$parent:e=>tr(e.parent),$root:e=>tr(e.root),$emit:e=>e.emit,$options:e=>zn(e),$forceUpdate:e=>()=>Rt(e.update),$nextTick:e=>Mt.bind(e.proxy),$watch:e=>cn.bind(e)}),or={get({_:e},t){const{ctx:n,setupState:o,data:r,props:s,accessCache:l,type:i,appContext:a}=e;let c;if("$"!==t[0]){const i=l[t];if(void 0!==i)switch(i){case 0:return o[t];case 1:return r[t];case 3:return n[t];case 2:return s[t]}else{if(o!==y&&A(o,t))return l[t]=0,o[t];if(r!==y&&A(r,t))return l[t]=1,r[t];if((c=e.propsOptions[0])&&A(c,t))return l[t]=2,s[t];if(n!==y&&A(n,t))return l[t]=3,n[t];Un&&(l[t]=4)}}const u=nr[t];let d,p;return u?("$attrs"===t&&fe(e,0,t),u(e)):(d=i.__cssModules)&&(d=d[t])?d:n!==y&&A(n,t)?(l[t]=3,n[t]):(p=a.config.globalProperties,A(p,t)?p[t]:void 0)},set({_:e},t,n){const{data:o,setupState:r,ctx:s}=e;if(r!==y&&A(r,t))r[t]=n;else if(o!==y&&A(o,t))o[t]=n;else if(A(e.props,t))return!1;return("$"!==t[0]||!(t.slice(1)in e))&&(s[t]=n,!0)},has({_:{data:e,setupState:t,accessCache:n,ctx:o,appContext:r,propsOptions:s}},l){let i;return void 0!==n[l]||e!==y&&A(e,l)||t!==y&&A(t,l)||(i=s[0])&&A(i,l)||A(o,l)||A(nr,l)||A(r.config.globalProperties,l)}},rr=N({},or,{get(e,t){if(t!==Symbol.unscopables)return or.get(e,t,e)},has:(e,t)=>"_"!==t[0]&&!u(t)}),sr=po();let lr=0;let ir=null;const ar=()=>ir||Gt,cr=e=>{ir=e};function ur(e){return 4&e.vnode.shapeFlag}let dr=!1;function pr(e,t,n){O(t)?e.render=t:M(t)&&(e.setupState=mt(t)),fr(e)}function fr(e,t,n){const o=e.type;e.render||(e.render=o.render||k,e.render._rc&&(e.withProxy=new Proxy(e.ctx,rr))),ir=e,de(),Vn(e),pe(),ir=null}function hr(e){if(e.exposed)return e.exposeProxy||(e.exposeProxy=new Proxy(mt(at(e.exposed)),{get:(t,n)=>n in t?t[n]:n in nr?nr[n](e):void 0}))}function mr(e,t=ir){t&&(t.effects||(t.effects=[])).push(e)}function vr(e){return O(e)&&e.displayName||e.name}function gr(e){const t=function(e){let t,n;return O(e)?(t=e,n=k):(t=e.get,n=e.set),new yt(t,n,O(e)||!e.set)}(e);return mr(t.effect),t}function br(e,t,n){const o=arguments.length;return 2===o?M(t)&&!E(t)?Fo(t)?Wo(e,null,[t]):Wo(e,t):Wo(e,null,t):(o>3?n=Array.prototype.slice.call(arguments,2):3===o&&Fo(n)&&(n=[n]),Wo(e,t,n))}const yr="3.1.5",xr="undefined"!=typeof document?document:null,kr=new Map,_r={insert:(e,t,n)=>{t.insertBefore(e,n||null)},remove:e=>{const t=e.parentNode;t&&t.removeChild(e)},createElement:(e,t,n,o)=>{const r=t?xr.createElementNS("http://www.w3.org/2000/svg",e):xr.createElement(e,n?{is:n}:void 0);return"select"===e&&o&&null!=o.multiple&&r.setAttribute("multiple",o.multiple),r},createText:e=>xr.createTextNode(e),createComment:e=>xr.createComment(e),setText:(e,t)=>{e.nodeValue=t},setElementText:(e,t)=>{e.textContent=t},parentNode:e=>e.parentNode,nextSibling:e=>e.nextSibling,querySelector:e=>xr.querySelector(e),setScopeId(e,t){e.setAttribute(t,"")},cloneNode(e){const t=e.cloneNode(!0);return"_value"in e&&(t._value=e._value),t},insertStaticContent(e,t,n,o){const r=n?n.previousSibling:t.lastChild;let s=kr.get(e);if(!s){const t=xr.createElement("template");if(t.innerHTML=o?`${e}`:e,s=t.content,o){const e=s.firstChild;for(;e.firstChild;)s.appendChild(e.firstChild);s.removeChild(e)}kr.set(e,s)}return t.insertBefore(s.cloneNode(!0),n),[r?r.nextSibling:t.firstChild,n?n.previousSibling:t.lastChild]}};const wr=/\s*!important$/;function Sr(e,t,n){if(E(n))n.forEach((n=>Sr(e,t,n)));else if(t.startsWith("--"))e.setProperty(t,n);else{const o=function(e,t){const n=Nr[t];if(n)return n;let o=H(t);if("filter"!==o&&o in e)return Nr[t]=o;o=J(o);for(let r=0;rdocument.createEvent("Event").timeStamp&&(Pr=()=>performance.now());const e=navigator.userAgent.match(/firefox\/(\d+)/i);Ar=!!(e&&Number(e[1])<=53)}let Er=0;const Lr=Promise.resolve(),$r=()=>{Er=0};function Or(e,t,n,o,r=null){const s=e._vei||(e._vei={}),l=s[t];if(o&&l)l.value=o;else{const[n,i]=function(e){let t;if(Tr.test(e)){let n;for(t={};n=e.match(Tr);)e=e.slice(0,e.length-n[0].length),t[n[0].toLowerCase()]=!0}return[G(e.slice(2)),t]}(t);if(o){!function(e,t,n,o){e.addEventListener(t,n,o)}(e,n,s[t]=function(e,t){const n=e=>{const o=e.timeStamp||Pr();(Ar||o>=n.attached-1)&&kt(function(e,t){if(E(t)){const n=e.stopImmediatePropagation;return e.stopImmediatePropagation=()=>{n.call(e),e._stopped=!0},t.map((e=>t=>!t._stopped&&e(t)))}return t}(e,n.value),t,5,[e])};return n.value=e,n.attached=(()=>Er||(Lr.then($r),Er=Pr()))(),n}(o,r),i)}else l&&(!function(e,t,n,o){e.removeEventListener(t,n,o)}(e,n,l,i),s[t]=void 0)}}const Tr=/(?:Once|Passive|Capture)$/;const jr=/^on[a-z]/;Boolean;const Mr=N({patchProp:(e,t,n,o,r=!1,s,l,i,a)=>{switch(t){case"class":!function(e,t,n){const o=e._vtc;o&&(t=(t?[t,...o]:[...o]).join(" ")),null==t?e.removeAttribute("class"):n?e.setAttribute("class",t):e.className=t}(e,o,r);break;case"style":!function(e,t,n){const o=e.style;if(n)if(T(n)){if(t!==n){const t=o.display;o.cssText=n,"_vod"in e&&(o.display=t)}}else{for(const e in n)Sr(o,e,n[e]);if(t&&!T(t))for(const e in t)null==n[e]&&Sr(o,e,"")}else e.removeAttribute("style")}(e,n,o);break;default:S(t)?C(t)||Or(e,t,0,o,l):function(e,t,n,o){if(o)return"innerHTML"===t||!!(t in e&&jr.test(t)&&O(n));if("spellcheck"===t||"draggable"===t)return!1;if("form"===t)return!1;if("list"===t&&"INPUT"===e.tagName)return!1;if("type"===t&&"TEXTAREA"===e.tagName)return!1;if(jr.test(t)&&T(n))return!1;return t in e}(e,t,o,r)?function(e,t,n,o,r,s,l){if("innerHTML"===t||"textContent"===t)return o&&l(o,r,s),void(e[t]=null==n?"":n);if("value"===t&&"PROGRESS"!==e.tagName){e._value=n;const o=null==n?"":n;return e.value!==o&&(e.value=o),void(null==n&&e.removeAttribute(t))}if(""===n||null==n){const o=typeof e[t];if(""===n&&"boolean"===o)return void(e[t]=!0);if(null==n&&"string"===o)return e[t]="",void e.removeAttribute(t);if("number"===o){try{e[t]=0}catch(i){}return void e.removeAttribute(t)}}try{e[t]=n}catch(a){}}(e,t,o,s,l,i,a):("true-value"===t?e._trueValue=o:"false-value"===t&&(e._falseValue=o),function(e,t,n,o,r){if(o&&t.startsWith("xlink:"))null==n?e.removeAttributeNS(Br,t.slice(6,t.length)):e.setAttributeNS(Br,t,n);else{const o=d(t);null==n||o&&!1===n?e.removeAttribute(t):e.setAttribute(t,o?"":n)}}(e,t,o,r))}},forcePatchProp:(e,t)=>"value"===t},_r);let Rr,Fr=!1;const Ir=(...e)=>{const t=(Rr=Fr?Rr:_o(Mr),Fr=!0,Rr).createApp(...e),{mount:n}=t;return t.mount=e=>{const t=function(e){if(T(e)){return document.querySelector(e)}return e}(e);if(t)return n(t,!0,t instanceof SVGElement)},t};const Ur=/^https?:/i,Vr="undefined"!=typeof window;function Dr(e,t){const n=function(e,t){t.sort(((e,t)=>{const n=t.split("/").length-e.split("/").length;return 0!==n?n:t.length-e.length}));for(const n of t)if(e.startsWith(n))return}(t,Object.keys(e));return n?e[n]:void 0}function Wr(e,t){t=function(e,t){if(!Vr)return t;const n=e.base,o=n.endsWith("/")?n.slice(0,-1):n;return t.slice(o.length)}(e,t);const n=Dr(e.locales||{},t)||{},o=Dr(e.themeConfig&&e.themeConfig.locales||{},t)||{};return a(i(i({},e),n),{themeConfig:a(i(i({},e.themeConfig),o),{locales:{}}),lang:o.lang||e.lang,locales:{}})}const zr=Symbol(),Hr=dt((qr='{"lang":"en-US","title":"Awesome Notes","description":"Daily Notes I Learned.","base":"/awesome-notes/","head":[["link",{"rel":"icon","type":"image/svg+xml","href":"/awesome-notes/logo.svg"}]],"themeConfig":{"repo":"sabertazimi/awesome-notes","logo":"/logo.svg","docsDir":"notes","docsBranch":"main","editLinks":true,"editLinkText":"Edit this page on GitHub","lastUpdated":"Last Updated","nav":[{"text":"Notes","link":"/","activeMatch":"^/$"},{"text":"Computer Science","link":"/computerScience/virtualization/virtBasicNotes","activeMatch":"^/computerScience/"},{"text":"Language","link":"/language/verilog/verilogBasicNotes","activeMatch":"^/language/"},{"text":"Programming","link":"/programming/tools/vim/vimBasicNotes","activeMatch":"^/programming/"},{"text":"Web","link":"/web/security/securityBasicNotes","activeMatch":"^/web/"},{"text":"Links","items":[{"text":"Twitter","link":"https://twitter.com/sabertazimi"},{"text":"Facebook","link":"https://facebook.com/sabertazimi"},{"text":"Weibo","link":"https://weibo.com/sabertazimi"}]},{"text":"GitBook","link":"https://notes.tazimi.dev"}],"sidebar":{"/computerScience/":[{"text":"Computer Science","children":[{"text":"Algorithms","link":"/computerScience/algorithms/algorithmsBasicNotes"},{"text":"OJ","link":"/computerScience/algorithms/ojBasicNotes"},{"text":"Architecture","link":"/computerScience/architecture/archBasicNotes"},{"text":"Graph Processing","link":"/computerScience/architecture/graphProcessingBasicNotes"},{"text":"Compiler","link":"/computerScience/compilers/compilersBasicNotes"},{"text":"Database","link":"/computerScience/database/databaseBasicNotes"},{"text":"Latex","link":"/computerScience/latex/latexBasicNotes"},{"text":"Network","link":"/computerScience/network/networkBasicNotes"},{"text":"CSAPP","link":"/computerScience/operatingSystems/CSAPP"},{"text":"Operating Systems","link":"/computerScience/operatingSystems/operatingSystemsBasicNotes"},{"text":"Virtualization","link":"/computerScience/virtualization/virtBasicNotes"}]}],"/language/":[{"text":"Language","children":[{"text":"Assembly","link":"/language/assembly/assemblyBasicNotes"},{"text":"C","link":"/language/c/cBasicNotes"},{"text":"C++","link":"/language/cpp/cppBasicNotes"},{"text":"Go","link":"/language/go/goBasicNotes"},{"text":"Haskell","link":"/language/haskell/haskellBasicNotes"},{"text":"Java","link":"/language/java/javaBasicNotes"},{"text":"Python","link":"/language/python/pythonBasicNotes"},{"text":"Verilog","link":"/language/verilog/verilogBasicNotes"}]}],"/programming/":[{"text":"Programming","children":[{"text":"Android","link":"/programming/android/androidBasicNotes"},{"text":"Clean Code","link":"/programming/devops/cleanCodeBasicNotes"},{"text":"Design Patterns","link":"/programming/devops/designPatternsBasicNotes"},{"text":"Software Testing","link":"/programming/devops/softwareTestingBasicNotes"},{"text":"Functional Programming","link":"/programming/functionalProgramming/functionalProgrammingBasicNotes"},{"text":"Game Design","link":"/programming/game/gameDesignBasicNotes"},{"text":"Linux","link":"/programming/linux/linuxBasicNotes"},{"text":"CMake","link":"/programming/tools/buildTools/CMakeBasicNotes"},{"text":"Git","link":"/programming/tools/git/gitBasicNotes"},{"text":"Vim","link":"/programming/tools/vim/vimBasicNotes"}]}],"/web/":[{"text":"Web","children":[{"text":"HTML","link":"/web/html/htmlBasicNotes"},{"text":"CSS","link":"/web/css/cssBasicNotes"},{"text":"Advanced CSS","link":"/web/css/cssAdvancedNotes"},{"text":"Sass","link":"/web/css/sassBasicNotes"},{"text":"Bootstrap","link":"/web/css/bootstrapBasicNotes"},{"text":"JavaScript","link":"/web/javascript/javascriptBasicNotes"},{"text":"Advanced JavaScript","link":"/web/javascript/javascriptAdvancedNotes"},{"text":"TypeScript","link":"/web/javascript/typescriptBasicNotes"},{"text":"Angular","link":"/web/angular/angularBasicNotes"},{"text":"React","link":"/web/react/reactBasicNotes"},{"text":"React Router","link":"/web/react/reactRouterBasicNotes"},{"text":"Redux","link":"/web/react/reduxBasicNotes"},{"text":"Vue","link":"/web/vue/vueBasicNotes"},{"text":"Library","link":"/web/frameworks/libraryBasicNotes"},{"text":"jQuery","link":"/web/frameworks/jQueryBasicNotes"},{"text":"Electron","link":"/web/frameworks/electronBasicNotes"},{"text":"Node","link":"/web/node/nodeBasicNotes"},{"text":"Express","link":"/web/node/expressBasicNotes"},{"text":"GraphQL","link":"/web/node/graphqlBasicNotes"},{"text":"Message Queue","link":"/web/node/messageQueueBasicNotes"},{"text":"Web Security","link":"/web/security/securityBasicNotes"}]}],"/":[{"text":"Computer Science","children":[{"text":"Algorithms","link":"/computerScience/algorithms/algorithmsBasicNotes"},{"text":"OJ","link":"/computerScience/algorithms/ojBasicNotes"},{"text":"Architecture","link":"/computerScience/architecture/archBasicNotes"},{"text":"Graph Processing","link":"/computerScience/architecture/graphProcessingBasicNotes"},{"text":"Compiler","link":"/computerScience/compilers/compilersBasicNotes"},{"text":"Database","link":"/computerScience/database/databaseBasicNotes"},{"text":"Latex","link":"/computerScience/latex/latexBasicNotes"},{"text":"Network","link":"/computerScience/network/networkBasicNotes"},{"text":"CSAPP","link":"/computerScience/operatingSystems/CSAPP"},{"text":"Operating Systems","link":"/computerScience/operatingSystems/operatingSystemsBasicNotes"},{"text":"Virtualization","link":"/computerScience/virtualization/virtBasicNotes"}]},{"text":"Language","children":[{"text":"Assembly","link":"/language/assembly/assemblyBasicNotes"},{"text":"C","link":"/language/c/cBasicNotes"},{"text":"C++","link":"/language/cpp/cppBasicNotes"},{"text":"Go","link":"/language/go/goBasicNotes"},{"text":"Haskell","link":"/language/haskell/haskellBasicNotes"},{"text":"Java","link":"/language/java/javaBasicNotes"},{"text":"Python","link":"/language/python/pythonBasicNotes"},{"text":"Verilog","link":"/language/verilog/verilogBasicNotes"}]},{"text":"Programming","children":[{"text":"Android","link":"/programming/android/androidBasicNotes"},{"text":"Clean Code","link":"/programming/devops/cleanCodeBasicNotes"},{"text":"Design Patterns","link":"/programming/devops/designPatternsBasicNotes"},{"text":"Software Testing","link":"/programming/devops/softwareTestingBasicNotes"},{"text":"Functional Programming","link":"/programming/functionalProgramming/functionalProgrammingBasicNotes"},{"text":"Game Design","link":"/programming/game/gameDesignBasicNotes"},{"text":"Linux","link":"/programming/linux/linuxBasicNotes"},{"text":"CMake","link":"/programming/tools/buildTools/CMakeBasicNotes"},{"text":"Git","link":"/programming/tools/git/gitBasicNotes"},{"text":"Vim","link":"/programming/tools/vim/vimBasicNotes"}]},{"text":"Web","children":[{"text":"HTML","link":"/web/html/htmlBasicNotes"},{"text":"CSS","link":"/web/css/cssBasicNotes"},{"text":"Advanced CSS","link":"/web/css/cssAdvancedNotes"},{"text":"Sass","link":"/web/css/sassBasicNotes"},{"text":"Bootstrap","link":"/web/css/bootstrapBasicNotes"},{"text":"JavaScript","link":"/web/javascript/javascriptBasicNotes"},{"text":"Advanced JavaScript","link":"/web/javascript/javascriptAdvancedNotes"},{"text":"TypeScript","link":"/web/javascript/typescriptBasicNotes"},{"text":"Angular","link":"/web/angular/angularBasicNotes"},{"text":"React","link":"/web/react/reactBasicNotes"},{"text":"React Router","link":"/web/react/reactRouterBasicNotes"},{"text":"Redux","link":"/web/react/reduxBasicNotes"},{"text":"Vue","link":"/web/vue/vueBasicNotes"},{"text":"Library","link":"/web/frameworks/libraryBasicNotes"},{"text":"jQuery","link":"/web/frameworks/jQueryBasicNotes"},{"text":"Electron","link":"/web/frameworks/electronBasicNotes"},{"text":"Node","link":"/web/node/nodeBasicNotes"},{"text":"Express","link":"/web/node/expressBasicNotes"},{"text":"GraphQL","link":"/web/node/graphqlBasicNotes"},{"text":"Message Queue","link":"/web/node/messageQueueBasicNotes"},{"text":"Web Security","link":"/web/security/securityBasicNotes"}]}]}},"locales":{},"customData":{}}',nt(JSON.parse(qr))));var qr;function Gr(){const e=rn(zr);if(!e)throw new Error("vitepress data not properly injected in app");return e}function Jr(e){return Ur.test(e)?e:function(e,t){return`${e}${t}`.replace(/\/+/g,"/")}(Hr.value.base,e)}function Qr(e){let t=e.replace(/\.html$/,"");if(t=decodeURIComponent(t),t.endsWith("/")&&(t+="index"),Vr){const e="/awesome-notes/";t=t.slice(e.length).replace(/\//g,"_")+".md";const n=__VP_HASH_MAP__[t.toLowerCase()];t=`${e}assets/${t}.${n}.js`}else t=`./${t.slice(1).replace(/\//g,"_")}.md.js`;return t}const Kr=Symbol();function Yr(){return function(){const e=rn(Kr);if(!e)throw new Error("useRouter() is called without provider.");return e}().route}function Xr(e,t,n=!1){const o=e.classList.contains(".header-anchor")?e:document.querySelector(decodeURIComponent(t));if(o){const e=o.offsetTop;!n||Math.abs(e-window.scrollY)>window.innerHeight?window.scrollTo(0,e):window.scrollTo({left:0,top:e,behavior:"smooth"})}}function Zr(e,t){let n=[],o=!0;var r;an((()=>{const r=e.data,s=t.value,l=r&&r.title,i=r&&r.description,a=r&&r.frontmatter.head;var c;document.title=(l?l+" | ":"")+s.title,document.querySelector("meta[name=description]").setAttribute("content",i||s.description),(e=>{if(o)return void(o=!1);const t=[],r=Math.min(n.length,e.length);for(let o=0;odocument.head.removeChild(e))),e.slice(r).forEach((e=>{const n=es(e);document.head.appendChild(n),t.push(n)})),n=t})([...a?(c=a,c.filter((e=>{return!("meta"===(t=e)[0]&&t[1]&&"description"===t[1].name);var t}))):[]])}),null,r)}function es([e,t,n]){const o=document.createElement(e);for(const r in t)o.setAttribute(r,t[r]);return n&&(o.innerHTML=n),o}const ts=yn({name:"VitePressContent",setup(){const e=Yr();return()=>br("div",{style:{position:"relative"}},[e.component?br(e.component):null])}});Kt("data-v-edc1134c");const ns=Wo("p",{class:"title"},"Debug",-1),os={class:"block"};Yt();yn({setup(e){const t=Gr(),n=dt(null),o=dt(!1);return ln(o,(e=>{!1===e&&(n.value.scrollTop=0)})),(e,r)=>(To(),Ro("div",{class:["debug",{open:o.value}],ref:n,onClick:r[1]||(r[1]=e=>o.value=!o.value)},[ns,Wo("pre",os,g(ft(t)),1)],2))}}).__scopeId="data-v-edc1134c";const rs=/#.*$/,ss=/(index)?\.(md|html)$/,ls=/\/$/,is=/^[a-z]+:/i;function as(e){return Array.isArray(e)}function cs(e){return is.test(e)}function us(e){return decodeURI(e).replace(rs,"").replace(ss,"")}function ds(e){return/^\//.test(e)?e:`/${e}`}function ps(e){return e.replace(/(index)?(\.(md|html))?$/,"")||"/"}function fs(e,t){if(function(e){return!1===e||"auto"===e||as(e)}(e))return e;t=ds(t);for(const n in e)if(t.startsWith(ds(n)))return e[n];return"auto"}function hs(e){return e.reduce(((e,t)=>(t.link&&e.push({text:t.text,link:ps(t.link)}),function(e){return void 0!==e.children}(t)&&(e=[...e,...hs(t.children)]),e)),[])}var ms=yn({setup(e){const{site:t,theme:n,localePath:o}=Gr();return(e,r)=>(To(),Ro("a",{class:"nav-bar-title",href:ft(o),"aria-label":`${ft(t).title}, back to home`},[ft(n).logo?(To(),Ro("img",{key:0,class:"logo",src:ft(Jr)(ft(n).logo),alt:"Logo"},null,8,["src"])):Go("v-if",!0),Ho(" "+g(ft(t).title),1)],8,["href","aria-label"]))}});ms.__scopeId="data-v-f07481ba";const vs=["GitHub","GitLab","Bitbucket"].map((e=>[e,new RegExp(e,"i")]));function gs(){const{site:e}=Gr();return gr((()=>{const t=e.value.themeConfig,n=t.docsRepo||t.repo;if(!n)return null;const o=/^https?:/.test(r=n)?r:`https://github.com/${r}`;var r;return{text:function(e,t){if(t)return t;const n=e.match(/^https?:\/\/[^/]+/);if(!n)return"Source";const o=vs.find((([e,t])=>t.test(n[0])));if(o&&o[0])return o[0];return"Source"}(o,t.repoLabel),link:o}}))}function bs(e){const t=Yr(),n=cs(e.value.link);return{props:gr((()=>{const o=ys(`/${t.data.relativePath}`);let r=!1;if(e.value.activeMatch)r=new RegExp(e.value.activeMatch).test(o);else{const t=ys(Jr(e.value.link));r="/"===t?t===o:o.startsWith(t)}return{class:{active:r,isExternal:n},href:n?e.value.link:Jr(e.value.link),target:e.value.target||n?"_blank":null,rel:e.value.rel||n?"noopener noreferrer":null,"aria-label":e.value.ariaLabel}})),isExternal:n}}function ys(e){return e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\.(html|md)$/,"").replace(/\/index$/,"/")}const xs={},ks={class:"icon outbound",xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",x:"0px",y:"0px",viewBox:"0 0 100 100",width:"15",height:"15"},_s=Wo("path",{fill:"currentColor",d:"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"},null,-1),ws=Wo("polygon",{fill:"currentColor",points:"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"},null,-1);xs.render=function(e,t){return To(),Ro("svg",ks,[_s,ws])},Kt("data-v-2c9af77b");const Ss={class:"nav-link"};Yt();var Cs=yn({props:{item:{type:null,required:!0}},setup(e){const t=vt(e),{props:n,isExternal:o}=bs(t.item);return(t,r)=>(To(),Ro("div",Ss,[Wo("a",Yo({class:"item"},ft(n)),[Ho(g(e.item.text)+" ",1),ft(o)?(To(),Ro(xs,{key:0})):Go("v-if",!0)],16)]))}});Cs.__scopeId="data-v-2c9af77b",Kt("data-v-22d28cf1");const Ns={class:"nav-dropdown-link-item"},Bs=Wo("span",{class:"arrow"},null,-1),Ps={class:"text"},As={class:"icon"};Yt();var Es=yn({props:{item:{type:null,required:!0}},setup(e){const t=vt(e),{props:n,isExternal:o}=bs(t.item);return(t,r)=>(To(),Ro("div",Ns,[Wo("a",Yo({class:"item"},ft(n)),[Bs,Wo("span",Ps,g(e.item.text),1),Wo("span",As,[ft(o)?(To(),Ro(xs,{key:0})):Go("v-if",!0)])],16)]))}});Es.__scopeId="data-v-22d28cf1",Kt("data-v-2a5d8d7e");const Ls={class:"button-text"},$s={class:"dialog"};Yt();var Os=yn({props:{item:{type:null,required:!0}},setup(e){const t=Yr(),n=dt(!1);function o(){n.value=!n.value}return ln((()=>t.path),(()=>{n.value=!1})),(t,r)=>(To(),Ro("div",{class:["nav-dropdown-link",{open:n.value}]},[Wo("button",{class:"button","aria-label":e.item.ariaLabel,onClick:o},[Wo("span",Ls,g(e.item.text),1),Wo("span",{class:["button-arrow",n.value?"down":"right"]},null,2)],8,["aria-label"]),Wo("ul",$s,[(To(!0),Ro(Po,null,Xo(e.item.items,(e=>(To(),Ro("li",{key:e.text,class:"dialog-item"},[Wo(Es,{item:e},null,8,["item"])])))),128))])],2))}});Os.__scopeId="data-v-2a5d8d7e",Kt("data-v-74485887");const Ts={key:0,class:"nav-links"},js={key:1,class:"item"},Ms={key:2,class:"item"};Yt();var Rs=yn({setup(e){const{theme:t}=Gr(),n=function(){const e=Yr(),{site:t}=Gr();return gr((()=>{const n=t.value.themeConfig.locales;if(!n)return null;const o=Object.keys(n);if(o.length<=1)return null;const r=Vr?t.value.base:"/",s=r.endsWith("/")?r.slice(0,-1):r,l=e.path.slice(s.length),i=o.find((e=>"/"!==e&&l.startsWith(e))),a=i?l.substring(i.length-1):l,c=o.map((e=>{const t=e.endsWith("/")?e.slice(0,-1):e;return{text:n[e].label,link:`${t}${a}`}})),u=i||"/";return{text:n[u].selectText?n[u].selectText:"Languages",items:c}}))}(),o=gs(),r=gr((()=>t.value.nav||o.value||n.value));return(e,s)=>ft(r)?(To(),Ro("nav",Ts,[ft(t).nav?(To(!0),Ro(Po,{key:0},Xo(ft(t).nav,(e=>(To(),Ro("div",{key:e.text,class:"item"},[e.items?(To(),Ro(Os,{key:0,item:e},null,8,["item"])):(To(),Ro(Cs,{key:1,item:e},null,8,["item"]))])))),128)):Go("v-if",!0),ft(n)?(To(),Ro("div",js,[Wo(Os,{item:ft(n)},null,8,["item"])])):Go("v-if",!0),ft(o)?(To(),Ro("div",Ms,[Wo(Cs,{item:ft(o)},null,8,["item"])])):Go("v-if",!0)])):Go("v-if",!0)}});Rs.__scopeId="data-v-74485887";var Fs={emits:["toggle"]};const Is=Wo("svg",{class:"icon",xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",role:"img",viewBox:"0 0 448 512"},[Wo("path",{fill:"currentColor",d:"M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z",class:""})],-1);Fs.render=function(e,t,n,o,r,s){return To(),Ro("div",{class:"sidebar-button",onClick:t[1]||(t[1]=t=>e.$emit("toggle"))},[Is])},Kt("data-v-dd1cb1ee");const Us={class:"nav-bar"},Vs=Wo("div",{class:"flex-grow"},null,-1),Ds={class:"nav"};Yt();var Ws=yn({emits:["toggle"],setup:e=>(e,t)=>(To(),Ro("header",Us,[Wo(Fs,{onToggle:t[1]||(t[1]=t=>e.$emit("toggle"))}),Wo(ms),Vs,Wo("div",Ds,[Wo(Rs)]),Zo(e.$slots,"search",{},void 0,!0)]))});function zs(){let e=null,t=null;const n=function(e,t){let n,o=!1;return()=>{n&&clearTimeout(n),o?n=setTimeout(e,t):(e(),o=!0,setTimeout((()=>{o=!1}),t))}}(o,300);function o(){const e=function(e){return[].slice.call(document.querySelectorAll(".header-anchor")).filter((t=>e.some((e=>e.hash===t.hash))))}([].slice.call(document.querySelectorAll(".sidebar a.sidebar-link-item")));for(let t=0;t ul > li");o&&o!==t.parentElement?(e=o.querySelector("a"),e&&e.classList.add("active")):e=null}function s(e){e&&e.classList.remove("active")}Ln((()=>{o(),window.addEventListener("scroll",n)})),On((()=>{r(decodeURIComponent(location.hash))})),jn((()=>{window.removeEventListener("scroll",n)}))}function Hs(e){const t=document.querySelector(".nav-bar").offsetHeight;return e.parentElement.offsetTop-t-15}function qs(e,t,n){const o=window.scrollY;return 0===e&&0===o?[!0,null]:o{if(e-1>t)return;const l={text:r,link:`#${s}`};2===e?(o=l,n.push(l)):o&&(o.children||(o.children=[])).push(l)})),n}Ws.__scopeId="data-v-dd1cb1ee";const Js=e=>{const t=Yr(),{site:n,frontmatter:o}=Gr(),r=e.depth||1,s=o.value.sidebarDepth||1/0,l=t.data.headers,i=e.item.text,a=function(e,t){if(void 0===t)return t;if(t.startsWith("#"))return t;return function(e,t){const n=e.endsWith("/"),o=t.startsWith("/");return n&&o?e.slice(0,-1)+t:n||o?e+t:`${e}/${t}`}(e,t)}(n.value.base,e.item.link),c=e.item.children,u=function(e,t){return void 0!==t&&us(`/${e.data.relativePath}`)===us(t)}(t,e.item.link),d=r0?br("ul",{class:"sidebar-links"},t.map((e=>br(Js,{item:e,depth:o})))):e&&n?Qs(!1,function(e){return Ks(function(e){let t;return(e=e.map((e=>Object.assign({},e)))).forEach((e=>{2===e.level?t=e:t&&(t.children||(t.children=[])).push(e)})),e.filter((e=>2===e.level))}(e))}(n),void 0,o):null}function Ks(e){return e.map((e=>({text:e.title,link:`#${e.slug}`,children:e.children?Ks(e.children):void 0})))}const Ys={key:0,class:"sidebar-links"};var Xs=yn({setup(e){const t=function(){const e=Yr(),{site:t}=Gr();return zs(),gr((()=>{const n=e.data.headers,o=e.data.frontmatter.sidebar,r=e.data.frontmatter.sidebarDepth;if(!1===o)return[];if("auto"===o)return Gs(n,r);const s=fs(t.value.themeConfig.sidebar,e.data.relativePath);return!1===s?[]:"auto"===s?Gs(n,r):s}))}();return(e,n)=>ft(t).length>0?(To(),Ro("ul",Ys,[(To(!0),Ro(Po,null,Xo(ft(t),(e=>(To(),Ro(ft(Js),{item:e},null,8,["item"])))),256))])):Go("v-if",!0)}}),Zs=yn({props:{open:{type:Boolean,required:!0}},setup:e=>(t,n)=>(To(),Ro("aside",{class:["sidebar",{open:e.open}]},[Wo(Rs,{class:"nav"}),Zo(t.$slots,"sidebar-top",{},void 0,!0),Wo(Xs),Zo(t.$slots,"sidebar-bottom",{},void 0,!0)],2))});Zs.__scopeId="data-v-74188dea";const el=/bitbucket.org/;function tl(){const{page:e,theme:t,frontmatter:n}=Gr();return{url:gr((()=>{const{repo:o,docsDir:r="",docsBranch:s="master",docsRepo:l=o,editLinks:i}=t.value,a=null!=n.value.editLink?n.value.editLink:i,{relativePath:c}=e.value;return a&&c&&o?function(e,t,n,o,r){return el.test(e)?function(e,t,n,o,r){return(cs(t)?t:e).replace(ls,"")+`/src/${o}/`+(n?n.replace(ls,"")+"/":"")+r+`?mode=edit&spa=0&at=${o}&fileviewer=file-view-default`}(e,t,n,o,r):function(e,t,n,o,r){return(cs(t)?t:`https://github.com/${t}`).replace(ls,"")+`/edit/${o}/`+(n?n.replace(ls,"")+"/":"")+r}(0,t,n,o,r)}(o,l,r,s,c):null})),text:gr((()=>t.value.editLinkText||"Edit this page"))}}Kt("data-v-93821cda");const nl={class:"edit-link"};Yt();var ol=yn({setup(e){const{url:t,text:n}=tl();return(e,o)=>(To(),Ro("div",nl,[ft(t)?(To(),Ro("a",{key:0,class:"link",href:ft(t),target:"_blank",rel:"noopener noreferrer"},[Ho(g(ft(n))+" ",1),Wo(xs,{class:"icon"})],8,["href"])):Go("v-if",!0)]))}});ol.__scopeId="data-v-93821cda",Kt("data-v-694e09d6");const rl={key:0,class:"last-updated"},sl={class:"prefix"},ll={class:"datetime"};Yt();var il=yn({setup(e){const{theme:t,page:n}=Gr(),o=gr((()=>{const e=t.value.lastUpdated;return void 0!==e&&!1!==e})),r=gr((()=>{const e=t.value.lastUpdated;return!0===e?"Last Updated":e})),s=dt("");return Ln((()=>{s.value=new Date(n.value.lastUpdated).toLocaleString("en-US")})),On((()=>{s.value=new Date(n.value.lastUpdated).toLocaleString("en-US")})),(e,t)=>ft(o)?(To(),Ro("p",rl,[Wo("span",sl,g(ft(r))+":",1),Wo("span",ll,g(s.value),1)])):Go("v-if",!0)}});il.__scopeId="data-v-694e09d6",Kt("data-v-3538c8db");const al={class:"page-footer"},cl={class:"edit"},ul={class:"updated"};Yt();var dl=yn({setup:e=>(e,t)=>(To(),Ro("footer",al,[Wo("div",cl,[Wo(ol)]),Wo("div",ul,[Wo(il)])]))});function pl(){const{page:e,theme:t}=Gr(),n=gr((()=>ps(ds(e.value.relativePath)))),o=gr((()=>{const e=fs(t.value.sidebar,n.value);return as(e)?hs(e):[]})),r=gr((()=>o.value.findIndex((e=>e.link===n.value)))),s=gr((()=>{if(!1!==t.value.nextLinks&&r.value>-1&&r.value{if(!1!==t.value.prevLinks&&r.value>0)return o.value[r.value-1]})),i=gr((()=>!!s.value||!!l.value));return{next:s,prev:l,hasLinks:i}}dl.__scopeId="data-v-3538c8db";const fl={},hl={xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24"},ml=Wo("path",{d:"M19,11H7.4l5.3-5.3c0.4-0.4,0.4-1,0-1.4s-1-0.4-1.4,0l-7,7c-0.1,0.1-0.2,0.2-0.2,0.3c-0.1,0.2-0.1,0.5,0,0.8c0.1,0.1,0.1,0.2,0.2,0.3l7,7c0.2,0.2,0.5,0.3,0.7,0.3s0.5-0.1,0.7-0.3c0.4-0.4,0.4-1,0-1.4L7.4,13H19c0.6,0,1-0.4,1-1S19.6,11,19,11z"},null,-1);fl.render=function(e,t){return To(),Ro("svg",hl,[ml])};const vl={},gl={xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24"},bl=Wo("path",{d:"M19.9,12.4c0.1-0.2,0.1-0.5,0-0.8c-0.1-0.1-0.1-0.2-0.2-0.3l-7-7c-0.4-0.4-1-0.4-1.4,0s-0.4,1,0,1.4l5.3,5.3H5c-0.6,0-1,0.4-1,1s0.4,1,1,1h11.6l-5.3,5.3c-0.4,0.4-0.4,1,0,1.4c0.2,0.2,0.5,0.3,0.7,0.3s0.5-0.1,0.7-0.3l7-7C19.8,12.6,19.9,12.5,19.9,12.4z"},null,-1);vl.render=function(e,t){return To(),Ro("svg",gl,[bl])},Kt("data-v-5ae8aee1");const yl={key:0,class:"next-and-prev-link"},xl={class:"container"},kl={class:"prev"},_l={class:"text"},wl={class:"next"},Sl={class:"text"};Yt();var Cl=yn({setup(e){const{hasLinks:t,prev:n,next:o}=pl();return(e,r)=>ft(t)?(To(),Ro("div",yl,[Wo("div",xl,[Wo("div",kl,[ft(n)?(To(),Ro("a",{key:0,class:"link",href:ft(Jr)(ft(n).link)},[Wo(fl,{class:"icon icon-prev"}),Wo("span",_l,g(ft(n).text),1)],8,["href"])):Go("v-if",!0)]),Wo("div",wl,[ft(o)?(To(),Ro("a",{key:0,class:"link",href:ft(Jr)(ft(o).link)},[Wo("span",Sl,g(ft(o).text),1),Wo(vl,{class:"icon icon-next"})],8,["href"])):Go("v-if",!0)])])])):Go("v-if",!0)}});Cl.__scopeId="data-v-5ae8aee1",Kt("data-v-d004a6fc");const Nl={class:"page"},Bl={class:"container"};Yt();var Pl=yn({setup:e=>(e,t)=>{const n=Co("Content");return To(),Ro("main",Nl,[Wo("div",Bl,[Zo(e.$slots,"top",{},void 0,!0),Wo(n,{class:"content"}),Wo(dl),Wo(Cl),Zo(e.$slots,"bottom",{},void 0,!0)])])}});Pl.__scopeId="data-v-d004a6fc";const Al={key:0,id:"ads-container"};var El=yn({setup(e){const t=kn((()=>import("./Home.f03b71db.js"))),n=()=>null,o=n,r=n,s=n,l=Yr(),{site:i,page:a,theme:c,frontmatter:u}=Gr(),d=gr((()=>!!u.value.customLayout)),p=gr((()=>!!u.value.home)),f=gr((()=>Object.keys(c.value.locales||{}).length>0)),h=gr((()=>{const e=c.value;return!1!==u.value.navbar&&!1!==e.navbar&&(i.value.title||e.logo||e.repo||e.nav)})),m=dt(!1),v=gr((()=>{return!u.value.home&&!1!==u.value.sidebar&&!(as(e=fs(c.value.sidebar,l.data.relativePath))?0===e.length:!e);var e})),g=e=>{m.value="boolean"==typeof e?e:!m.value},b=g.bind(null,!1);ln(l,b);const y=gr((()=>[{"no-navbar":!h.value,"sidebar-open":m.value,"no-sidebar":!v.value}]));return(e,n)=>{const l=Co("Content"),u=Co("Debug");return To(),Ro(Po,null,[Wo("div",{class:["theme",ft(y)]},[ft(h)?(To(),Ro(Ws,{key:0,onToggle:g},{search:Xt((()=>[Zo(e.$slots,"navbar-search",{},(()=>[ft(c).algolia?(To(),Ro(ft(s),{options:ft(c).algolia,multilang:ft(f),key:ft(i).lang},null,8,["options","multilang"])):Go("v-if",!0)]))])),_:3})):Go("v-if",!0),Wo(Zs,{open:m.value},{"sidebar-top":Xt((()=>[Zo(e.$slots,"sidebar-top")])),"sidebar-bottom":Xt((()=>[Zo(e.$slots,"sidebar-bottom")])),_:3},8,["open"]),Go(" TODO: make this button accessible "),Wo("div",{class:"sidebar-mask",onClick:n[1]||(n[1]=e=>g(!1))}),ft(d)?(To(),Ro(l,{key:1})):ft(p)?(To(),Ro(ft(t),{key:2},{hero:Xt((()=>[Zo(e.$slots,"home-hero")])),features:Xt((()=>[Zo(e.$slots,"home-features")])),footer:Xt((()=>[Zo(e.$slots,"home-footer")])),_:3})):(To(),Ro(Pl,{key:3},{top:Xt((()=>[Zo(e.$slots,"page-top-ads",{},(()=>[ft(c).carbonAds&&ft(c).carbonAds.carbon?(To(),Ro("div",Al,[Wo(ft(o),{key:"carbon"+ft(a).relativePath,code:ft(c).carbonAds.carbon,placement:ft(c).carbonAds.placement},null,8,["code","placement"])])):Go("v-if",!0)])),Zo(e.$slots,"page-top")])),bottom:Xt((()=>[Zo(e.$slots,"page-bottom"),Zo(e.$slots,"page-bottom-ads",{},(()=>[ft(c).carbonAds&&ft(c).carbonAds.custom?(To(),Ro(ft(r),{key:"custom"+ft(a).relativePath,code:ft(c).carbonAds.custom,placement:ft(c).carbonAds.placement},null,8,["code","placement"])):Go("v-if",!0)]))])),_:3}))],2),Wo(u)],64)}}});const Ll={class:"theme"},$l=Wo("h1",null,"404",-1);const Ol={Layout:El,NotFound:yn({setup(e){const t=["There's nothing here.","How did we get here?","That's a Four-Oh-Four.","Looks like we've got some broken links."];return(e,n)=>(To(),Ro("div",Ll,[$l,Wo("blockquote",null,g(t[Math.floor(Math.random()*t.length)]),1),Wo("a",{href:e.$site.base,"aria-label":"go to home"},"Take me home.",8,["href"])]))}})},Tl=new Set,jl=()=>document.createElement("link");let Ml;const Rl=Vr&&(Ml=jl())&&Ml.relList&&Ml.relList.supports&&Ml.relList.supports("prefetch")?e=>{const t=jl();t.rel="prefetch",t.href=e,document.head.appendChild(t)}:e=>{const t=new XMLHttpRequest;t.open("GET",e,t.withCredentials=!0),t.send()};const Fl=yn({setup(e,{slots:t}){const n=dt(!1);return Ln((()=>{n.value=!0})),()=>n.value&&t.default?t.default():null}}),Il=Ol.NotFound||(()=>"404 Not Found"),Ul={name:"VitePressApp",setup(){const{site:e}=Gr();return Ln((()=>{ln((()=>e.value.lang),(e=>{document.documentElement.lang=e}),{immediate:!0})})),function(){if(!Vr)return;if(!window.IntersectionObserver)return;let e;if((e=navigator.connection)&&(e.saveData||/2g/.test(e.effectiveType)))return;const t=window.requestIdleCallback||setTimeout;let n=null;const o=()=>{n&&n.disconnect(),n=new IntersectionObserver((e=>{e.forEach((e=>{if(e.isIntersecting){const t=e.target;n.unobserve(t);const{pathname:o}=t;if(!Tl.has(o)){Tl.add(o);const e=Qr(o);Rl(e)}}}))})),t((()=>{document.querySelectorAll("#app a").forEach((e=>{const{target:t,hostname:o,pathname:r}=e,s=r.match(/\.\w+$/);s&&".html"!==s[0]||"_blank"!==t&&o===location.hostname&&(r!==location.pathname?n.observe(e):Tl.add(r))}))}))};Ln(o);const r=Yr();ln((()=>r.path),o),jn((()=>{n&&n.disconnect()}))}(),()=>br(Ol.Layout)}};function Vl(){const e=function(){let e,t=Vr;return function(e,t){const n=tt({path:"/",component:null,data:null});function o(e=(Vr?location.href:"/")){const t=new URL(e,"http://a.com");return t.pathname.endsWith("/")||t.pathname.endsWith(".html")||(t.pathname+=".html",e=t.pathname+t.search+t.hash),Vr&&(history.replaceState({scrollPosition:window.scrollY},document.title),history.pushState(null,"",e)),s(e)}let r=null;async function s(o,s=0){const l=new URL(o,"http://a.com"),i=r=l.pathname;try{let t=e(i);if("then"in t&&"function"==typeof t.then&&(t=await t),r===i){r=null;const{default:e,__pageData:o}=t;if(!e)throw new Error(`Invalid route component: ${e}`);n.path=i,n.component=at(e),n.data=nt(JSON.parse(o)),Vr&&Mt((()=>{if(l.hash&&!s){const e=document.querySelector(decodeURIComponent(l.hash));if(e)return void Xr(e,l.hash)}window.scrollTo(0,s)}))}}catch(a){a.message.match(/fetch/)||console.error(a),r===i&&(r=null,n.path=i,n.component=t?at(t):null)}}return Vr&&(window.addEventListener("click",(e=>{const t=e.target.closest("a");if(t){const{href:n,protocol:r,hostname:s,pathname:l,hash:i,target:a}=t,c=window.location,u=l.match(/\.\w+$/);e.ctrlKey||e.shiftKey||e.altKey||e.metaKey||"_blank"===a||r!==c.protocol||s!==c.hostname||u&&".html"!==u[0]||(e.preventDefault(),l===c.pathname?i&&i!==c.hash&&(history.pushState(null,"",i),Xr(t,i,t.classList.contains("header-anchor"))):o(n))}}),{capture:!0}),window.addEventListener("popstate",(e=>{s(location.href,e.state&&e.state.scrollPosition||0)})),window.addEventListener("hashchange",(e=>{e.preventDefault()}))),{route:n,go:o}}((n=>{let o=Qr(n);return t&&(e=o),(t||e===o)&&(o=o.replace(/\.js$/,".lean.js")),Vr?(t=!1,import(o)):require(o)}),Il)}(),t=Ir(Ul);t.provide(Kr,e);const n=function(e){const t=gr((()=>Wr(Hr.value,e.path)));return{site:t,theme:gr((()=>t.value.themeConfig)),page:gr((()=>e.data)),frontmatter:gr((()=>e.data.frontmatter)),lang:gr((()=>t.value.lang)),localePath:gr((()=>{const{locales:e,lang:n}=t.value,o=Object.keys(e).find((t=>e[t].lang===n));return Jr(e&&o||"/")})),title:gr((()=>e.data.title?e.data.title+" | "+t.value.title:t.value.title)),description:gr((()=>e.data.description||t.value.description))}}(e.route);return t.provide(zr,n),Vr&&Zr(e.route,n.site),t.component("Content",ts),t.component("ClientOnly",Fl),t.component("Debug",(()=>null)),Object.defineProperty(t.config.globalProperties,"$frontmatter",{get:()=>n.frontmatter.value}),{app:t,router:e}}if(Vr){const{app:e,router:t}=Vl();t.go().then((()=>{e.mount("#app")}))}export{Po as F,Cs as _,qo as a,Wo as b,Ro as c,Vl as createApp,Ho as d,Go as e,yn as f,gr as g,ft as h,Yt as i,Co as j,Zo as k,To as o,Kt as p,Xo as r,g as t,Gr as u,Jr as w}; diff --git a/assets/computerScience_algorithms_algorithmsBasicNotes.md.36b13240.js b/assets/computerScience_algorithms_algorithmsBasicNotes.md.36b13240.js new file mode 100644 index 00000000000..213793ab71e --- /dev/null +++ b/assets/computerScience_algorithms_algorithmsBasicNotes.md.36b13240.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as t}from"./app.6dd2a1b7.js";const e='{"title":"Algorithm Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Sorting Algorithm","slug":"sorting-algorithm"},{"level":3,"title":"Summary","slug":"summary"},{"level":3,"title":"Selection Sort","slug":"selection-sort"},{"level":3,"title":"Insertion Sort","slug":"insertion-sort"},{"level":3,"title":"Shell Sort","slug":"shell-sort"},{"level":3,"title":"Merge Sort","slug":"merge-sort"},{"level":3,"title":"Quick Sort","slug":"quick-sort"},{"level":3,"title":"Heap Sort","slug":"heap-sort"},{"level":3,"title":"Radix Sort","slug":"radix-sort"},{"level":2,"title":"Tree Algorithm","slug":"tree-algorithm"},{"level":3,"title":"Binary Search Tree","slug":"binary-search-tree"},{"level":3,"title":"2-3 Tree","slug":"_2-3-tree"},{"level":3,"title":"Red-Black BST","slug":"red-black-bst"},{"level":3,"title":"B Tree","slug":"b-tree"},{"level":3,"title":"Fibonacci Heap","slug":"fibonacci-heap"},{"level":3,"title":"K-Dimensional Tree","slug":"k-dimensional-tree"},{"level":2,"title":"Search Algorithm","slug":"search-algorithm"},{"level":3,"title":"First Search","slug":"first-search"},{"level":3,"title":"cycle detection","slug":"cycle-detection"},{"level":2,"title":"Dynamic Programming","slug":"dynamic-programming"},{"level":3,"title":"子问题","slug":"子问题"},{"level":3,"title":"范例","slug":"范例"},{"level":2,"title":"Greedy Algorithm","slug":"greedy-algorithm"},{"level":2,"title":"Map Algorithm","slug":"map-algorithm"},{"level":3,"title":"图的表示","slug":"图的表示"},{"level":3,"title":"广度优先遍历","slug":"广度优先遍历"},{"level":3,"title":"深度优先遍历","slug":"深度优先遍历"},{"level":3,"title":"拓扑排序","slug":"拓扑排序"},{"level":3,"title":"单源最短路径","slug":"单源最短路径"},{"level":3,"title":"结点对最短路径","slug":"结点对最短路径"},{"level":3,"title":"最大流问题","slug":"最大流问题"},{"level":2,"title":"Tree Edit Distance","slug":"tree-edit-distance"},{"level":3,"title":"Definition","slug":"definition"}],"relativePath":"computerScience/algorithms/algorithmsBasicNotes.md","lastUpdated":1627194884000}',o={},l=a("h1",{id:"algorithm-basic-notes"},[a("a",{class:"header-anchor",href:"#algorithm-basic-notes","aria-hidden":"true"},"#"),t(" Algorithm Basic Notes")],-1),c=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#algorithm-basic-notes"},"Algorithm Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#sorting-algorithm"},"Sorting Algorithm")]),a("li",null,[a("a",{href:"#tree-algorithm"},"Tree Algorithm")]),a("li",null,[a("a",{href:"#search-algorithm"},"Search Algorithm")]),a("li",null,[a("a",{href:"#dynamic-programming"},"Dynamic Programming")]),a("li",null,[a("a",{href:"#greedy-algorithm"},"Greedy Algorithm")]),a("li",null,[a("a",{href:"#map-algorithm"},"Map Algorithm")]),a("li",null,[a("a",{href:"#tree-edit-distance"},"Tree Edit Distance")])])])])])],-1),p=a("h2",{id:"sorting-algorithm"},[a("a",{class:"header-anchor",href:"#sorting-algorithm","aria-hidden":"true"},"#"),t(" Sorting Algorithm")],-1),u=a("h3",{id:"summary"},[a("a",{class:"header-anchor",href:"#summary","aria-hidden":"true"},"#"),t(" Summary")],-1),i=a("ul",null,[a("li",null,"强制稳定: 增加(唯一)时间戳, 修改 CompareTo 接口定义 => 当主元素相同时, 时间戳小的元素更小")],-1),r=a("h3",{id:"selection-sort"},[a("a",{class:"header-anchor",href:"#selection-sort","aria-hidden":"true"},"#"),t(" Selection Sort")],-1),k=a("ul",null,[a("li",null,"swap: O(n)"),a("li",null,"compare: O(n^2)")],-1),b=a("h3",{id:"insertion-sort"},[a("a",{class:"header-anchor",href:"#insertion-sort","aria-hidden":"true"},"#"),t(" Insertion Sort")],-1),m=a("ul",null,[a("li",null,"swap: O(n^2/4)"),a("li",null,"compare: O(n^2/4)")],-1),d=a("h3",{id:"shell-sort"},[a("a",{class:"header-anchor",href:"#shell-sort","aria-hidden":"true"},"#"),t(" Shell Sort")],-1),h=a("ul",null,[a("li",null,"swap: O(n^2/4)"),a("li",null,"compare: O(n^2/4)")],-1),f=a("h3",{id:"merge-sort"},[a("a",{class:"header-anchor",href:"#merge-sort","aria-hidden":"true"},"#"),t(" Merge Sort")],-1),y=a("ul",null,[a("li",null,"利用 Merge Sort 计算逆序对个数: left[i] > right[j] => inversions += (mid - i + 1), 即所有 i~mid 元素都与 j 元素为逆序对")],-1),g=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// merge and count"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token keyword"},"static"),t(),a("span",{class:"token keyword"},"long"),t(),a("span",{class:"token function"},"merge"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" a"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" aux"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" lo"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" mid"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" hi"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"long"),t(" inversions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// copy to aux[]"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token punctuation"},";"),t(" k "),a("span",{class:"token operator"},"<="),t(" hi"),a("span",{class:"token punctuation"},";"),t(" k"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n aux"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// merge back to a[]"),t("\n "),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token punctuation"},","),t(" j "),a("span",{class:"token operator"},"="),t(" mid"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token punctuation"},";"),t(" k "),a("span",{class:"token operator"},"<="),t(" hi"),a("span",{class:"token punctuation"},";"),t(" k"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(" "),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},">"),t(" mid"),a("span",{class:"token punctuation"},")"),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" aux"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("j "),a("span",{class:"token operator"},">"),t(" hi"),a("span",{class:"token punctuation"},")"),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" aux"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("aux"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"<"),t(" aux"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" aux"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t(" inversions "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token punctuation"},"("),t("mid "),a("span",{class:"token operator"},"-"),t("\n i "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"else"),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" aux"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" inversions"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// return the number of inversions in the subArray b[lo..hi]"),t("\n "),a("span",{class:"token comment"},"// side effect b[lo..hi] is rearranged in ascending order"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token keyword"},"static"),t(),a("span",{class:"token keyword"},"long"),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" a"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" b"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" aux"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" lo"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" hi"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"long"),t(" inversions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("hi "),a("span",{class:"token operator"},"<="),t(" lo"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),t(" mid "),a("span",{class:"token operator"},"="),t(" lo "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token punctuation"},"("),t("hi "),a("span",{class:"token operator"},"-"),t(" lo"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),t("\n inversions "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" b"),a("span",{class:"token punctuation"},","),t(" aux"),a("span",{class:"token punctuation"},","),t(" lo"),a("span",{class:"token punctuation"},","),t(" mid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n inversions "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" b"),a("span",{class:"token punctuation"},","),t(" aux"),a("span",{class:"token punctuation"},","),t(" mid"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(" hi"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n inversions "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token function"},"merge"),a("span",{class:"token punctuation"},"("),t("b"),a("span",{class:"token punctuation"},","),t(" aux"),a("span",{class:"token punctuation"},","),t(" lo"),a("span",{class:"token punctuation"},","),t(" mid"),a("span",{class:"token punctuation"},","),t(" hi"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"assert"),t(" inversions "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token function"},"brute"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" lo"),a("span",{class:"token punctuation"},","),t(" hi"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" inversions"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n\n "),a("span",{class:"token comment"},"/**\n * Returns the number of inversions in the integer array.\n * The argument array is not modified.\n * @param a the array\n * @return the number of inversions in the array. An inversion is a pair of\n * indices {@code i} and {@code j} such that {@code i < j}\n * and {@code a[i]} > {@code a[j]}.\n */"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"static"),t(),a("span",{class:"token keyword"},"long"),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" a"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" b "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),t("a"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" aux "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),t("a"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" a"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t("\n b"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"long"),t(" inversions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" b"),a("span",{class:"token punctuation"},","),t(" aux"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(" a"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"-"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" inversions"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br")])],-1),w=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// return Kendall tau distance between two permutations"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"static"),t(),a("span",{class:"token keyword"},"long"),t(),a("span",{class:"token function"},"distance"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" a"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" b"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"!="),t(" b"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"IllegalArgumentException"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Array dimensions disagree"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"int"),t(" n "),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" ainV "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),t("n"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t("\n ainV"),a("span",{class:"token punctuation"},"["),t("a"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" i"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token class-name"},"Integer"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" bNew "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Integer"),a("span",{class:"token punctuation"},"["),t("n"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t("\n bNew"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" ainV"),a("span",{class:"token punctuation"},"["),t("b"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token class-name"},"Inversions"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),t("bNew"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),v=a("h3",{id:"quick-sort"},[a("a",{class:"header-anchor",href:"#quick-sort","aria-hidden":"true"},"#"),t(" Quick Sort")],-1),x=a("ul",null,[a("li",null,"partition: 哨兵(最后再将其归位) + 大循环 + 2 小循环, 交换元素法"),a("li",null,"partition: 辅助数组 brr, 3 循环(3 次扫描 arr) 分别将小/等/大于 guard 的数加入 brr"),a("li",null,"partition: 哨兵(最后再将其归位) + lo + hi, 外加 2 个动指针 leftLimit 与 rightLimit, 表示小于区的上界和大于区的上界")],-1),R=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// lt eq gt three parts"),t("\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"quick3waySort"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token operator"},"*"),t("a"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" lo"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" hi"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("hi "),a("span",{class:"token operator"},"<="),t(" lo"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),t(" lt "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token punctuation"},","),t(" i "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(" gt "),a("span",{class:"token operator"},"="),t(" hi"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),t(" v "),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"["),t("lo"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"<="),t(" gt"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" cmp "),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(" "),a("span",{class:"token punctuation"},"("),t("cmp "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token function"},"exch"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" lt"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},","),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("cmp "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token function"},"exch"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" i"),a("span",{class:"token punctuation"},","),t(" gt"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"sort"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" lo"),a("span",{class:"token punctuation"},","),t(" lt "),a("span",{class:"token operator"},"-"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"sort"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" gt "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(" hi"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),S=a("h3",{id:"heap-sort"},[a("a",{class:"header-anchor",href:"#heap-sort","aria-hidden":"true"},"#"),t(" Heap Sort")],-1),N=a("ul",null,[a("li",null,"Built on Priority Queue"),a("li",null,"swap: 2NlgN + 2N (2NlgN for sink N times, 2N for construct MaxHeap)"),a("li",null,"compare: NlgN + N (NlgN for sink N times, N for construct MaxHeap)")],-1),j=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// MaxPQ"),t("\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"swim"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("k "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"1"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token function"},"less"),a("span",{class:"token punctuation"},"("),t("k"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" k"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"exch"),a("span",{class:"token punctuation"},"("),t("k"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" k"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n k "),a("span",{class:"token operator"},"="),t(" k"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"sink"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"*"),t("k "),a("span",{class:"token operator"},"<="),t(" N"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" j "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"*"),t("k"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("j "),a("span",{class:"token operator"},"<"),t(" N "),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token function"},"less"),a("span",{class:"token punctuation"},"("),t("j"),a("span",{class:"token punctuation"},","),t(" j"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" j"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"less"),a("span",{class:"token punctuation"},"("),t("k"),a("span",{class:"token punctuation"},","),t(" j"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"break"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exch"),a("span",{class:"token punctuation"},"("),t("k"),a("span",{class:"token punctuation"},","),t(" j"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n k "),a("span",{class:"token operator"},"="),t(" j"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),A=a("h3",{id:"radix-sort"},[a("a",{class:"header-anchor",href:"#radix-sort","aria-hidden":"true"},"#"),t(" Radix Sort")],-1),T=a("p",null,"基数排序 (可用于混乱 shuffle 数组):",-1),D=a("ul",null,[a("li",null,"从个位到高位放入桶"),a("li",null,"从高位到个位放入桶")],-1),E=a("blockquote",null,[a("p",null,"Sorting Algorithms Performance")],-1),B=a("p",null,[a("img",{src:"/awesome-notes/assets/SortingPerformance.caae88a8.png",alt:"Performance"})],-1),M=a("h2",{id:"tree-algorithm"},[a("a",{class:"header-anchor",href:"#tree-algorithm","aria-hidden":"true"},"#"),t(" Tree Algorithm")],-1),F=a("h3",{id:"binary-search-tree"},[a("a",{class:"header-anchor",href:"#binary-search-tree","aria-hidden":"true"},"#"),t(" Binary Search Tree")],-1),z=a("p",null,"Hibbard Deletion",-1),C=a("h3",{id:"_2-3-tree"},[a("a",{class:"header-anchor",href:"#_2-3-tree","aria-hidden":"true"},"#"),t(" 2-3 Tree")],-1),P=a("p",null,"2-3 Tree is Balance Tree:",-1),L=a("p",null,"插入:",-1),K=a("ul",null,[a("li",null,"1+1=2node -> 3node"),a("li",null,[a("strong",null,"1+2=3node -> 4node"),t(" -> 2node")]),a("li",null,"将 4node 结点中间元素移至父结点, 其余 2 元素分离为子 2node 节点")],-1),q=a("h3",{id:"red-black-bst"},[a("a",{class:"header-anchor",href:"#red-black-bst","aria-hidden":"true"},"#"),t(" Red-Black BST")],-1),I=a("ul",null,[a("li",null,"基于 2-3Tree, 将 3node 用红色标记"),a("li",null,"关键: 将红色标记向上传递至根部")],-1),O=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// is node x red; false if x is null ?"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token keyword"},"boolean"),t(),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" x"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("x "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" x"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"=="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),H=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// make a right-leaning link lean to the left"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"rotateLeft"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null) && isRed(h.right);"),t("\n "),a("span",{class:"token class-name"},"Node"),t(" x "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},";"),t("\n x"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},";"),t("\n\n x"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n x"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n x"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},"."),t("size"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" x"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// make a left-leaning link lean to the right"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null) && isRed(h.left);"),t("\n "),a("span",{class:"token class-name"},"Node"),t(" x "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},";"),t("\n x"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},";"),t("\n\n x"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n x"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n x"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},"."),t("size"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" x"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// flip the colors of a node and its two children"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// h must have opposite color of its two children"),t("\n "),a("span",{class:"token comment"},"// assert (h != null) && (h.left != null) && (h.right != null);"),t("\n "),a("span",{class:"token comment"},"// assert (!isRed(h) && isRed(h.left) && isRed(h.right))"),t("\n "),a("span",{class:"token comment"},"// || (isRed(h) && !isRed(h.left) && !isRed(h.right));"),t("\n h"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"!"),t("h"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"!"),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"!"),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br")])],-1),G=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// insert the key-value pair in the subtree rooted at h"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Key"),t(" key"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Value"),t(" val"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// insert/put new node as left/right child of leaf node"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("h "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Node"),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},","),t(" val"),a("span",{class:"token punctuation"},","),t(" RED"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"int"),t(" cmp "),a("span",{class:"token operator"},"="),t(" key"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(" "),a("span",{class:"token punctuation"},"("),t("cmp "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(" h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" val"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("cmp "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(" h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" val"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(" h"),a("span",{class:"token punctuation"},"."),t("val "),a("span",{class:"token operator"},"="),t(" val"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// fix-up any right-leaning links"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token operator"},"&&"),t(" "),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token operator"},"&&"),t(" "),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" h"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Key"),t(" key"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Value"),t(" val"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"IllegalArgumentException"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"first argument to put() is null"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("val "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n root "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" val"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" BLACK"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// assert check();"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br")])],-1),_=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// Assuming that h is red and both h.left and h.left.left"),t("\n "),a("span",{class:"token comment"},"// are black, make h.left or one of its children red."),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"moveRedLeft"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null);"),t("\n "),a("span",{class:"token comment"},"// assert isRed(h) && !isRed(h.left) && !isRed(h.left.left);"),t("\n\n "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" h"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// Assuming that h is red and both h.right and h.right.left"),t("\n "),a("span",{class:"token comment"},"// are black, make h.right or one of its children red."),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"moveRedRight"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null);"),t("\n "),a("span",{class:"token comment"},"// assert isRed(h) && !isRed(h.right) && !isRed(h.right.left);"),t("\n "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" h"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// restore red-black tree invariant"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"balance"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null);"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" h"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br")])],-1),V=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// delete the key-value pair with the minimum key rooted at h"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"moveRedLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"balance"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"/**\n * Removes the smallest key and associated value from the symbol table.\n * @throws NoSuchElementException if the symbol table is empty\n */"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"NoSuchElementException"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"BST underflow"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// if both children of root are black, set root to red"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n root "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" BLACK"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// assert check();"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// delete the key-value pair with the maximum key rooted at h"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"deleteMax"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"moveRedRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMax"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"balance"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"/**\n * Removes the largest key and associated value from the symbol table.\n * @throws NoSuchElementException if the symbol table is empty\n */"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"deleteMax"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"NoSuchElementException"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"BST underflow"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// if both children of root are black, set root to red"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n root "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMax"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" BLACK"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// assert check();"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// delete the key-value pair with the given key rooted at h"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Key"),t(" key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert get(h, key) != null;"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"moveRedLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"moveRedRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"Node"),t(" x "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"min"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("key "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("val "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("val"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// h.val = get(h.right, min(h.right).key);"),t("\n "),a("span",{class:"token comment"},"// h.key = min(h.right).key;"),t("\n h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"else"),t(" h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"balance"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"/**\n * Removes the specified key and its associated value from this symbol table\n * (if the key is in this symbol table).\n *\n * @param key the key\n * @throws IllegalArgumentException if {@code key} is {@code null}\n */"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Key"),t(" key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"IllegalArgumentException"),a("span",{class:"token punctuation"},"("),t('"argument '),a("span",{class:"token keyword"},"to"),t("\n "),a("span",{class:"token namespace"},"delete"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(" is "),a("span",{class:"token keyword"},"null"),t('"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"contains"),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// if both children of root are black, set root to red"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n root "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" BLACK"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// assert check();"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br"),a("span",{class:"line-number"},"83"),a("br"),a("span",{class:"line-number"},"84"),a("br"),a("span",{class:"line-number"},"85"),a("br"),a("span",{class:"line-number"},"86"),a("br"),a("span",{class:"line-number"},"87"),a("br"),a("span",{class:"line-number"},"88"),a("br"),a("span",{class:"line-number"},"89"),a("br"),a("span",{class:"line-number"},"90"),a("br"),a("span",{class:"line-number"},"91"),a("br"),a("span",{class:"line-number"},"92"),a("br"),a("span",{class:"line-number"},"93"),a("br"),a("span",{class:"line-number"},"94"),a("br"),a("span",{class:"line-number"},"95"),a("br"),a("span",{class:"line-number"},"96"),a("br"),a("span",{class:"line-number"},"97"),a("br"),a("span",{class:"line-number"},"98"),a("br"),a("span",{class:"line-number"},"99"),a("br"),a("span",{class:"line-number"},"100"),a("br"),a("span",{class:"line-number"},"101"),a("br"),a("span",{class:"line-number"},"102"),a("br"),a("span",{class:"line-number"},"103"),a("br"),a("span",{class:"line-number"},"104"),a("br"),a("span",{class:"line-number"},"105"),a("br"),a("span",{class:"line-number"},"106"),a("br"),a("span",{class:"line-number"},"107"),a("br"),a("span",{class:"line-number"},"108"),a("br"),a("span",{class:"line-number"},"109"),a("br")])],-1),Q=a("h4",{id:"基本性质"},[a("a",{class:"header-anchor",href:"#基本性质","aria-hidden":"true"},"#"),t(" 基本性质")],-1),W=a("ol",null,[a("li",null,"非红即黑"),a("li",null,"根黑"),a("li",null,"叶黑 e.g T.null 黑哨兵"),a("li",null,"红父孩子黑"),a("li",null,"简单路径同黑"),a("li",null,"右孩子不红 e.g 父黑两孩红 -> 父红两孩黑(flip); 父黑右红 -> 父左旋变红, 右孩子变黑(left-rotate)")],-1),J=a("h4",{id:"基本操作"},[a("a",{class:"header-anchor",href:"#基本操作","aria-hidden":"true"},"#"),t(" 基本操作")],-1),U=a("ol",null,[a("li",null,"插入(插入红点, 旋转+重新着色(反色)保持红黑性质)"),a("li",null,"删除(删除红点, 旋转+重新着色(反色)保持红黑性质)")],-1),X=a("h3",{id:"b-tree"},[a("a",{class:"header-anchor",href:"#b-tree","aria-hidden":"true"},"#"),t(" B Tree")],-1),Y=a("p",null,"t: 每个内部结点至少 t 个孩子(t-1 个 key), 至多 2t 个孩子(2t-1 个 key)",-1),Z=a("h4",{id:"插入-删除"},[a("a",{class:"header-anchor",href:"#插入-删除","aria-hidden":"true"},"#"),t(" 插入/删除")],-1),$=a("p",null,"下溯的同时,分裂满结点",-1),nn=a("h3",{id:"fibonacci-heap"},[a("a",{class:"header-anchor",href:"#fibonacci-heap","aria-hidden":"true"},"#"),t(" Fibonacci Heap")],-1),sn=a("p",null,"BST + 循环双向链表:",-1),an=a("ul",null,[a("li",null,"一个根树(根结点)循环双向链表"),a("li",null,"n 个孩子循环双向链表: 每个根树的每层结点形成一个循环双向链表")],-1),tn=a("h3",{id:"k-dimensional-tree"},[a("a",{class:"header-anchor",href:"#k-dimensional-tree","aria-hidden":"true"},"#"),t(" K-Dimensional Tree")],-1),en=a("ul",null,[a("li",null,"分隔空间数据")],-1),on=a("p",null,"e.g 左子树:左下方 右子树:右上方",-1),ln=a("h2",{id:"search-algorithm"},[a("a",{class:"header-anchor",href:"#search-algorithm","aria-hidden":"true"},"#"),t(" Search Algorithm")],-1),cn=a("h3",{id:"first-search"},[a("a",{class:"header-anchor",href:"#first-search","aria-hidden":"true"},"#"),t(" First Search")],-1),pn=a("ul",null,[a("li",null,"DFS(深度优先):栈实现"),a("li",null,"BFS(广度优先):队列实现")],-1),un=a("p",null,[a("img",{src:"/awesome-notes/assets/SearchPerformance.866c6c75.jpg",alt:"Search Algorithm Performance"})],-1),rn=a("h3",{id:"cycle-detection"},[a("a",{class:"header-anchor",href:"#cycle-detection","aria-hidden":"true"},"#"),t(" cycle detection")],-1),kn=a("ul",null,[a("li",null,"许多图论算法不适用于存在环路的复杂图,故使用循环检测剔除意外情况")],-1),bn=a("p",null,"处理方法:可将环路元素(如强联通分支)视作单一元素,忽视其内部结构",-1),mn=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t("a "),a("span",{class:"token operator"},"="),t(" b"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("b "),a("span",{class:"token operator"},"="),t(" c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("c "),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"//a extends b;b extends c;c extends a;"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),dn=a("h2",{id:"dynamic-programming"},[a("a",{class:"header-anchor",href:"#dynamic-programming","aria-hidden":"true"},"#"),t(" Dynamic Programming")],-1),hn=a("ul",null,[a("li",null,[t("最优解结构特征: 一个选择 + 子问题的最优解 - 所有(可"),a("strong",null,"重复求解"),t(")子问题的最优解可"),a("strong",null,"独立求解"),t("(不互相影响)")]),a("li",null,"递归定义最优解: 列出递归表达式"),a("li",null,"自底向上求解最优解"),a("li",null,"构造最优解(额外信息数组)")],-1),fn=a("h3",{id:"子问题"},[a("a",{class:"header-anchor",href:"#子问题","aria-hidden":"true"},"#"),t(" 子问题")],-1),yn=a("ul",null,[a("li",null,"子问题可映射为有向图, 并对其进行拓扑排序: 共有 O(n) 个子问题, 每个子问题最多 O(n) 种选择, 则算法时间复杂度为 O(n^2).其对应子问题图有 n 个顶点, 每个顶点最多有 n-1 条边."),a("li",null,"递归生成可以重复求解的子问题,而不是不断生成新的子问题")],-1),gn=a("h3",{id:"范例"},[a("a",{class:"header-anchor",href:"#范例","aria-hidden":"true"},"#"),t(" 范例")],-1),wn=a("ul",null,[a("li",null,"切割钢条问题: max{p[i], r[n-i]}"),a("li",null,"矩阵相乘链问题"),a("li",null,"最大公共子序列问题: r[i, j] = max{r[i, j-1], r[i-1, j]}"),a("li",null,"无权最短路径: path[i, j] = min{path[i, r], [r, j]}")],-1),vn=a("h2",{id:"greedy-algorithm"},[a("a",{class:"header-anchor",href:"#greedy-algorithm","aria-hidden":"true"},"#"),t(" Greedy Algorithm")],-1),xn=a("ul",null,[a("li",null,[t("最优解结构特征: 一个选择 + 子问题的最优解 - 所有(可"),a("strong",null,"重复求解"),t(")子问题的最优解可"),a("strong",null,"独立求解"),t("(不互相影响)")]),a("li",null,"递归定义最优解: 列出递归表达式"),a("li",null,[t("自底向上求解最优解: 每次不进行多次选择, 只进行一次 "),a("strong",null,"贪心选择")]),a("li",null,"构造最优解(额外信息数组)")],-1),Rn=a("h2",{id:"map-algorithm"},[a("a",{class:"header-anchor",href:"#map-algorithm","aria-hidden":"true"},"#"),t(" Map Algorithm")],-1),Sn=a("h3",{id:"图的表示"},[a("a",{class:"header-anchor",href:"#图的表示","aria-hidden":"true"},"#"),t(" 图的表示")],-1),Nn=a("ul",null,[a("li",null,"邻接链表法"),a("li",null,"邻接矩阵法")],-1),jn=a("h4",{id:"稀疏矩阵"},[a("a",{class:"header-anchor",href:"#稀疏矩阵","aria-hidden":"true"},"#"),t(" 稀疏矩阵")],-1),An=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[t("unordered_map"),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},","),t(" unordered_map"),a("span",{class:"token operator"},"<"),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token operator"},">"),t(),a("span",{class:"token operator"},">"),t(),a("span",{class:"token comment"},"// => (row, (col, val))"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Tn=a("h3",{id:"广度优先遍历"},[a("a",{class:"header-anchor",href:"#广度优先遍历","aria-hidden":"true"},"#"),t(" 广度优先遍历")],-1),Dn=a("h4",{id:"bfs-node-color"},[a("a",{class:"header-anchor",href:"#bfs-node-color","aria-hidden":"true"},"#"),t(" BFS Node Color")],-1),En=a("ul",null,[a("li",null,"white: 未被发现/访问"),a("li",null,"gray: 已被发现(进入队列), 邻接结点未全部发现"),a("li",null,"black: 已被发现, 邻接结点全部发现")],-1),Bn=a("h4",{id:"bfs-node-parent"},[a("a",{class:"header-anchor",href:"#bfs-node-parent","aria-hidden":"true"},"#"),t(" BFS Node Parent")],-1),Mn=a("p",null,"广度优先树父结点",-1),Fn=a("h4",{id:"bfs-node-distance"},[a("a",{class:"header-anchor",href:"#bfs-node-distance","aria-hidden":"true"},"#"),t(" BFS Node Distance")],-1),zn=a("p",null,"距离 = v.pi.d + 1",-1),Cn=a("h4",{id:"利用队列实现广度优先遍历"},[a("a",{class:"header-anchor",href:"#利用队列实现广度优先遍历","aria-hidden":"true"},"#"),t(" 利用队列实现广度优先遍历")],-1),Pn=a("h3",{id:"深度优先遍历"},[a("a",{class:"header-anchor",href:"#深度优先遍历","aria-hidden":"true"},"#"),t(" 深度优先遍历")],-1),Ln=a("p",null,"利用 递归/栈 实现深度优先遍历",-1),Kn=a("h4",{id:"dfs-node-color"},[a("a",{class:"header-anchor",href:"#dfs-node-color","aria-hidden":"true"},"#"),t(" DFS Node Color")],-1),qn=a("ul",null,[a("li",null,"white: 未被发现/访问"),a("li",null,"gray: 已被发现, 未二次访问"),a("li",null,"black: 已被发现, 二次访问(比其深的所有结点皆被发现)")],-1),In=a("p",null,"当第一个访问 edge(u,v) 时:",-1),On=a("ul",null,[a("li",null,"v.color == white: 树边"),a("li",null,[t("v.color == gray : 后向边(v 为 深度优先*"),a("em",null,"森林"),t("- 的祖父结点)")]),a("li",null,"v.color == black: 前向边/横向边(v 为较深的结点/子结点)"),a("li",null,"无向图深度优先遍历不会出现 前向边/横向边")],-1),Hn=a("h4",{id:"dfs-node-parent"},[a("a",{class:"header-anchor",href:"#dfs-node-parent","aria-hidden":"true"},"#"),t(" DFS Node Parent")],-1),Gn=a("p",null,"比 v 浅的结点(比 v 更早被发现的结点)",-1),_n=a("h4",{id:"dfs-node-distance"},[a("a",{class:"header-anchor",href:"#dfs-node-distance","aria-hidden":"true"},"#"),t(" DFS Node Distance")],-1),Vn=a("ul",null,[a("li",null,"v.d = ++time: 被发现的时间戳(入栈)"),a("li",null,"v.f = ++time: 被二次访问的时间戳(出栈)"),a("li",null,[t("time"),a("code",null,"<"),t("v.d, white; v.d"),a("code",null,"<"),t("time"),a("code",null,"<"),t("v.f, gray: time"),a("code",null,">"),t("v.f, black")])],-1),Qn=a("h3",{id:"拓扑排序"},[a("a",{class:"header-anchor",href:"#拓扑排序","aria-hidden":"true"},"#"),t(" 拓扑排序")],-1),Wn=a("p",null,"目标集合: 拓扑排序后集合, 先入顶点高序, 后入顶点低序",-1),Jn=a("h4",{id:"kahn-算法"},[a("a",{class:"header-anchor",href:"#kahn-算法","aria-hidden":"true"},"#"),t(" Kahn 算法")],-1),Un=a("p",null,"不断将图中入度为 0 的点移入目标集合",-1),Xn=a("h4",{id:"dfs-深度优先"},[a("a",{class:"header-anchor",href:"#dfs-深度优先","aria-hidden":"true"},"#"),t(" DFS(深度优先)")],-1),Yn=a("p",null,"当深度遍历至较深处, 并开始回溯时, 将此时访问的顶点加入目标集合(v.f 降序)",-1),Zn=a("h3",{id:"单源最短路径"},[a("a",{class:"header-anchor",href:"#单源最短路径","aria-hidden":"true"},"#"),t(" 单源最短路径")],-1),$n=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"Relax"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" u"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" v"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" w"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(" v"),a("span",{class:"token punctuation"},"."),t("d "),a("span",{class:"token operator"},">"),t(" u"),a("span",{class:"token punctuation"},"."),t("d "),a("span",{class:"token operator"},"+"),t(" w"),a("span",{class:"token punctuation"},"["),t("u"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("v"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token punctuation"},"{"),t("\n v"),a("span",{class:"token punctuation"},"."),t("pi "),a("span",{class:"token operator"},"="),t(" u"),a("span",{class:"token punctuation"},";"),t("\n v"),a("span",{class:"token punctuation"},"."),t("d "),a("span",{class:"token operator"},"="),t(" v"),a("span",{class:"token punctuation"},"."),t("pi"),a("span",{class:"token punctuation"},"."),t("d "),a("span",{class:"token operator"},"+"),t(" w"),a("span",{class:"token punctuation"},"["),t("v"),a("span",{class:"token punctuation"},"."),t("pi"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("v"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),ns=a("h4",{id:"dag-shortest-paths"},[a("a",{class:"header-anchor",href:"#dag-shortest-paths","aria-hidden":"true"},"#"),t(" DAG Shortest Paths")],-1),ss=a("p",null,"先将图进行拓扑排序(深度优先遍历), 再按照拓扑排序顺序, 依次对每个结点(拓扑排序)的邻接边进行 relax",-1),as=a("blockquote",null,[a("p",null,"a -> b -> c --\x3e d, 且 a--b, a--c, b--d, c--d: relax(a, b), relax(a, c), relax(b, d), relax(c, d)")],-1),ts=a("h4",{id:"bellman-ford-algorithm"},[a("a",{class:"header-anchor",href:"#bellman-ford-algorithm","aria-hidden":"true"},"#"),t(" Bellman-Ford Algorithm")],-1),es=a("p",null,"对每条边进行 n 次(结点总数) relax",-1),os=a("h4",{id:"dijkstra-algorithm"},[a("a",{class:"header-anchor",href:"#dijkstra-algorithm","aria-hidden":"true"},"#"),t(" Dijkstra Algorithm")],-1),ls=a("p",null,"贪心算法: 每次选取不属于 S 集合(white) 且 v.d 最小(gray)的结点, 对其所有邻接边进行 relax, 并将其加入 S 集合(black)",-1),cs=a("ul",null,[a("li",null,"white: 不属于 S 集合"),a("li",null,"gray: 不属于 S 集合 且 v.d 最小"),a("li",null,"black: 属于 S 集合")],-1),ps=a("h3",{id:"结点对最短路径"},[a("a",{class:"header-anchor",href:"#结点对最短路径","aria-hidden":"true"},"#"),t(" 结点对最短路径")],-1),us=a("p",null,"动态规划:l^m(i, j) = min(l^m-1(i, j), min(1<=k<=n){l^m-1(i, k)+w(k, j)})",-1),is=a("blockquote",null,[a("p",null,"m: 中间结点个数")],-1),rs=a("h4",{id:"floyd-warshall-algorithm"},[a("a",{class:"header-anchor",href:"#floyd-warshall-algorithm","aria-hidden":"true"},"#"),t(" Floyd-Warshall Algorithm")],-1),ks=a("p",null,"d^k(i, j) = w(i, j), k = 0 | min(d^k-1(i, j), d^k-1(i, k) + d^k-1(k, j)), k >= 1",-1),bs=a("p",null,"pi^(i, j) = pi^k-1(i, j) or pi^k-1(k, j)",-1),ms=a("blockquote",null,[a("p",null,"k: 中间结点个数")],-1),ds=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[t("Matrix "),a("span",{class:"token function"},"floyd_warshall"),a("span",{class:"token punctuation"},"("),t("Matrix W"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" n "),a("span",{class:"token operator"},"="),t(" W"),a("span",{class:"token punctuation"},"."),t("rows"),a("span",{class:"token punctuation"},";"),t("\n Matrix D"),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"0"),t(),a("span",{class:"token operator"},"="),t(" W"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("k "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" k"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n D"),a("span",{class:"token operator"},"^"),t("k "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token function"},"Matrix"),a("span",{class:"token punctuation"},"("),t("n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" j "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" j "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" J"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n d"),a("span",{class:"token operator"},"^"),t("k"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"min"),a("span",{class:"token punctuation"},"("),t("d"),a("span",{class:"token operator"},"^"),t("k"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t(" d"),a("span",{class:"token operator"},"^"),t("k"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token operator"},"+"),t("d"),a("span",{class:"token operator"},"^"),t("k"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" D"),a("span",{class:"token operator"},"^"),t("n"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),hs=a("h3",{id:"最大流问题"},[a("a",{class:"header-anchor",href:"#最大流问题","aria-hidden":"true"},"#"),t(" 最大流问题")],-1),fs=a("p",null,"MaxFlow Problem:",-1),ys=a("p",null,[a("img",{src:"/awesome-notes/assets/FordFulkersonAlgorithm.94aecb4b.png",alt:"Ford Fulkerson Algorithm"})],-1),gs=a("h4",{id:"最大流模型"},[a("a",{class:"header-anchor",href:"#最大流模型","aria-hidden":"true"},"#"),t(" 最大流模型")],-1),ws=a("p",null,"最大流模型必须满足以下条件:",-1),vs=a("ul",null,[a("li",null,"无双向边"),a("li",null,"唯一的源点 s 和 唯一的汇点 t")],-1),xs=a("p",null,"对于不符合该模型的问题可进行简单转化:",-1),Rs=a("ul",null,[a("li",null,"双向边: 添加额外结点, 切割双向边的其中一条, 使得双向边变成 3 条单向边")],-1),Ss=a("blockquote",null,[a("p",null,"a --\x3e b, b --\x3e a: a --\x3e c, c --\x3e b, b --\x3e a")],-1),Ns=a("ul",null,[a("li",null,"多源点/汇点: 添加一个总源点/汇点")],-1),js=a("h4",{id:"残存网络"},[a("a",{class:"header-anchor",href:"#残存网络","aria-hidden":"true"},"#"),t(" 残存网络")],-1),As=a("ul",null,[a("li",null,"若原图 u --\x3e v 总容量 > 0, 则残存网络中 边 u --\x3e v:剩余容量, 边 v --\x3e u: 已用容量"),a("li",null,"增广路径: 残存网络中一条可行通路")],-1),Ts=a("h4",{id:"最大流最小割定理"},[a("a",{class:"header-anchor",href:"#最大流最小割定理","aria-hidden":"true"},"#"),t(" 最大流最小割定理")],-1),Ds=a("p",null,"MaxFlow-MinCut Theorem:",-1),Es=a("ul",null,[a("li",null,"切割的净流量: 流出-流入"),a("li",null,"切割的容量: 流出总容量(无需减流入总容量)"),a("li",null,"最小切割: 容量最小的切割")],-1),Bs=a("p",null,"最大流最小割定理: 以下三个命题等价",-1),Ms=a("ul",null,[a("li",null,"f 是 G 的一个最大流"),a("li",null,"残存网络 Gf 不含增广路径"),a("li",null,"|f| = c(S, T)(切割的容量): |f| <= c(S, T)(流网络中任意流 f <= 任意切割容量 c(S, T))")],-1),Fs=a("h4",{id:"ford-fulkerson-algorithm"},[a("a",{class:"header-anchor",href:"#ford-fulkerson-algorithm","aria-hidden":"true"},"#"),t(" Ford-Fulkerson Algorithm")],-1),zs=a("p",null,"不断寻找增广路径",-1),Cs=a("h2",{id:"tree-edit-distance"},[a("a",{class:"header-anchor",href:"#tree-edit-distance","aria-hidden":"true"},"#"),t(" Tree Edit Distance")],-1),Ps=a("h3",{id:"definition"},[a("a",{class:"header-anchor",href:"#definition","aria-hidden":"true"},"#"),t(" Definition")],-1),Ls=a("p",null,"Tree Edit Distance: 给定 Cost(edit operation) 时的最小编辑费用",-1);o.render=function(a,t,e,o,Ks,qs){return n(),s("div",null,[l,c,p,u,i,r,k,b,m,d,h,f,y,g,w,v,x,R,S,N,j,A,T,D,E,B,M,F,z,C,P,L,K,q,I,O,H,G,_,V,Q,W,J,U,X,Y,Z,$,nn,sn,an,tn,en,on,ln,cn,pn,un,rn,kn,bn,mn,dn,hn,fn,yn,gn,wn,vn,xn,Rn,Sn,Nn,jn,An,Tn,Dn,En,Bn,Mn,Fn,zn,Cn,Pn,Ln,Kn,qn,In,On,Hn,Gn,_n,Vn,Qn,Wn,Jn,Un,Xn,Yn,Zn,$n,ns,ss,as,ts,es,os,ls,cs,ps,us,is,rs,ks,bs,ms,ds,hs,fs,ys,gs,ws,vs,xs,Rs,Ss,Ns,js,As,Ts,Ds,Es,Bs,Ms,Fs,zs,Cs,Ps,Ls])};export default o;export{e as __pageData}; diff --git a/assets/computerScience_algorithms_algorithmsBasicNotes.md.36b13240.lean.js b/assets/computerScience_algorithms_algorithmsBasicNotes.md.36b13240.lean.js new file mode 100644 index 00000000000..213793ab71e --- /dev/null +++ b/assets/computerScience_algorithms_algorithmsBasicNotes.md.36b13240.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as t}from"./app.6dd2a1b7.js";const e='{"title":"Algorithm Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Sorting Algorithm","slug":"sorting-algorithm"},{"level":3,"title":"Summary","slug":"summary"},{"level":3,"title":"Selection Sort","slug":"selection-sort"},{"level":3,"title":"Insertion Sort","slug":"insertion-sort"},{"level":3,"title":"Shell Sort","slug":"shell-sort"},{"level":3,"title":"Merge Sort","slug":"merge-sort"},{"level":3,"title":"Quick Sort","slug":"quick-sort"},{"level":3,"title":"Heap Sort","slug":"heap-sort"},{"level":3,"title":"Radix Sort","slug":"radix-sort"},{"level":2,"title":"Tree Algorithm","slug":"tree-algorithm"},{"level":3,"title":"Binary Search Tree","slug":"binary-search-tree"},{"level":3,"title":"2-3 Tree","slug":"_2-3-tree"},{"level":3,"title":"Red-Black BST","slug":"red-black-bst"},{"level":3,"title":"B Tree","slug":"b-tree"},{"level":3,"title":"Fibonacci Heap","slug":"fibonacci-heap"},{"level":3,"title":"K-Dimensional Tree","slug":"k-dimensional-tree"},{"level":2,"title":"Search Algorithm","slug":"search-algorithm"},{"level":3,"title":"First Search","slug":"first-search"},{"level":3,"title":"cycle detection","slug":"cycle-detection"},{"level":2,"title":"Dynamic Programming","slug":"dynamic-programming"},{"level":3,"title":"子问题","slug":"子问题"},{"level":3,"title":"范例","slug":"范例"},{"level":2,"title":"Greedy Algorithm","slug":"greedy-algorithm"},{"level":2,"title":"Map Algorithm","slug":"map-algorithm"},{"level":3,"title":"图的表示","slug":"图的表示"},{"level":3,"title":"广度优先遍历","slug":"广度优先遍历"},{"level":3,"title":"深度优先遍历","slug":"深度优先遍历"},{"level":3,"title":"拓扑排序","slug":"拓扑排序"},{"level":3,"title":"单源最短路径","slug":"单源最短路径"},{"level":3,"title":"结点对最短路径","slug":"结点对最短路径"},{"level":3,"title":"最大流问题","slug":"最大流问题"},{"level":2,"title":"Tree Edit Distance","slug":"tree-edit-distance"},{"level":3,"title":"Definition","slug":"definition"}],"relativePath":"computerScience/algorithms/algorithmsBasicNotes.md","lastUpdated":1627194884000}',o={},l=a("h1",{id:"algorithm-basic-notes"},[a("a",{class:"header-anchor",href:"#algorithm-basic-notes","aria-hidden":"true"},"#"),t(" Algorithm Basic Notes")],-1),c=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#algorithm-basic-notes"},"Algorithm Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#sorting-algorithm"},"Sorting Algorithm")]),a("li",null,[a("a",{href:"#tree-algorithm"},"Tree Algorithm")]),a("li",null,[a("a",{href:"#search-algorithm"},"Search Algorithm")]),a("li",null,[a("a",{href:"#dynamic-programming"},"Dynamic Programming")]),a("li",null,[a("a",{href:"#greedy-algorithm"},"Greedy Algorithm")]),a("li",null,[a("a",{href:"#map-algorithm"},"Map Algorithm")]),a("li",null,[a("a",{href:"#tree-edit-distance"},"Tree Edit Distance")])])])])])],-1),p=a("h2",{id:"sorting-algorithm"},[a("a",{class:"header-anchor",href:"#sorting-algorithm","aria-hidden":"true"},"#"),t(" Sorting Algorithm")],-1),u=a("h3",{id:"summary"},[a("a",{class:"header-anchor",href:"#summary","aria-hidden":"true"},"#"),t(" Summary")],-1),i=a("ul",null,[a("li",null,"强制稳定: 增加(唯一)时间戳, 修改 CompareTo 接口定义 => 当主元素相同时, 时间戳小的元素更小")],-1),r=a("h3",{id:"selection-sort"},[a("a",{class:"header-anchor",href:"#selection-sort","aria-hidden":"true"},"#"),t(" Selection Sort")],-1),k=a("ul",null,[a("li",null,"swap: O(n)"),a("li",null,"compare: O(n^2)")],-1),b=a("h3",{id:"insertion-sort"},[a("a",{class:"header-anchor",href:"#insertion-sort","aria-hidden":"true"},"#"),t(" Insertion Sort")],-1),m=a("ul",null,[a("li",null,"swap: O(n^2/4)"),a("li",null,"compare: O(n^2/4)")],-1),d=a("h3",{id:"shell-sort"},[a("a",{class:"header-anchor",href:"#shell-sort","aria-hidden":"true"},"#"),t(" Shell Sort")],-1),h=a("ul",null,[a("li",null,"swap: O(n^2/4)"),a("li",null,"compare: O(n^2/4)")],-1),f=a("h3",{id:"merge-sort"},[a("a",{class:"header-anchor",href:"#merge-sort","aria-hidden":"true"},"#"),t(" Merge Sort")],-1),y=a("ul",null,[a("li",null,"利用 Merge Sort 计算逆序对个数: left[i] > right[j] => inversions += (mid - i + 1), 即所有 i~mid 元素都与 j 元素为逆序对")],-1),g=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// merge and count"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token keyword"},"static"),t(),a("span",{class:"token keyword"},"long"),t(),a("span",{class:"token function"},"merge"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" a"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" aux"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" lo"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" mid"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" hi"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"long"),t(" inversions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// copy to aux[]"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token punctuation"},";"),t(" k "),a("span",{class:"token operator"},"<="),t(" hi"),a("span",{class:"token punctuation"},";"),t(" k"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n aux"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// merge back to a[]"),t("\n "),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token punctuation"},","),t(" j "),a("span",{class:"token operator"},"="),t(" mid"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token punctuation"},";"),t(" k "),a("span",{class:"token operator"},"<="),t(" hi"),a("span",{class:"token punctuation"},";"),t(" k"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(" "),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},">"),t(" mid"),a("span",{class:"token punctuation"},")"),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" aux"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("j "),a("span",{class:"token operator"},">"),t(" hi"),a("span",{class:"token punctuation"},")"),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" aux"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("aux"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"<"),t(" aux"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" aux"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t(" inversions "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token punctuation"},"("),t("mid "),a("span",{class:"token operator"},"-"),t("\n i "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"else"),t(" a"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" aux"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" inversions"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// return the number of inversions in the subArray b[lo..hi]"),t("\n "),a("span",{class:"token comment"},"// side effect b[lo..hi] is rearranged in ascending order"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token keyword"},"static"),t(),a("span",{class:"token keyword"},"long"),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" a"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" b"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" aux"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" lo"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" hi"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"long"),t(" inversions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("hi "),a("span",{class:"token operator"},"<="),t(" lo"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),t(" mid "),a("span",{class:"token operator"},"="),t(" lo "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token punctuation"},"("),t("hi "),a("span",{class:"token operator"},"-"),t(" lo"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),t("\n inversions "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" b"),a("span",{class:"token punctuation"},","),t(" aux"),a("span",{class:"token punctuation"},","),t(" lo"),a("span",{class:"token punctuation"},","),t(" mid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n inversions "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" b"),a("span",{class:"token punctuation"},","),t(" aux"),a("span",{class:"token punctuation"},","),t(" mid"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(" hi"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n inversions "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token function"},"merge"),a("span",{class:"token punctuation"},"("),t("b"),a("span",{class:"token punctuation"},","),t(" aux"),a("span",{class:"token punctuation"},","),t(" lo"),a("span",{class:"token punctuation"},","),t(" mid"),a("span",{class:"token punctuation"},","),t(" hi"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"assert"),t(" inversions "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token function"},"brute"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" lo"),a("span",{class:"token punctuation"},","),t(" hi"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" inversions"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n\n "),a("span",{class:"token comment"},"/**\n * Returns the number of inversions in the integer array.\n * The argument array is not modified.\n * @param a the array\n * @return the number of inversions in the array. An inversion is a pair of\n * indices {@code i} and {@code j} such that {@code i < j}\n * and {@code a[i]} > {@code a[j]}.\n */"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"static"),t(),a("span",{class:"token keyword"},"long"),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" a"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" b "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),t("a"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" aux "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),t("a"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" a"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t("\n b"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"long"),t(" inversions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" b"),a("span",{class:"token punctuation"},","),t(" aux"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(" a"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"-"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" inversions"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br")])],-1),w=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// return Kendall tau distance between two permutations"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"static"),t(),a("span",{class:"token keyword"},"long"),t(),a("span",{class:"token function"},"distance"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" a"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" b"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"!="),t(" b"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"IllegalArgumentException"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Array dimensions disagree"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"int"),t(" n "),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" ainV "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},"["),t("n"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t("\n ainV"),a("span",{class:"token punctuation"},"["),t("a"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" i"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token class-name"},"Integer"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),t(" bNew "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Integer"),a("span",{class:"token punctuation"},"["),t("n"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t("\n bNew"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" ainV"),a("span",{class:"token punctuation"},"["),t("b"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token class-name"},"Inversions"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"count"),a("span",{class:"token punctuation"},"("),t("bNew"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),v=a("h3",{id:"quick-sort"},[a("a",{class:"header-anchor",href:"#quick-sort","aria-hidden":"true"},"#"),t(" Quick Sort")],-1),x=a("ul",null,[a("li",null,"partition: 哨兵(最后再将其归位) + 大循环 + 2 小循环, 交换元素法"),a("li",null,"partition: 辅助数组 brr, 3 循环(3 次扫描 arr) 分别将小/等/大于 guard 的数加入 brr"),a("li",null,"partition: 哨兵(最后再将其归位) + lo + hi, 外加 2 个动指针 leftLimit 与 rightLimit, 表示小于区的上界和大于区的上界")],-1),R=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// lt eq gt three parts"),t("\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"quick3waySort"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token operator"},"*"),t("a"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" lo"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" hi"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("hi "),a("span",{class:"token operator"},"<="),t(" lo"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),t(" lt "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token punctuation"},","),t(" i "),a("span",{class:"token operator"},"="),t(" lo"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(" gt "),a("span",{class:"token operator"},"="),t(" hi"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),t(" v "),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"["),t("lo"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"<="),t(" gt"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" cmp "),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(" "),a("span",{class:"token punctuation"},"("),t("cmp "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token function"},"exch"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" lt"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},","),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("cmp "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token function"},"exch"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" i"),a("span",{class:"token punctuation"},","),t(" gt"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"sort"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" lo"),a("span",{class:"token punctuation"},","),t(" lt "),a("span",{class:"token operator"},"-"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"sort"),a("span",{class:"token punctuation"},"("),t("a"),a("span",{class:"token punctuation"},","),t(" gt "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(" hi"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),S=a("h3",{id:"heap-sort"},[a("a",{class:"header-anchor",href:"#heap-sort","aria-hidden":"true"},"#"),t(" Heap Sort")],-1),N=a("ul",null,[a("li",null,"Built on Priority Queue"),a("li",null,"swap: 2NlgN + 2N (2NlgN for sink N times, 2N for construct MaxHeap)"),a("li",null,"compare: NlgN + N (NlgN for sink N times, N for construct MaxHeap)")],-1),j=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// MaxPQ"),t("\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"swim"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("k "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"1"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token function"},"less"),a("span",{class:"token punctuation"},"("),t("k"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" k"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"exch"),a("span",{class:"token punctuation"},"("),t("k"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" k"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n k "),a("span",{class:"token operator"},"="),t(" k"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"sink"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"*"),t("k "),a("span",{class:"token operator"},"<="),t(" N"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" j "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"*"),t("k"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("j "),a("span",{class:"token operator"},"<"),t(" N "),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token function"},"less"),a("span",{class:"token punctuation"},"("),t("j"),a("span",{class:"token punctuation"},","),t(" j"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" j"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"less"),a("span",{class:"token punctuation"},"("),t("k"),a("span",{class:"token punctuation"},","),t(" j"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"break"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exch"),a("span",{class:"token punctuation"},"("),t("k"),a("span",{class:"token punctuation"},","),t(" j"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n k "),a("span",{class:"token operator"},"="),t(" j"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),A=a("h3",{id:"radix-sort"},[a("a",{class:"header-anchor",href:"#radix-sort","aria-hidden":"true"},"#"),t(" Radix Sort")],-1),T=a("p",null,"基数排序 (可用于混乱 shuffle 数组):",-1),D=a("ul",null,[a("li",null,"从个位到高位放入桶"),a("li",null,"从高位到个位放入桶")],-1),E=a("blockquote",null,[a("p",null,"Sorting Algorithms Performance")],-1),B=a("p",null,[a("img",{src:"/awesome-notes/assets/SortingPerformance.caae88a8.png",alt:"Performance"})],-1),M=a("h2",{id:"tree-algorithm"},[a("a",{class:"header-anchor",href:"#tree-algorithm","aria-hidden":"true"},"#"),t(" Tree Algorithm")],-1),F=a("h3",{id:"binary-search-tree"},[a("a",{class:"header-anchor",href:"#binary-search-tree","aria-hidden":"true"},"#"),t(" Binary Search Tree")],-1),z=a("p",null,"Hibbard Deletion",-1),C=a("h3",{id:"_2-3-tree"},[a("a",{class:"header-anchor",href:"#_2-3-tree","aria-hidden":"true"},"#"),t(" 2-3 Tree")],-1),P=a("p",null,"2-3 Tree is Balance Tree:",-1),L=a("p",null,"插入:",-1),K=a("ul",null,[a("li",null,"1+1=2node -> 3node"),a("li",null,[a("strong",null,"1+2=3node -> 4node"),t(" -> 2node")]),a("li",null,"将 4node 结点中间元素移至父结点, 其余 2 元素分离为子 2node 节点")],-1),q=a("h3",{id:"red-black-bst"},[a("a",{class:"header-anchor",href:"#red-black-bst","aria-hidden":"true"},"#"),t(" Red-Black BST")],-1),I=a("ul",null,[a("li",null,"基于 2-3Tree, 将 3node 用红色标记"),a("li",null,"关键: 将红色标记向上传递至根部")],-1),O=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// is node x red; false if x is null ?"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token keyword"},"boolean"),t(),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" x"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("x "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" x"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"=="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),H=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// make a right-leaning link lean to the left"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"rotateLeft"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null) && isRed(h.right);"),t("\n "),a("span",{class:"token class-name"},"Node"),t(" x "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},";"),t("\n x"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},";"),t("\n\n x"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n x"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n x"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},"."),t("size"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" x"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// make a left-leaning link lean to the right"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null) && isRed(h.left);"),t("\n "),a("span",{class:"token class-name"},"Node"),t(" x "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},";"),t("\n x"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},";"),t("\n\n x"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n x"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n x"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(" h"),a("span",{class:"token punctuation"},"."),t("size"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" x"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// flip the colors of a node and its two children"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// h must have opposite color of its two children"),t("\n "),a("span",{class:"token comment"},"// assert (h != null) && (h.left != null) && (h.right != null);"),t("\n "),a("span",{class:"token comment"},"// assert (!isRed(h) && isRed(h.left) && isRed(h.right))"),t("\n "),a("span",{class:"token comment"},"// || (isRed(h) && !isRed(h.left) && !isRed(h.right));"),t("\n h"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"!"),t("h"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"!"),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"!"),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("color"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br")])],-1),G=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// insert the key-value pair in the subtree rooted at h"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Key"),t(" key"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Value"),t(" val"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// insert/put new node as left/right child of leaf node"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("h "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Node"),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},","),t(" val"),a("span",{class:"token punctuation"},","),t(" RED"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"int"),t(" cmp "),a("span",{class:"token operator"},"="),t(" key"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(" "),a("span",{class:"token punctuation"},"("),t("cmp "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(" h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" val"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("cmp "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(" h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" val"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"else"),t(" h"),a("span",{class:"token punctuation"},"."),t("val "),a("span",{class:"token operator"},"="),t(" val"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// fix-up any right-leaning links"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token operator"},"&&"),t(" "),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token operator"},"&&"),t(" "),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" h"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Key"),t(" key"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Value"),t(" val"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"IllegalArgumentException"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"first argument to put() is null"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("val "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n root "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" val"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" BLACK"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// assert check();"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br")])],-1),_=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// Assuming that h is red and both h.left and h.left.left"),t("\n "),a("span",{class:"token comment"},"// are black, make h.left or one of its children red."),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"moveRedLeft"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null);"),t("\n "),a("span",{class:"token comment"},"// assert isRed(h) && !isRed(h.left) && !isRed(h.left.left);"),t("\n\n "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" h"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// Assuming that h is red and both h.right and h.right.left"),t("\n "),a("span",{class:"token comment"},"// are black, make h.right or one of its children red."),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"moveRedRight"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null);"),t("\n "),a("span",{class:"token comment"},"// assert isRed(h) && !isRed(h.right) && !isRed(h.right.left);"),t("\n "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" h"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// restore red-black tree invariant"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"balance"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert (h != null);"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token function"},"flipColors"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("size "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token function"},"size"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" h"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br")])],-1),V=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t(" "),a("span",{class:"token comment"},"// delete the key-value pair with the minimum key rooted at h"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"moveRedLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"balance"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"/**\n * Removes the smallest key and associated value from the symbol table.\n * @throws NoSuchElementException if the symbol table is empty\n */"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"NoSuchElementException"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"BST underflow"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// if both children of root are black, set root to red"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n root "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" BLACK"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// assert check();"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// delete the key-value pair with the maximum key rooted at h"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"deleteMax"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"moveRedRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMax"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"balance"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"/**\n * Removes the largest key and associated value from the symbol table.\n * @throws NoSuchElementException if the symbol table is empty\n */"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"deleteMax"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"NoSuchElementException"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"BST underflow"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// if both children of root are black, set root to red"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n root "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMax"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" BLACK"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// assert check();"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// delete the key-value pair with the given key rooted at h"),t("\n "),a("span",{class:"token keyword"},"private"),t(),a("span",{class:"token class-name"},"Node"),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Node"),t(" h"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Key"),t(" key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// assert get(h, key) != null;"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"moveRedLeft"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"rotateRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n h "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"moveRedRight"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"compareTo"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"Node"),t(" x "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"min"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("key "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("key"),a("span",{class:"token punctuation"},";"),t("\n h"),a("span",{class:"token punctuation"},"."),t("val "),a("span",{class:"token operator"},"="),t(" x"),a("span",{class:"token punctuation"},"."),t("val"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// h.val = get(h.right, min(h.right).key);"),t("\n "),a("span",{class:"token comment"},"// h.key = min(h.right).key;"),t("\n h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"deleteMin"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"else"),t(" h"),a("span",{class:"token punctuation"},"."),t("right "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"balance"),a("span",{class:"token punctuation"},"("),t("h"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"/**\n * Removes the specified key and its associated value from this symbol table\n * (if the key is in this symbol table).\n *\n * @param key the key\n * @throws IllegalArgumentException if {@code key} is {@code null}\n */"),t("\n "),a("span",{class:"token keyword"},"public"),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Key"),t(" key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("key "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"throw"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"IllegalArgumentException"),a("span",{class:"token punctuation"},"("),t('"argument '),a("span",{class:"token keyword"},"to"),t("\n "),a("span",{class:"token namespace"},"delete"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(" is "),a("span",{class:"token keyword"},"null"),t('"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"contains"),a("span",{class:"token punctuation"},"("),t("key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// if both children of root are black, set root to red"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("left"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isRed"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},"."),t("right"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" RED"),a("span",{class:"token punctuation"},";"),t("\n\n root "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"delete"),a("span",{class:"token punctuation"},"("),t("root"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),a("span",{class:"token function"},"isEmpty"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(" root"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" BLACK"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// assert check();"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br"),a("span",{class:"line-number"},"83"),a("br"),a("span",{class:"line-number"},"84"),a("br"),a("span",{class:"line-number"},"85"),a("br"),a("span",{class:"line-number"},"86"),a("br"),a("span",{class:"line-number"},"87"),a("br"),a("span",{class:"line-number"},"88"),a("br"),a("span",{class:"line-number"},"89"),a("br"),a("span",{class:"line-number"},"90"),a("br"),a("span",{class:"line-number"},"91"),a("br"),a("span",{class:"line-number"},"92"),a("br"),a("span",{class:"line-number"},"93"),a("br"),a("span",{class:"line-number"},"94"),a("br"),a("span",{class:"line-number"},"95"),a("br"),a("span",{class:"line-number"},"96"),a("br"),a("span",{class:"line-number"},"97"),a("br"),a("span",{class:"line-number"},"98"),a("br"),a("span",{class:"line-number"},"99"),a("br"),a("span",{class:"line-number"},"100"),a("br"),a("span",{class:"line-number"},"101"),a("br"),a("span",{class:"line-number"},"102"),a("br"),a("span",{class:"line-number"},"103"),a("br"),a("span",{class:"line-number"},"104"),a("br"),a("span",{class:"line-number"},"105"),a("br"),a("span",{class:"line-number"},"106"),a("br"),a("span",{class:"line-number"},"107"),a("br"),a("span",{class:"line-number"},"108"),a("br"),a("span",{class:"line-number"},"109"),a("br")])],-1),Q=a("h4",{id:"基本性质"},[a("a",{class:"header-anchor",href:"#基本性质","aria-hidden":"true"},"#"),t(" 基本性质")],-1),W=a("ol",null,[a("li",null,"非红即黑"),a("li",null,"根黑"),a("li",null,"叶黑 e.g T.null 黑哨兵"),a("li",null,"红父孩子黑"),a("li",null,"简单路径同黑"),a("li",null,"右孩子不红 e.g 父黑两孩红 -> 父红两孩黑(flip); 父黑右红 -> 父左旋变红, 右孩子变黑(left-rotate)")],-1),J=a("h4",{id:"基本操作"},[a("a",{class:"header-anchor",href:"#基本操作","aria-hidden":"true"},"#"),t(" 基本操作")],-1),U=a("ol",null,[a("li",null,"插入(插入红点, 旋转+重新着色(反色)保持红黑性质)"),a("li",null,"删除(删除红点, 旋转+重新着色(反色)保持红黑性质)")],-1),X=a("h3",{id:"b-tree"},[a("a",{class:"header-anchor",href:"#b-tree","aria-hidden":"true"},"#"),t(" B Tree")],-1),Y=a("p",null,"t: 每个内部结点至少 t 个孩子(t-1 个 key), 至多 2t 个孩子(2t-1 个 key)",-1),Z=a("h4",{id:"插入-删除"},[a("a",{class:"header-anchor",href:"#插入-删除","aria-hidden":"true"},"#"),t(" 插入/删除")],-1),$=a("p",null,"下溯的同时,分裂满结点",-1),nn=a("h3",{id:"fibonacci-heap"},[a("a",{class:"header-anchor",href:"#fibonacci-heap","aria-hidden":"true"},"#"),t(" Fibonacci Heap")],-1),sn=a("p",null,"BST + 循环双向链表:",-1),an=a("ul",null,[a("li",null,"一个根树(根结点)循环双向链表"),a("li",null,"n 个孩子循环双向链表: 每个根树的每层结点形成一个循环双向链表")],-1),tn=a("h3",{id:"k-dimensional-tree"},[a("a",{class:"header-anchor",href:"#k-dimensional-tree","aria-hidden":"true"},"#"),t(" K-Dimensional Tree")],-1),en=a("ul",null,[a("li",null,"分隔空间数据")],-1),on=a("p",null,"e.g 左子树:左下方 右子树:右上方",-1),ln=a("h2",{id:"search-algorithm"},[a("a",{class:"header-anchor",href:"#search-algorithm","aria-hidden":"true"},"#"),t(" Search Algorithm")],-1),cn=a("h3",{id:"first-search"},[a("a",{class:"header-anchor",href:"#first-search","aria-hidden":"true"},"#"),t(" First Search")],-1),pn=a("ul",null,[a("li",null,"DFS(深度优先):栈实现"),a("li",null,"BFS(广度优先):队列实现")],-1),un=a("p",null,[a("img",{src:"/awesome-notes/assets/SearchPerformance.866c6c75.jpg",alt:"Search Algorithm Performance"})],-1),rn=a("h3",{id:"cycle-detection"},[a("a",{class:"header-anchor",href:"#cycle-detection","aria-hidden":"true"},"#"),t(" cycle detection")],-1),kn=a("ul",null,[a("li",null,"许多图论算法不适用于存在环路的复杂图,故使用循环检测剔除意外情况")],-1),bn=a("p",null,"处理方法:可将环路元素(如强联通分支)视作单一元素,忽视其内部结构",-1),mn=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[t("a "),a("span",{class:"token operator"},"="),t(" b"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("b "),a("span",{class:"token operator"},"="),t(" c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("c "),a("span",{class:"token operator"},"="),t(" a"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"//a extends b;b extends c;c extends a;"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),dn=a("h2",{id:"dynamic-programming"},[a("a",{class:"header-anchor",href:"#dynamic-programming","aria-hidden":"true"},"#"),t(" Dynamic Programming")],-1),hn=a("ul",null,[a("li",null,[t("最优解结构特征: 一个选择 + 子问题的最优解 - 所有(可"),a("strong",null,"重复求解"),t(")子问题的最优解可"),a("strong",null,"独立求解"),t("(不互相影响)")]),a("li",null,"递归定义最优解: 列出递归表达式"),a("li",null,"自底向上求解最优解"),a("li",null,"构造最优解(额外信息数组)")],-1),fn=a("h3",{id:"子问题"},[a("a",{class:"header-anchor",href:"#子问题","aria-hidden":"true"},"#"),t(" 子问题")],-1),yn=a("ul",null,[a("li",null,"子问题可映射为有向图, 并对其进行拓扑排序: 共有 O(n) 个子问题, 每个子问题最多 O(n) 种选择, 则算法时间复杂度为 O(n^2).其对应子问题图有 n 个顶点, 每个顶点最多有 n-1 条边."),a("li",null,"递归生成可以重复求解的子问题,而不是不断生成新的子问题")],-1),gn=a("h3",{id:"范例"},[a("a",{class:"header-anchor",href:"#范例","aria-hidden":"true"},"#"),t(" 范例")],-1),wn=a("ul",null,[a("li",null,"切割钢条问题: max{p[i], r[n-i]}"),a("li",null,"矩阵相乘链问题"),a("li",null,"最大公共子序列问题: r[i, j] = max{r[i, j-1], r[i-1, j]}"),a("li",null,"无权最短路径: path[i, j] = min{path[i, r], [r, j]}")],-1),vn=a("h2",{id:"greedy-algorithm"},[a("a",{class:"header-anchor",href:"#greedy-algorithm","aria-hidden":"true"},"#"),t(" Greedy Algorithm")],-1),xn=a("ul",null,[a("li",null,[t("最优解结构特征: 一个选择 + 子问题的最优解 - 所有(可"),a("strong",null,"重复求解"),t(")子问题的最优解可"),a("strong",null,"独立求解"),t("(不互相影响)")]),a("li",null,"递归定义最优解: 列出递归表达式"),a("li",null,[t("自底向上求解最优解: 每次不进行多次选择, 只进行一次 "),a("strong",null,"贪心选择")]),a("li",null,"构造最优解(额外信息数组)")],-1),Rn=a("h2",{id:"map-algorithm"},[a("a",{class:"header-anchor",href:"#map-algorithm","aria-hidden":"true"},"#"),t(" Map Algorithm")],-1),Sn=a("h3",{id:"图的表示"},[a("a",{class:"header-anchor",href:"#图的表示","aria-hidden":"true"},"#"),t(" 图的表示")],-1),Nn=a("ul",null,[a("li",null,"邻接链表法"),a("li",null,"邻接矩阵法")],-1),jn=a("h4",{id:"稀疏矩阵"},[a("a",{class:"header-anchor",href:"#稀疏矩阵","aria-hidden":"true"},"#"),t(" 稀疏矩阵")],-1),An=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[t("unordered_map"),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},","),t(" unordered_map"),a("span",{class:"token operator"},"<"),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),a("span",{class:"token operator"},">"),t(),a("span",{class:"token operator"},">"),t(),a("span",{class:"token comment"},"// => (row, (col, val))"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Tn=a("h3",{id:"广度优先遍历"},[a("a",{class:"header-anchor",href:"#广度优先遍历","aria-hidden":"true"},"#"),t(" 广度优先遍历")],-1),Dn=a("h4",{id:"bfs-node-color"},[a("a",{class:"header-anchor",href:"#bfs-node-color","aria-hidden":"true"},"#"),t(" BFS Node Color")],-1),En=a("ul",null,[a("li",null,"white: 未被发现/访问"),a("li",null,"gray: 已被发现(进入队列), 邻接结点未全部发现"),a("li",null,"black: 已被发现, 邻接结点全部发现")],-1),Bn=a("h4",{id:"bfs-node-parent"},[a("a",{class:"header-anchor",href:"#bfs-node-parent","aria-hidden":"true"},"#"),t(" BFS Node Parent")],-1),Mn=a("p",null,"广度优先树父结点",-1),Fn=a("h4",{id:"bfs-node-distance"},[a("a",{class:"header-anchor",href:"#bfs-node-distance","aria-hidden":"true"},"#"),t(" BFS Node Distance")],-1),zn=a("p",null,"距离 = v.pi.d + 1",-1),Cn=a("h4",{id:"利用队列实现广度优先遍历"},[a("a",{class:"header-anchor",href:"#利用队列实现广度优先遍历","aria-hidden":"true"},"#"),t(" 利用队列实现广度优先遍历")],-1),Pn=a("h3",{id:"深度优先遍历"},[a("a",{class:"header-anchor",href:"#深度优先遍历","aria-hidden":"true"},"#"),t(" 深度优先遍历")],-1),Ln=a("p",null,"利用 递归/栈 实现深度优先遍历",-1),Kn=a("h4",{id:"dfs-node-color"},[a("a",{class:"header-anchor",href:"#dfs-node-color","aria-hidden":"true"},"#"),t(" DFS Node Color")],-1),qn=a("ul",null,[a("li",null,"white: 未被发现/访问"),a("li",null,"gray: 已被发现, 未二次访问"),a("li",null,"black: 已被发现, 二次访问(比其深的所有结点皆被发现)")],-1),In=a("p",null,"当第一个访问 edge(u,v) 时:",-1),On=a("ul",null,[a("li",null,"v.color == white: 树边"),a("li",null,[t("v.color == gray : 后向边(v 为 深度优先*"),a("em",null,"森林"),t("- 的祖父结点)")]),a("li",null,"v.color == black: 前向边/横向边(v 为较深的结点/子结点)"),a("li",null,"无向图深度优先遍历不会出现 前向边/横向边")],-1),Hn=a("h4",{id:"dfs-node-parent"},[a("a",{class:"header-anchor",href:"#dfs-node-parent","aria-hidden":"true"},"#"),t(" DFS Node Parent")],-1),Gn=a("p",null,"比 v 浅的结点(比 v 更早被发现的结点)",-1),_n=a("h4",{id:"dfs-node-distance"},[a("a",{class:"header-anchor",href:"#dfs-node-distance","aria-hidden":"true"},"#"),t(" DFS Node Distance")],-1),Vn=a("ul",null,[a("li",null,"v.d = ++time: 被发现的时间戳(入栈)"),a("li",null,"v.f = ++time: 被二次访问的时间戳(出栈)"),a("li",null,[t("time"),a("code",null,"<"),t("v.d, white; v.d"),a("code",null,"<"),t("time"),a("code",null,"<"),t("v.f, gray: time"),a("code",null,">"),t("v.f, black")])],-1),Qn=a("h3",{id:"拓扑排序"},[a("a",{class:"header-anchor",href:"#拓扑排序","aria-hidden":"true"},"#"),t(" 拓扑排序")],-1),Wn=a("p",null,"目标集合: 拓扑排序后集合, 先入顶点高序, 后入顶点低序",-1),Jn=a("h4",{id:"kahn-算法"},[a("a",{class:"header-anchor",href:"#kahn-算法","aria-hidden":"true"},"#"),t(" Kahn 算法")],-1),Un=a("p",null,"不断将图中入度为 0 的点移入目标集合",-1),Xn=a("h4",{id:"dfs-深度优先"},[a("a",{class:"header-anchor",href:"#dfs-深度优先","aria-hidden":"true"},"#"),t(" DFS(深度优先)")],-1),Yn=a("p",null,"当深度遍历至较深处, 并开始回溯时, 将此时访问的顶点加入目标集合(v.f 降序)",-1),Zn=a("h3",{id:"单源最短路径"},[a("a",{class:"header-anchor",href:"#单源最短路径","aria-hidden":"true"},"#"),t(" 单源最短路径")],-1),$n=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"Relax"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" u"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" v"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" w"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(" v"),a("span",{class:"token punctuation"},"."),t("d "),a("span",{class:"token operator"},">"),t(" u"),a("span",{class:"token punctuation"},"."),t("d "),a("span",{class:"token operator"},"+"),t(" w"),a("span",{class:"token punctuation"},"["),t("u"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("v"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token punctuation"},"{"),t("\n v"),a("span",{class:"token punctuation"},"."),t("pi "),a("span",{class:"token operator"},"="),t(" u"),a("span",{class:"token punctuation"},";"),t("\n v"),a("span",{class:"token punctuation"},"."),t("d "),a("span",{class:"token operator"},"="),t(" v"),a("span",{class:"token punctuation"},"."),t("pi"),a("span",{class:"token punctuation"},"."),t("d "),a("span",{class:"token operator"},"+"),t(" w"),a("span",{class:"token punctuation"},"["),t("v"),a("span",{class:"token punctuation"},"."),t("pi"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("v"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),ns=a("h4",{id:"dag-shortest-paths"},[a("a",{class:"header-anchor",href:"#dag-shortest-paths","aria-hidden":"true"},"#"),t(" DAG Shortest Paths")],-1),ss=a("p",null,"先将图进行拓扑排序(深度优先遍历), 再按照拓扑排序顺序, 依次对每个结点(拓扑排序)的邻接边进行 relax",-1),as=a("blockquote",null,[a("p",null,"a -> b -> c --\x3e d, 且 a--b, a--c, b--d, c--d: relax(a, b), relax(a, c), relax(b, d), relax(c, d)")],-1),ts=a("h4",{id:"bellman-ford-algorithm"},[a("a",{class:"header-anchor",href:"#bellman-ford-algorithm","aria-hidden":"true"},"#"),t(" Bellman-Ford Algorithm")],-1),es=a("p",null,"对每条边进行 n 次(结点总数) relax",-1),os=a("h4",{id:"dijkstra-algorithm"},[a("a",{class:"header-anchor",href:"#dijkstra-algorithm","aria-hidden":"true"},"#"),t(" Dijkstra Algorithm")],-1),ls=a("p",null,"贪心算法: 每次选取不属于 S 集合(white) 且 v.d 最小(gray)的结点, 对其所有邻接边进行 relax, 并将其加入 S 集合(black)",-1),cs=a("ul",null,[a("li",null,"white: 不属于 S 集合"),a("li",null,"gray: 不属于 S 集合 且 v.d 最小"),a("li",null,"black: 属于 S 集合")],-1),ps=a("h3",{id:"结点对最短路径"},[a("a",{class:"header-anchor",href:"#结点对最短路径","aria-hidden":"true"},"#"),t(" 结点对最短路径")],-1),us=a("p",null,"动态规划:l^m(i, j) = min(l^m-1(i, j), min(1<=k<=n){l^m-1(i, k)+w(k, j)})",-1),is=a("blockquote",null,[a("p",null,"m: 中间结点个数")],-1),rs=a("h4",{id:"floyd-warshall-algorithm"},[a("a",{class:"header-anchor",href:"#floyd-warshall-algorithm","aria-hidden":"true"},"#"),t(" Floyd-Warshall Algorithm")],-1),ks=a("p",null,"d^k(i, j) = w(i, j), k = 0 | min(d^k-1(i, j), d^k-1(i, k) + d^k-1(k, j)), k >= 1",-1),bs=a("p",null,"pi^(i, j) = pi^k-1(i, j) or pi^k-1(k, j)",-1),ms=a("blockquote",null,[a("p",null,"k: 中间结点个数")],-1),ds=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[t("Matrix "),a("span",{class:"token function"},"floyd_warshall"),a("span",{class:"token punctuation"},"("),t("Matrix W"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" n "),a("span",{class:"token operator"},"="),t(" W"),a("span",{class:"token punctuation"},"."),t("rows"),a("span",{class:"token punctuation"},";"),t("\n Matrix D"),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"0"),t(),a("span",{class:"token operator"},"="),t(" W"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" k "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("k "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" k"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n D"),a("span",{class:"token operator"},"^"),t("k "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token function"},"Matrix"),a("span",{class:"token punctuation"},"("),t("n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" j "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" j "),a("span",{class:"token operator"},"<"),t(" n"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" J"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n d"),a("span",{class:"token operator"},"^"),t("k"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"min"),a("span",{class:"token punctuation"},"("),t("d"),a("span",{class:"token operator"},"^"),t("k"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t(" d"),a("span",{class:"token operator"},"^"),t("k"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token operator"},"+"),t("d"),a("span",{class:"token operator"},"^"),t("k"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"["),t("k"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("j"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" D"),a("span",{class:"token operator"},"^"),t("n"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),hs=a("h3",{id:"最大流问题"},[a("a",{class:"header-anchor",href:"#最大流问题","aria-hidden":"true"},"#"),t(" 最大流问题")],-1),fs=a("p",null,"MaxFlow Problem:",-1),ys=a("p",null,[a("img",{src:"/awesome-notes/assets/FordFulkersonAlgorithm.94aecb4b.png",alt:"Ford Fulkerson Algorithm"})],-1),gs=a("h4",{id:"最大流模型"},[a("a",{class:"header-anchor",href:"#最大流模型","aria-hidden":"true"},"#"),t(" 最大流模型")],-1),ws=a("p",null,"最大流模型必须满足以下条件:",-1),vs=a("ul",null,[a("li",null,"无双向边"),a("li",null,"唯一的源点 s 和 唯一的汇点 t")],-1),xs=a("p",null,"对于不符合该模型的问题可进行简单转化:",-1),Rs=a("ul",null,[a("li",null,"双向边: 添加额外结点, 切割双向边的其中一条, 使得双向边变成 3 条单向边")],-1),Ss=a("blockquote",null,[a("p",null,"a --\x3e b, b --\x3e a: a --\x3e c, c --\x3e b, b --\x3e a")],-1),Ns=a("ul",null,[a("li",null,"多源点/汇点: 添加一个总源点/汇点")],-1),js=a("h4",{id:"残存网络"},[a("a",{class:"header-anchor",href:"#残存网络","aria-hidden":"true"},"#"),t(" 残存网络")],-1),As=a("ul",null,[a("li",null,"若原图 u --\x3e v 总容量 > 0, 则残存网络中 边 u --\x3e v:剩余容量, 边 v --\x3e u: 已用容量"),a("li",null,"增广路径: 残存网络中一条可行通路")],-1),Ts=a("h4",{id:"最大流最小割定理"},[a("a",{class:"header-anchor",href:"#最大流最小割定理","aria-hidden":"true"},"#"),t(" 最大流最小割定理")],-1),Ds=a("p",null,"MaxFlow-MinCut Theorem:",-1),Es=a("ul",null,[a("li",null,"切割的净流量: 流出-流入"),a("li",null,"切割的容量: 流出总容量(无需减流入总容量)"),a("li",null,"最小切割: 容量最小的切割")],-1),Bs=a("p",null,"最大流最小割定理: 以下三个命题等价",-1),Ms=a("ul",null,[a("li",null,"f 是 G 的一个最大流"),a("li",null,"残存网络 Gf 不含增广路径"),a("li",null,"|f| = c(S, T)(切割的容量): |f| <= c(S, T)(流网络中任意流 f <= 任意切割容量 c(S, T))")],-1),Fs=a("h4",{id:"ford-fulkerson-algorithm"},[a("a",{class:"header-anchor",href:"#ford-fulkerson-algorithm","aria-hidden":"true"},"#"),t(" Ford-Fulkerson Algorithm")],-1),zs=a("p",null,"不断寻找增广路径",-1),Cs=a("h2",{id:"tree-edit-distance"},[a("a",{class:"header-anchor",href:"#tree-edit-distance","aria-hidden":"true"},"#"),t(" Tree Edit Distance")],-1),Ps=a("h3",{id:"definition"},[a("a",{class:"header-anchor",href:"#definition","aria-hidden":"true"},"#"),t(" Definition")],-1),Ls=a("p",null,"Tree Edit Distance: 给定 Cost(edit operation) 时的最小编辑费用",-1);o.render=function(a,t,e,o,Ks,qs){return n(),s("div",null,[l,c,p,u,i,r,k,b,m,d,h,f,y,g,w,v,x,R,S,N,j,A,T,D,E,B,M,F,z,C,P,L,K,q,I,O,H,G,_,V,Q,W,J,U,X,Y,Z,$,nn,sn,an,tn,en,on,ln,cn,pn,un,rn,kn,bn,mn,dn,hn,fn,yn,gn,wn,vn,xn,Rn,Sn,Nn,jn,An,Tn,Dn,En,Bn,Mn,Fn,zn,Cn,Pn,Ln,Kn,qn,In,On,Hn,Gn,_n,Vn,Qn,Wn,Jn,Un,Xn,Yn,Zn,$n,ns,ss,as,ts,es,os,ls,cs,ps,us,is,rs,ks,bs,ms,ds,hs,fs,ys,gs,ws,vs,xs,Rs,Ss,Ns,js,As,Ts,Ds,Es,Bs,Ms,Fs,zs,Cs,Ps,Ls])};export default o;export{e as __pageData}; diff --git a/assets/computerScience_algorithms_ojBasicNotes.md.87451c39.js b/assets/computerScience_algorithms_ojBasicNotes.md.87451c39.js new file mode 100644 index 00000000000..325d16ce6ed --- /dev/null +++ b/assets/computerScience_algorithms_ojBasicNotes.md.87451c39.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"OJ Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"C++ Notes for OJ","slug":"c-notes-for-oj"},{"level":3,"title":"Format","slug":"format"},{"level":3,"title":"Iterator","slug":"iterator"},{"level":3,"title":"limits","slug":"limits"},{"level":3,"title":"Implementation Pattern","slug":"implementation-pattern"},{"level":3,"title":"algorithm","slug":"algorithm"},{"level":3,"title":"map","slug":"map"},{"level":3,"title":"Algorithm","slug":"algorithm-1"},{"level":2,"title":"Search Problem","slug":"search-problem"},{"level":3,"title":"Search in Sorted Array","slug":"search-in-sorted-array"},{"level":3,"title":"Max Min Problem","slug":"max-min-problem"},{"level":3,"title":"Range Max Min Query","slug":"range-max-min-query"},{"level":2,"title":"Greedy Algorithm","slug":"greedy-algorithm"},{"level":2,"title":"Simulation","slug":"simulation"},{"level":3,"title":"Painting","slug":"painting"},{"level":3,"title":"Reverting","slug":"reverting"},{"level":3,"title":"Meet Problem and Collision Problem","slug":"meet-problem-and-collision-problem"},{"level":2,"title":"String","slug":"string"},{"level":3,"title":"Rotate String","slug":"rotate-string"},{"level":2,"title":"Map Theory","slug":"map-theory"},{"level":3,"title":"Shortest Paths","slug":"shortest-paths"},{"level":3,"title":"Minimal Spanning Tree","slug":"minimal-spanning-tree"},{"level":3,"title":"BFS","slug":"bfs"},{"level":3,"title":"DFS","slug":"dfs"},{"level":3,"title":"Connected Component","slug":"connected-component"},{"level":2,"title":"Dynamic Programming","slug":"dynamic-programming"},{"level":3,"title":"典型题目","slug":"典型题目"},{"level":3,"title":"Digital Bits Dynamic Programming(数位 DP)","slug":"digital-bits-dynamic-programming-数位-dp"},{"level":2,"title":"Math","slug":"math"},{"level":3,"title":"Matrix Fast Power","slug":"matrix-fast-power"},{"level":3,"title":"Mod Power","slug":"mod-power"},{"level":2,"title":"Tips","slug":"tips"},{"level":3,"title":"Array","slug":"array"},{"level":3,"title":"Map","slug":"map-1"},{"level":3,"title":"Set","slug":"set"},{"level":3,"title":"Two Pointer","slug":"two-pointer"},{"level":3,"title":"Float Pointer","slug":"float-pointer"},{"level":3,"title":"bit 表示法","slug":"bit-表示法"}],"relativePath":"computerScience/algorithms/ojBasicNotes.md","lastUpdated":1627194884000}',t={},o=a('

OJ Basic Notes

C++ Notes for OJ

Format

string and integer

  • String Stream
  • to_string
  • stoll
  • atoi/atol/atof

Iterator

slice

vector<int> b(a.begin() + 1, a.end());\nvector<int> c(a.rbegin(), a.rend());\n
1
2

limits

 #include<iostream>\n #include<string>\n #include <limits>\n\nusing namespace std;\n\nint main(void) {\n    cout << "type: \\t\\t" << "************size**************"<< endl;\n    cout << "bool: \\t\\t" << "所占字节数:" << sizeof(bool);\n    cout << "\\t最大值:" << (numeric_limits<bool>::max)();\n    cout << "\\t\\t最小值:" << (numeric_limits<bool>::min)() << endl;\n    cout << "char: \\t\\t" << "所占字节数:" << sizeof(char);\n    cout << "\\t最大值:" << (numeric_limits<char>::max)();\n    cout << "\\t\\t最小值:" << (numeric_limits<char>::min)() << endl;\n    cout << "signed char: \\t" << "所占字节数:" << sizeof(signed char);\n    cout << "\\t最大值:" << (numeric_limits<signed char>::max)();\n    cout << "\\t\\t最小值:" << (numeric_limits<signed char>::min)() << endl;\n    cout << "unsigned char: \\t" << "所占字节数:" << sizeof(unsigned char);\n    cout << "\\t最大值:" << (numeric_limits<unsigned char>::max)();\n    cout << "\\t\\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;\n    cout << "wchar_t: \\t" << "所占字节数:" << sizeof(wchar_t);\n    cout << "\\t最大值:" << (numeric_limits<wchar_t>::max)();\n    cout << "\\t\\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;\n    cout << "short: \\t\\t" << "所占字节数:" << sizeof(short);\n    cout << "\\t最大值:" << (numeric_limits<short>::max)();\n    cout << "\\t\\t最小值:" << (numeric_limits<short>::min)() << endl;\n    cout << "int: \\t\\t" << "所占字节数:" << sizeof(int);\n    cout << "\\t最大值:" << (numeric_limits<int>::max)();\n    cout << "\\t最小值:" << (numeric_limits<int>::min)() << endl;\n    cout << "unsigned: \\t" << "所占字节数:" << sizeof(unsigned);\n    cout << "\\t最大值:" << (numeric_limits<unsigned>::max)();\n    cout << "\\t最小值:" << (numeric_limits<unsigned>::min)() << endl;\n    cout << "long: \\t\\t" << "所占字节数:" << sizeof(long);\n    cout << "\\t最大值:" << (numeric_limits<long>::max)();\n    cout << "\\t最小值:" << (numeric_limits<long>::min)() << endl;\n    cout << "unsigned long: \\t" << "所占字节数:" << sizeof(unsigned long);\n    cout << "\\t最大值:" << (numeric_limits<unsigned long>::max)();\n    cout << "\\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;\n    cout << "double: \\t" << "所占字节数:" << sizeof(double);\n    cout << "\\t最大值:" << (numeric_limits<double>::max)();\n    cout << "\\t最小值:" << (numeric_limits<double>::min)() << endl;\n    cout << "long double: \\t" << "所占字节数:" << sizeof(long double);\n    cout << "\\t最大值:" << (numeric_limits<long double>::max)();\n    cout << "\\t最小值:" << (numeric_limits<long double>::min)() << endl;\n    cout << "float: \\t\\t" << "所占字节数:" << sizeof(float);\n    cout << "\\t最大值:" << (numeric_limits<float>::max)();\n    cout << "\\t最小值:" << (numeric_limits<float>::min)() << endl;\n    cout << "size_t: \\t" << "所占字节数:" << sizeof(size_t);\n    cout << "\\t最大值:" << (numeric_limits<size_t>::max)();\n    cout << "\\t最小值:" << (numeric_limits<size_t>::min)() << endl;\n    cout << "string: \\t" << "所占字节数:" << sizeof(string) << endl;\n    // << "\\t最大值:" << (numeric_limits<string>::max)() << "\\t最小值:"\n    // << (numeric_limits<string>::min)() << endl;\n    cout << "type: \\t\\t" << "************size**************"<< endl;\n    return 0;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

Implementation Pattern

#define FIN             freopen("input.txt","r",stdin)\n#define FOUT            freopen("output.txt","w",stdout)\n#define fst             first\n#define snd             second\n\ntypedef long long LL;\ntypedef pair < int, int >PII;\n\nconst int INF = 0x3f3f3f3f;\nconst int MAX_N = 500 + 5;\nconst int MAX_P = 6 + 5;\n\nint T, N, M, P, res;\nchar buf[MAX_N];\nint usr[MAX_N], usr_cnt;\n\nstruct MST {\n    struct Edge {\n        int u, v, w;\n        Edge() {}\n        Edge(int u, int v, int w):u(u), v(v), w(w) {}\n        bool operator <(const Edge & e) const {\n            return w < e.w;\n        }\n    } edges[MAX_N * MAX_P];\n\n    int par[MAX_N], tot;\n\n    void init() {\n        memset(par, -1, sizeof(par));\n        tot = 0;\n    }\n\n    int Find(int x) {\n        return -1 == par[x] ? x : (par[x] = Find(par[x]));\n    }\n\n    void add_edge(int u, int v, int w) {\n        edges[tot++] = Edge(u, v, w);\n    }\n\n    int Kruskal() {\n        memset(par, -1, sizeof(par));\n        int u, v, w, pu, pv, cnt = 0, ret = 0;\n        sort(edges, edges + tot);\n        for (int i = 0; i < tot; i++) {\n            if (cnt == usr_cnt - 1)\n                break;\n            u = edges[i].u, v = edges[i].v, w = edges[i].w;\n            pu = Find(u), pv = Find(v);\n            if (pu == pv)\n                continue;\n            par[pv] = pu;\n            ret += w;\n            cnt++;\n        }\n        return ret;\n    }\n} mst;\n\nstruct Dijkstra {\n    struct Edge {\n        int v, w, next;\n        Edge() {}\n        Edge(int v, int w, int next):v(v), w(w), next(next) {}\n    } edges[MAX_N * MAX_P * 2];\n\n    struct QNode {\n        int u, w;\n        QNode() {}\n        QNode(int u, int w):u(u), w(w) {}\n        bool operator >(const QNode & e) const {\n            return w > e.w;\n        }\n    } cur;\n\n    int head[MAX_N], tot;\n    int dist[MAX_N];\n    bool vis[MAX_N];\n    priority_queue < QNode, vector < QNode >, greater < QNode > >Q;\n\n    void init() {\n        tot = 0;\n        memset(head, -1, sizeof(head));\n        memset(dist, 0x3f, sizeof(dist));\n        memset(vis, false, sizeof(vis));\n    }\n\n    void add_edge(int u, int v, int w) {\n        edges[tot] = Edge(v, w, head[u]);\n        head[u] = tot++;\n    }\n\n    int dijkstra(int src, int dst) {\n        int u, v, w;\n        Q.push(QNode(src, dist[src] = 0));\n        while (!Q.empty()) {\n            cur = Q.top();\n            Q.pop();\n            u = cur.u;\n            if (vis[u])\n                continue;\n            vis[u] = true;\n            for (int i = head[u]; ~i; i = edges[i].next) {\n                v = edges[i].v, w = edges[i].w;\n                if (!vis[v] && dist[v] > dist[u] + w) {\n                    dist[v] = dist[u] + w;\n                    Q.push(QNode(v, dist[v]));\n                }\n            }\n        }\n        return dist[dst];\n    }\n} dij;\n\nint solve() {\n    int u, v, w;\n\n    if (usr_cnt == N) {\n        mst.init();\n\n        for (int i = 1; i <= M; i++) {\n            scanf("%d %d %d", &u, &v, &w);\n            mst.add_edge(u, v, w);\n        }\n\n        return mst.Kruskal();\n    } else if (usr_cnt == 2) {\n        dij.init();\n\n        for (int i = 1; i <= M; i++) {\n            scanf("%d %d %d", &u, &v, &w);\n            dij.add_edge(u, v, w);\n            dij.add_edge(v, u, w);\n        }\n\n        return dij.dijkstra(usr[0], usr[1]);\n    }\n}\n\nint main()\n{\n\n #ifndef ONLINE_JUDGE\n    FIN;\n #endif // ONLINE_JUDGE\n\n    scanf("%d", &T);\n\n    while (T--) {\n        usr_cnt = 0;\n        scanf("%d %d %d", &N, &M, &P);\n        scanf("%s", buf + 1);\n\n        for (int i = 1; i <= N; i++) {\n            if (buf[i] == '1')\n                usr[usr_cnt++] = i;\n        }\n\n        res = solve();\n        printf("%d\\n", res);\n    }\n\n    return 0;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

algorithm

sort

  • in map: auto sort
struct Node {\n    bool operator<(const Node &o) {\n        return *this < o;\n    }\n};\n\nmap<Node> mps;\n
1
2
3
4
5
6
7
  • sort vector
bool cmp(const elem &i, const elem &j) {\n    return i < j;\n}\n\nsort(vec.start(), vec.end(), cmp);\n
1
2
3
4
5

map

  • insert/update: mp[key] = value;
  • search: mp.count(key)/mp.find(key), 不会插入空元素
  • unordered_map: hash map

Algorithm

  • sort
  • reverse
  • accumulate
  • prev_permutation/next_permutation

Search Problem

Search in Sorted Array

  • Binary Search
  • Divide and Conquer

LeetCode 74/240

Max Min Problem

在某些问题中, 要求满足条件的 max/min, 且可以轻易地判定某个值是否满足该条件, 则可利用二分法进行值的枚举

// poj 1064\nint N, K;\ndouble L[max_n];\n\n// judgement\nbool C(double x) {\n  int num = 0;\n\n  for (int i = 0; i < N; i++) {\n    num += (int)(L[i] / x);\n  }\n\n  return num >= K;\n}\n\nvoid solve(void) {\n  double lb = 0, ub = numeric_limits<double>::max();\n\n  for (int i = 0; i < 100; i++) {\n    double mid = (lb + ub) / 2;\n    if (C(mid)) lb = mid;\n    else ub = mid;\n  }\n\n  printf("%.2f\\n", floor(ub * 100) / 100);\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

Range Max Min Query

  • Segment Tree (线段树)
  • Binary Indexed Tree (树状数组)
  • Bucket Method (Divide and Conquer)
const int maxN = 1 << 17;\n\nint n;\nint dat[2 * maxN - 1];\n\nvoid init(int n_) {\n  n = 1;\n\n  // padding to 2^n\n  while (n < n_) n *= 2;\n\n  for (int i = 0; i < 2 * n - 1; i++) {\n    dat[i] = (numeric_limits<int>::max)();\n  }\n}\n\nvoid update(int k, int a) {\n  k += n - 1;\n  dat[k] = a;\n\n  while (k > 0) {\n    k = (k - 1) / 2;\n    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);\n  }\n}\n\nint query(int a, int b, int k, int l, int r) {\n  // failed\n  if (r <= a || b <= 1) {\n    return (numeric_limits<int>::max)();\n  }\n\n  // [l, r) <= [a, b)\n  if (a <= 1 && r <= b) {\n    return dat[k];\n  } else {\n    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);\n    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);\n    return min(vl, vr);\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Greedy Algorithm

  • 字典排序比较问题
  • Huffman Tree

Simulation

Painting

  • 对于实际操作, 直接覆写至状态数组即可, 无需关心边界条件(效果会立即生效)

e.g 交接处方块 , 2 次写 1, maps[i][j] = 1, 不用担心重复计数

Reverting

  • using 1 bit to simulate operation
  • when need to output, calculate bits up
  • combined with dp problem

Meet Problem and Collision Problem

将相遇/碰撞的两物体视作插肩而过即可

String

sstream::stringstream\n\nsort()\ntransform(toUpper/toLower)\n\nstring::size_type\nstring::npos(vector.end())\n\nstr.find() == string::npos/string::size_type\nstr.substr(int pos, int len)\n\ngetline(cin/sin, strbuf)\n
1
2
3
4
5
6
7
8
9
10
11
12
  • string::size_type pre, post 指针: 进行逐行匹配

Rotate String

#include <string>\n#include <algorithm>\n\nstring left_rotate(string str, int offset) {\n  int size = str.length();\n  int n = offset % size;\n  reverse(str.begin(), str.begin() + n);\n  reverse(str.begin() + n, str.end());\n  reverse(str.begin(), str.end());\n  return str;\n}\n
1
2
3
4
5
6
7
8
9
10
11

Map Theory

Shortest Paths

  • Dijkstra
  • Floyd

Minimal Spanning Tree

  • Kruskal (tFind/tUnion)

BFS

Mark array/queue:

  • Shortest Paths
  • Diameter(直径) of Tree(Two pass for BFS)

DFS

Mark array/ Mark stack/Recursion:

  • Longest Paths

Connected Component

Strongly Connected Component

  • Tarjan Algorithm(v.index(DFS 时此点被访问的顺序) == v.lowLink(从 v 出发经有向边可达到的所有结点中最小的 index))

Union Find

quickly figure out connection of map

Dynamic Programming

  • dp 数组可以滚动使用, 从而节省空间

dp[m][n] => dp[2][n] (dp[i & 1][j])

典型题目

关键: 最优子结构 + 状态无后效性

  • 所有背包问题
  • 二分问题:最优二分搜索树/文件合并
  • 非连续特征序列: 最长子序列/最长上升序列
  • 多重部分和问题 e.g 数组中是否存在一对数的和为 xx
  • 计数问题/分组问题/分划问题

Digital Bits Dynamic Programming(数位 DP)

题目模式

  • 给定区间 [a, b], 求满足特定要求的数, 要求一般与大小无关, 与数位的组成相关
  • 规模巨大, 无法枚举

递增数: 1234, 2579; 双峰数: 19280, 26193; 含 49: 49, 149, 1492; 整除 13: 26, 39 ...

解题模式

f(a, b) = f(b, 0) - f(a - 1, 0)

暴力 + 存储 = 记忆化搜索:

  • 暴力枚举每一位的 (0, ...,9)
  • 利用 dp[pos][state] 与 dfs(pos, state, k, flag) 进行存储
/// \\brief 数字处理函数\n/// \\param num 原始数据\n/// \\param digit 保存每个数位的值\n/// \\param state 初始状态\nint f(int num){\n    int ans;\n    int pos = 0;\n\n    while(num){\n        digit[++pos]=num%10;\n        num=num/10;\n    }\n\n    return dfs( pos, state , true);\n}\n\n/// \\brief dfs 函数, 从高位开始遍历\n/// \\param f     记忆化数组\n/// \\param pos   当前数位下标\n/// \\param state 之前数字的状态\n/// \\param flag  上限标志\nint dfs(int pos, int state, bool flag) {\n    if (pos == -1) {\n        return state == target_state;\n    }\n    if (!exception && ~f[pos][state]) {\n        return f[pos][state]\n    }\n\n    int ans = 0;\n\n    // 对于每一个数位, 求出枚举界限\n    // 一般从 0 枚举至 9, 当到达特殊位置时, 枚举上限可能改变\n    int next = exception ? digit[i] : 9;\n\n    for (int digit = 0; digit <= next; digit++) {\n        ans += dfs(pos - 1, new_state(state, digit), exception && digit == next);\n    }\n\n    return flag ? ans : f[pos][state] = ans;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
typedef long long ll;\n\nint a[20];\n\nll dp[20][state];   //不同题目状态不同\n\nll dfs(int pos, int state, bool lead, bool limit) {\n    // 递归边界, 按位枚举最低位是0, pos == -1 表示枚举结束\n    if (pos == -1) {\n        // 若可以保证每一位都合法, 则 return 1; 否则 return 0\n        return 1;\n    }\n\n    // 记忆化\n    if (!limit && !lead && dp[pos][state] != -1) {\n        return dp[pos][state];\n    }\n\n    // 根据 limit 判断枚举的上界 up\n    int up = limit ? a[pos] : 9;\n\n    // 开始计数\n    ll ans=0;\n\n    // 枚举: 把不同情况的个数加到ans\n    for(int i = 0;i <= up; i++)\n    {\n        // 当前数位枚举的数是i,根据题目的约束条件分类讨论\n        // 计算不同情况下的个数, 根据 state 变量来保证 i 的合法性\n        // 一定要保证枚举数的合法性\n\n        // 比如要求数位上不能有 62 连续出现, 那么state 就要保存前一位 pre\n        // 前一位如果是 6 那么位不能是 2\n\n        // 当不合法时, 直接 continue\n        if() ...\n        else if()...\n\n        ans += dfs(pos-1, new_state(pos, state) /*状态转移*/,\n          lead && i==0, limit && i == a[pos])\n    }\n\n    // 计算结束, 记录状态\n    if (!limit && !lead) {\n        dp[pos][state] = ans;\n    }\n\n    return ans;\n}\n\nll solve(ll x) {\n    int pos=0;\n\n    // 数位分解\n    while(x) {\n        a[pos++] = x % 10;\n        x /= 10;\n    }\n\n    // 从最高位开始枚举\n    // 最高位前导为 0, 且受上限限制(无法枚举至 9)\n    return dfs(pos-1, state /*一系列状态 */, true, true);\n}\n\nint main(void) {\n    ll le,ri;\n\n    while (~scanf("%lld %lld", &le, &ri)) {\n        // 初始化dp数组为-1\n        printf("%lld\\n", solve(ri)-solve(le-1));\n    }\n\n    return 0;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

Math

Matrix Fast Power

typedef vector<vector> mat;\n\nmat mul(mat& A, mat& B) {\n    mat C(A.size(), vec(B[0].size()));\n    for(int i = 0; i < (int)A.size(); ++i)\n        for(int j = 0; j < (int)B[0].size(); ++j)\n                for(int k = 0; k < (int)B.size(); ++k)\n                        C[i][j] ^= A[i][k] & B[k][j];\n    return C;\n}\n\nmat pow(mat A, int p) {\n    mat E(A.size(), vec(A.size()));\n    for(int i = 0; i < (int)A.size(); ++i) E[i][i] = 1;\n    while(p){\n        if(p & 1) E = mul(E, A);\n        A = mul(A, A);\n        p >>= 1;\n    }\n    return E;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Mod Power

typedef long long ll;\n\nll mod_pow(ll x, ll n, ll mod) {\n    ll res = 1;\n\n    while (n > 0) {\n        if (n & 1) res = res * x % mod;\n\n        x = x * x % mod;\n        n >>= 1;\n    }\n\n    return res;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Tips

Array

  • 可以利用数组元素的正负性表示存在性(或其他特殊意义)

Map

  • 用于 Hash 化
  • 用于将字符串转为数字
  • 用于计数

Set

  • 用于去重与查重(duplicate)
  • 用于集合运算题(交、并、差等)

Two Pointer

可以在有穷时间内判断是否存在循环:一个快指针,一个慢指针,当两者相遇时,表示存在循环。

Float Pointer

利用浮动指针解决相关问题:

  • 字符串比较
  • 连续区间问题(尺取法)

bit 表示法

多用于状态枚举(1 bit 表示 1 个状态/开关), 表示状态集合

可用于动态规划中压缩状态

0 // empty set\n1 << i // just 1 bit on\n(1 << n) - 1 // n bit on\nif (S >> i & 1) // include nth(i) bit\nS | 1 << i // insert nth(i) bit\nS & ~(1 << i) // remove nth(1) bit\nS | T // union\nS & T // intersection\n\ni & -i // last 1 bit\n
1
2
3
4
5
6
7
8
9
10
',101);t.render=function(a,p,t,e,c,l){return n(),s("div",null,[o])};export default t;export{p as __pageData}; diff --git a/assets/computerScience_algorithms_ojBasicNotes.md.87451c39.lean.js b/assets/computerScience_algorithms_ojBasicNotes.md.87451c39.lean.js new file mode 100644 index 00000000000..623a127f64c --- /dev/null +++ b/assets/computerScience_algorithms_ojBasicNotes.md.87451c39.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"OJ Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"C++ Notes for OJ","slug":"c-notes-for-oj"},{"level":3,"title":"Format","slug":"format"},{"level":3,"title":"Iterator","slug":"iterator"},{"level":3,"title":"limits","slug":"limits"},{"level":3,"title":"Implementation Pattern","slug":"implementation-pattern"},{"level":3,"title":"algorithm","slug":"algorithm"},{"level":3,"title":"map","slug":"map"},{"level":3,"title":"Algorithm","slug":"algorithm-1"},{"level":2,"title":"Search Problem","slug":"search-problem"},{"level":3,"title":"Search in Sorted Array","slug":"search-in-sorted-array"},{"level":3,"title":"Max Min Problem","slug":"max-min-problem"},{"level":3,"title":"Range Max Min Query","slug":"range-max-min-query"},{"level":2,"title":"Greedy Algorithm","slug":"greedy-algorithm"},{"level":2,"title":"Simulation","slug":"simulation"},{"level":3,"title":"Painting","slug":"painting"},{"level":3,"title":"Reverting","slug":"reverting"},{"level":3,"title":"Meet Problem and Collision Problem","slug":"meet-problem-and-collision-problem"},{"level":2,"title":"String","slug":"string"},{"level":3,"title":"Rotate String","slug":"rotate-string"},{"level":2,"title":"Map Theory","slug":"map-theory"},{"level":3,"title":"Shortest Paths","slug":"shortest-paths"},{"level":3,"title":"Minimal Spanning Tree","slug":"minimal-spanning-tree"},{"level":3,"title":"BFS","slug":"bfs"},{"level":3,"title":"DFS","slug":"dfs"},{"level":3,"title":"Connected Component","slug":"connected-component"},{"level":2,"title":"Dynamic Programming","slug":"dynamic-programming"},{"level":3,"title":"典型题目","slug":"典型题目"},{"level":3,"title":"Digital Bits Dynamic Programming(数位 DP)","slug":"digital-bits-dynamic-programming-数位-dp"},{"level":2,"title":"Math","slug":"math"},{"level":3,"title":"Matrix Fast Power","slug":"matrix-fast-power"},{"level":3,"title":"Mod Power","slug":"mod-power"},{"level":2,"title":"Tips","slug":"tips"},{"level":3,"title":"Array","slug":"array"},{"level":3,"title":"Map","slug":"map-1"},{"level":3,"title":"Set","slug":"set"},{"level":3,"title":"Two Pointer","slug":"two-pointer"},{"level":3,"title":"Float Pointer","slug":"float-pointer"},{"level":3,"title":"bit 表示法","slug":"bit-表示法"}],"relativePath":"computerScience/algorithms/ojBasicNotes.md","lastUpdated":1627194884000}',t={},o=a('',101);t.render=function(a,p,t,e,c,l){return n(),s("div",null,[o])};export default t;export{p as __pageData}; diff --git a/assets/computerScience_architecture_archBasicNotes.md.1ed20244.js b/assets/computerScience_architecture_archBasicNotes.md.1ed20244.js new file mode 100644 index 00000000000..7e0a7bc83c3 --- /dev/null +++ b/assets/computerScience_architecture_archBasicNotes.md.1ed20244.js @@ -0,0 +1 @@ +import{o as e,c as t,a}from"./app.6dd2a1b7.js";const r='{"title":"Architecture Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"SuperPipeline and SuperScalar","slug":"superpipeline-and-superscalar"},{"level":3,"title":"Instructions Dependencies and Latencies","slug":"instructions-dependencies-and-latencies"},{"level":3,"title":"Branch Prediction","slug":"branch-prediction"},{"level":2,"title":"VLIW (Very Long Instruction Word)","slug":"vliw-very-long-instruction-word"},{"level":2,"title":"Out of Order Execution (OoO or OoE)","slug":"out-of-order-execution-ooo-or-ooe"},{"level":3,"title":"The Brainiac vs Speed-demon Debate","slug":"the-brainiac-vs-speed-demon-debate"},{"level":3,"title":"Power Wall and ILP Wall","slug":"power-wall-and-ilp-wall"},{"level":3,"title":"Decoupled x86 microarchitecture","slug":"decoupled-x86-microarchitecture"},{"level":2,"title":"SMT (Hardware Threads)","slug":"smt-hardware-threads"},{"level":3,"title":"More cores or Wider cores","slug":"more-cores-or-wider-cores"},{"level":2,"title":"DLP (data-level parallelism)","slug":"dlp-data-level-parallelism"},{"level":3,"title":"SIMD Vector Instructions","slug":"simd-vector-instructions"},{"level":2,"title":"Memory","slug":"memory"},{"level":3,"title":"Memory Wall","slug":"memory-wall"},{"level":3,"title":"Caches","slug":"caches"},{"level":3,"title":"Memory Latency and Bandwidth","slug":"memory-latency-and-bandwidth"},{"level":2,"title":"Distributed System","slug":"distributed-system"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"computerScience/architecture/archBasicNotes.md","lastUpdated":1627194884000}',s={},i=a('

Architecture Basic Notes

SuperPipeline and SuperScalar

A superpipeline processor own a 5-20 stage pipeline, a superscalar issues 3-8 instructions in parallel (more functional units like integer/float units).

Instructions Dependencies and Latencies

The number of cycles between when an instruction reaches the execute stage and when its result is available for use by other instructions is called the instruction's latency. The deeper the pipeline, the more stages and thus the longer the latency. The processor will need to stall the execution of the instructions until their data is available (dependencies), inserting a bubble into the pipeline where no work gets done, making multiple issue in this case impossible.

Branch Prediction

Another key problem for pipelining is branches (flush out all instructions of wrong branch). Unfortunately, even the best branch prediction techniques are sometimes wrong, and with a deep pipeline many instructions might need to be cancelled. This is called the mispredict penalty. The deeper the pipeline, the further into the future you must try to predict, the more likely you'll be wrong, and the greater the mispredict penalty when you are.

Predication can be used to eliminate branches such as cmovle (move data only when testing flag stays less or equal state). The Alpha architecture had a conditional move instruction from the very beginning. MIPS, SPARC, x86 added it later and the ARM architecture was the first architecture with a fully predicated instruction set (though the early ARM processors only had short pipelines and small mispredict penalties).

VLIW (Very Long Instruction Word)

In cases where backward compatibility is not an issue, it is possible for the instruction set itself to be designed to explicitly group instructions to be executed in parallel. A VLIW processor's instruction flow is much like a super-scalar, except the decode/dispatch stage is much simpler and only occurs for each group of sub-instructions. No VLIW designs have yet been commercially successful as mainstream CPUs.

Out of Order Execution (OoO or OoE)

If branches and long-latency instructions are going to cause bubbles in the pipeline(s), then perhaps those empty cycles can be used to do other work. To achieve this, the instructions in the program must be reordered (instruction scheduling and register renaming). Compiler completes static instruction scheduling (rearranged instruction stream at compile time), processor completes dynamic instruction scheduling (renaming registers and reorder instruction stream at runtime). The processor must keep a mapping of the instructions in flight at any moment and the physical registers they use. The extra logic of scheduler is particularly power-hungry because those transistors are always working.

The Brainiac vs Speed-demon Debate

Brainiac designs are at the smart-machine end of the spectrum, with lots of OOO hardware trying to squeeze every last drop of instruction-level parallelism out of the code, even if it costs millions of logic transistors and years of design effort to do it. In contrast, speed-demon designs are simpler and smaller, relying on a smart compiler and willing to sacrifice a little bit of instruction-level parallelism for the other benefits that simplicity brings. Which would you rather have: 4 powerful brainiac cores, or 8 simpler in-order cores? When it comes to the brainiac debate, many vendors have gone down one path then changed their mind and switched to the other side.

Power Wall and ILP Wall

Power usage goes up even faster than clock speed does (increasing clock speed by 20% with 50% more power usage, O(power) = frequency * Voltage * Voltage). Leakage current also goes up as the voltage is increased, leakage generally goes up as the temperature increases as well. The power and heat problems become unmanageable, because it's simply not possible to provide that much power and cooling to a silicon chip. Thus, going purely for clock speed is not the best strategy.

normal programs just don't have a lot of fine-grained parallelism in them, due to a combination of load latencies, cache misses, branches and dependencies between instructions. This limit of available instruction-level parallelism is called the ILP wall.

Decoupled x86 microarchitecture

Dynamically decode the x86 instructions into simple, RISC-like micro-instructions (μops, pronounced "micro-ops"), which can then be executed by a fast, RISC-style register-renaming OOO superscalar core. The pipeline depth of Core i2/i3 Sandy/Ivy Bridge was shown as 14/19 stages in the earlier section on superpipeline, it is 14 stages when the processor is running from its L0 μop cache (which is the common case), but 19 stages when running from the L1 instruction cache and having to decode x86 instructions and translate them into μops.

SMT (Hardware Threads)

Even the most aggressively brainiac OOO superscalar processor will still almost never exceed an average of about 2-3 instructions per cycle when running most mainstream, real-world software, due to a combination of load latencies, cache misses, branching and dependencies between instructions.

Simultaneous multi-threading (SMT) is a processor design technique which exploits thread-level parallelism (other running programs, or other threads within the same program). The instructions come from multiple threads running at the same time, all on the one processor core. An SMT processor uses just one physical processor core to present two or more logical processors to the system. Separate units include the program counter, the architecturally-visible registers, the memory mappings held in the TLB, shared units include the decoders and dispatch logic, the functional units, and the caches. SMT is essentially a way to convert TLP into ILP.

However, in practice, at least for desktops, laptops, tablets, phones and small servers, it is rarely the case that several different programs are actively executing at the same time, so it usually comes down to just the one task the machine is currently being used for. Some applications, such as database systems, image and video processing, audio processing, 3D graphics rendering and scientific code, do have obvious high-level (coarse-grained) parallelism available and easy to exploit, but many of these applications which are easy to parallelize are primarily limited by memory bandwidth, not by the processor.

If one thread saturates just one functional unit which the other threads need, it effectively stalls all of the other threads, even if they only need relatively little use of that unit. Competition between the threads for cache space may produce worse results than letting just one thread have all the cache space available, particularly for software where the critical working set is highly cache-size sensitive, such as hardware simulators/emulators, virtual machines and high-quality video encoding.

Due to above 3 reasons, SMT performance can actually be worse than single-thread performance (traditional context switching between threads) sometimes.

More cores or Wider cores

Very wide superscalar designs scale very badly in terms of both chip area and clock speed, so a single 10-issue core would actually be both larger and slower than two 5-issue cores:

  • the complex multiple-issue dispatch logic scales up as (issue width)^2
  • highly multi-ported register files and caches to service all those simultaneous accesses

Today, a "typical" SMT design implies both a wide execution core and OOO execution logic, including multiple decoders, the large and complex superscalar dispatch logic and so on. For applications with lots of active but memory-latency-limited threads (database systems, 3D graphics rendering), more simple cores would be better because big/wide cores would spend most of their time waiting for memory anyway. For most applications, however, there simply are not enough threads active to make this viable, and the performance of just a single thread is much more important, so a design with fewer but bigger, wider, more brainiac cores is more appropriate.

Intel's Xeon Haswell, the server version of Core i*4 Haswell, uses 5.7 billion transistors to provide 18 cores (up from 8 in Xeon Sandy Bridge), each a very aggressively brainiac 8-issue design (up from 6-issue in Sandy Bridge), each still with 2-thread SMT. IBM's POWER8 uses 4.4 billion transistors to move to a considerably more brainiac core design than POWER7, and at the same time provide 12 cores (up from 8 in POWER7), each with 8-thread SMT (up from 4 in POWER7).

In the future we might see asymmetric designs, with one or two big, wide, brainiac cores plus a large number of smaller, narrower, simpler cores. IBM's Cell processor (used in the Sony PlayStation 3) was arguably the first such design, but unfortunately it suffered from severe programmability problems because the ISA incompatible between small cores and large main core and had limited by awkward access to main memory. Some modern ARM designs also use an asymmetric approach, with several large cores paired with one or a few smaller, simpler "companion" cores, to increase battery life.

DLP (data-level parallelism)

Rather than looking for ways to execute groups of instructions in parallel, the idea is to look for ways to make one instruction apply to a group of data values in parallel.

SIMD Vector Instructions

One of DLP methods called SIMD parallelism (single instruction, multiple data). More often, it's called vector processing. With some thought, a small set of vector instructions can enable some impressive speedups, such as packing/unpacking, byte shuffling, bit masking instructions, just like x86 Matrix Math Extensions (MMX), Streaming SIMD Extensions (SSE), and ongoing revisions of Advanced Vector Extensions (AVX). MMX provide 64-bit vectors, x86 SSE added 8 new 128-bit registers, then widened to 256 bits with AVX.

Memory

Memory Wall

Latency is especially bad for loads from memory, which make up about a quarter of all instructions. Using a modern SDRAM with a CAS latency of 11, will typically be 24 cycles of the memory system bus, 1 to send the address to the DIMM (memory module), RAS-to-CAS delay of 11 for the row access, CAS latency of 11 for the column access, and a final 1 to send the first piece of data up to the processor (or E-cache). On a multi-processor system, even more bus cycles may be required to support cache coherency between the processors. There are the cycles within the processor itself, checking the various on-chip caches before the address even gets sent to the memory controller, accounting for 20 CPU cycles. For 2.4GHz processor and 800MHz SDRAM memory, summing up to (1+11+11+1) * 2400/800 + 20 = 92 CPU cycles, a 4.0 GHz processor would wait a staggering 140 cycles to access main memory. This problem of the large, and slowly growing, gap between the processor and main memory is called the memory wall.

Caches

Modern processors solve the problem of the memory wall with caches. A cache is a small but fast type of memory located on or near the processor chip. Its role is to keep faster copies of small pieces of main memory, L1 caches around 8-64K in size, L2 caches around 100K-10M in size, larger and slower L3 caches. A modern primary (L1) cache has a latency of just 2 to 4 processor cycles, with around 90% caches hit rates.

The memory hierarchy of a modern desktop/laptop: Core i4 Haswell.

LevelSizeLatency (cycles)Location
L1 Cache32KB4inside each core
L2 Cache256KB12beside each core
L3 Cache6MB~21shared between all cores
L4 E-Cache128MB~58separate eDRAM chip
RAM8+GB~117SDRAM DIMMs on motherboard
Swap100+GB10000+hard disk or SSD

Cache Locality

Temporal locality is exploited by merely keeping recently accessed data in the cache. To take advantage of spatial locality, data is transferred from main memory up into the cache in blocks of a few dozen bytes at a time, called a cache line.

Cache Layout

Using the virtual address might cause caches need to be flushed on every context switch (thrashing) (2 programs mapping a same virtual address to different physical address). Using the physical address means the V2N mapping must be performed, making every cache lookup slower. A common trick is to use virtual addresses for the cache indexing but physical addresses for the tags. The virtual-to-physical mapping (TLB lookup) can then be performed in parallel with the cache indexing so that it will be ready in time for the tag comparison. Such a scheme is called a virtually-indexed physically-tagged cache.

Set-associative caches are able to avoid some unfortunate cache conflicts. Unfortunately, the more highly associative a cache is, the slower it is to access. The instruction L1 cache can afford to be highly set-associative (prefetching and buffering in pipeline), but the data L1 cache settled on 4-way set-associative as the sweet spot. The large L2/L3 cache (LLC for "last-level cache") is also usually highly associative, perhaps as much as 12- or 16-way. External E-cache is sometimes direct-mapped for flexibility of size and implementation.

Memory Latency and Bandwidth

Lower-latency designs will be better for pointer-chasing code, such as compilers and database systems. Bandwidth-oriented (adding more memory banks and making the busses wider) systems have the advantage for programs with simple, linear access patterns, such as image processing and scientific code.

Latency is much harder to improve than bandwidth. Synchronously clocked DRAM (SDRAM) allowed pipelining of the memory system. This reduces effective latency because it allows a new memory access to be started before the current one has completed, while an asynchronous memory system had to wait for the transfer of half a cache line from the previous access before starting a new request.

Distributed System

  • 小型机是专门设计的硬件和专门设计的软件,只面向这种规模(例如几百颗 CPU)的计算
  • 小型机是完全闭源的,不需要考虑扩展性,特定的几种硬件在稳定性上前进了一大步
  • x86 的 IO 性能被架构锁死了,各种总线、PCI、PCIe、USB、SATA、以太网,为了个人计算机的便利性,牺牲了很多的性能和可靠性
  • 小型机使用总线通信,可以实现极高的信息传递效率,极其有效的监控以及极高的故障隔离速度

x86 服务器基于网络的分布式具有天然的缺陷:

  • 操作系统决定了网络性能不足
  • 网络需要使用事件驱动处理,比总线电路的延迟高几个数量级
  • PC 机的硬件不够可靠,故障率高
  • 很难有效监控,隔离故障速度慢

Reference

',57);s.render=function(a,r,s,o,n,l){return e(),t("div",null,[i])};export default s;export{r as __pageData}; diff --git a/assets/computerScience_architecture_archBasicNotes.md.1ed20244.lean.js b/assets/computerScience_architecture_archBasicNotes.md.1ed20244.lean.js new file mode 100644 index 00000000000..e7054489a84 --- /dev/null +++ b/assets/computerScience_architecture_archBasicNotes.md.1ed20244.lean.js @@ -0,0 +1 @@ +import{o as e,c as t,a}from"./app.6dd2a1b7.js";const r='{"title":"Architecture Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"SuperPipeline and SuperScalar","slug":"superpipeline-and-superscalar"},{"level":3,"title":"Instructions Dependencies and Latencies","slug":"instructions-dependencies-and-latencies"},{"level":3,"title":"Branch Prediction","slug":"branch-prediction"},{"level":2,"title":"VLIW (Very Long Instruction Word)","slug":"vliw-very-long-instruction-word"},{"level":2,"title":"Out of Order Execution (OoO or OoE)","slug":"out-of-order-execution-ooo-or-ooe"},{"level":3,"title":"The Brainiac vs Speed-demon Debate","slug":"the-brainiac-vs-speed-demon-debate"},{"level":3,"title":"Power Wall and ILP Wall","slug":"power-wall-and-ilp-wall"},{"level":3,"title":"Decoupled x86 microarchitecture","slug":"decoupled-x86-microarchitecture"},{"level":2,"title":"SMT (Hardware Threads)","slug":"smt-hardware-threads"},{"level":3,"title":"More cores or Wider cores","slug":"more-cores-or-wider-cores"},{"level":2,"title":"DLP (data-level parallelism)","slug":"dlp-data-level-parallelism"},{"level":3,"title":"SIMD Vector Instructions","slug":"simd-vector-instructions"},{"level":2,"title":"Memory","slug":"memory"},{"level":3,"title":"Memory Wall","slug":"memory-wall"},{"level":3,"title":"Caches","slug":"caches"},{"level":3,"title":"Memory Latency and Bandwidth","slug":"memory-latency-and-bandwidth"},{"level":2,"title":"Distributed System","slug":"distributed-system"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"computerScience/architecture/archBasicNotes.md","lastUpdated":1627194884000}',s={},i=a('',57);s.render=function(a,r,s,o,n,l){return e(),t("div",null,[i])};export default s;export{r as __pageData}; diff --git a/assets/computerScience_architecture_graphProcessingBasicNotes.md.e60be5c5.js b/assets/computerScience_architecture_graphProcessingBasicNotes.md.e60be5c5.js new file mode 100644 index 00000000000..01b3a1d5959 --- /dev/null +++ b/assets/computerScience_architecture_graphProcessingBasicNotes.md.e60be5c5.js @@ -0,0 +1 @@ +import{o as e,c as a,a as t}from"./app.6dd2a1b7.js";const r='{"title":"Graph Processing Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Graph System","slug":"graph-system"},{"level":2,"title":"NUMA Architecture","slug":"numa-architecture"},{"level":3,"title":"CPU Schedule","slug":"cpu-schedule"},{"level":3,"title":"Memory Schedule","slug":"memory-schedule"},{"level":2,"title":"Dataset","slug":"dataset"},{"level":2,"title":"Tools","slug":"tools"},{"level":3,"title":"Concurrency Lib","slug":"concurrency-lib"},{"level":3,"title":"Perf Tools","slug":"perf-tools"},{"level":3,"title":"Hardware Performance Counter","slug":"hardware-performance-counter"},{"level":3,"title":"Parallel Programming","slug":"parallel-programming"},{"level":3,"title":"Other Libs","slug":"other-libs"},{"level":3,"title":"DRAMSim2","slug":"dramsim2"},{"level":3,"title":"GCC","slug":"gcc"},{"level":3,"title":"Time Stamp Counter","slug":"time-stamp-counter"},{"level":3,"title":"NUMA Tool","slug":"numa-tool"}],"relativePath":"computerScience/architecture/graphProcessingBasicNotes.md","lastUpdated":1627194884000}',n={},s=t('

Graph Processing Basic Notes

Graph System

ClassificationIn-memoryOut-of-core
Single machineLigraGraphChi
PolymerX-Stream
GaloisGridGraph
Mosaic
DistributedPregelChaos
PowerGraph
PowerLyra
Gemini

NUMA Architecture

引入了 Node 和 Distance:

  • 对于 CPU 和 Memory 这两种最宝贵的硬件资源, NUMA 用近乎严格的方式划分了所属的资源组 (Node), 而每个资源组内的 CPU 和 Memory 几乎相等
  • 资源组的数量取决于物理 CPU 的个数
  • Distance 用来定义各个 Node 之间调用资源的开销, 为资源调度优化算法提供数据支持

每个进程(或线程)都会从父进程继承 NUMA 策略, 并分配有一个优先 node. 如果 NUMA 策略允许的话,进程可以调用其他 node 上的资源.

CPU Schedule

  • CPU Node Bind: 规定进程运行在某几个 node 之上
  • Physical CPU Bind: 精细地规定进程运行在哪些核上

Memory Schedule

  • Local Allocation: 从当前 Node 上请求分配内存
  • Preferred: 比较宽松地指定了一个推荐的 node 来获取内存, 如果被推荐的 node 上没有足够内存, 进程可以尝试别的 node
  • Memory Bind: 可以指定若干个 node,进 程只能从这些指定的 node 上请求分配内存
  • Interleave: 规定进程从指定的若干个 node 上以 RR (Round Robin) 交织地请求分配内存

NUMA 默认的内存分配策略是优先在进程所在 CPU 的本地内存中分配, 会导致 CPU 节点之间内存分配不均衡. 当某个 CPU 节点的内存不足时, 会导致 swap 产生, 而不是从远程节点分配内存, 这就是 swap insanity 现象

Dataset

Tools

Concurrency Lib

Perf Tools

Hardware Performance Counter

Parallel Programming

Other Libs

DRAMSim2

GCC

strict-alias warnings

for strict-aliasing warnings:

  1. use a union to represent the memory need to reinterpret
  2. use a reinterpret_cast, cast via char * at the point where reinterpret the memory - char * are defined as being able to alias anything
  3. use a type which has __attribute__((__may_alias__))
  4. turn off the aliasing assumptions globally using -fno-strict-aliasing

Time Stamp Counter

RDTSC

Clock Get Time

gcc *.c -o *.o ... -lrt # link with librt\n
1
#include <time.h>\n\nstruct timespec ts;\nclock_gettime(CLOCK_MONOTONIC, ts);\nprintf("%d %d", ts.tv_sec, ts.tv_nsec);\n
1
2
3
4
5

NUMA Tool

grep -i numa /var/log/dmesg\n
1

NUMA Control

installation
sudo apt install -y numactl\n
1
usage
numactl --show\nnumactl --hardware\nnumactl --interleave=all\n
1
2
3

NUMA Status

numastat\n
1
',47);n.render=function(t,r,n,l,i,o){return e(),a("div",null,[s])};export default n;export{r as __pageData}; diff --git a/assets/computerScience_architecture_graphProcessingBasicNotes.md.e60be5c5.lean.js b/assets/computerScience_architecture_graphProcessingBasicNotes.md.e60be5c5.lean.js new file mode 100644 index 00000000000..069812ef82e --- /dev/null +++ b/assets/computerScience_architecture_graphProcessingBasicNotes.md.e60be5c5.lean.js @@ -0,0 +1 @@ +import{o as e,c as a,a as t}from"./app.6dd2a1b7.js";const r='{"title":"Graph Processing Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Graph System","slug":"graph-system"},{"level":2,"title":"NUMA Architecture","slug":"numa-architecture"},{"level":3,"title":"CPU Schedule","slug":"cpu-schedule"},{"level":3,"title":"Memory Schedule","slug":"memory-schedule"},{"level":2,"title":"Dataset","slug":"dataset"},{"level":2,"title":"Tools","slug":"tools"},{"level":3,"title":"Concurrency Lib","slug":"concurrency-lib"},{"level":3,"title":"Perf Tools","slug":"perf-tools"},{"level":3,"title":"Hardware Performance Counter","slug":"hardware-performance-counter"},{"level":3,"title":"Parallel Programming","slug":"parallel-programming"},{"level":3,"title":"Other Libs","slug":"other-libs"},{"level":3,"title":"DRAMSim2","slug":"dramsim2"},{"level":3,"title":"GCC","slug":"gcc"},{"level":3,"title":"Time Stamp Counter","slug":"time-stamp-counter"},{"level":3,"title":"NUMA Tool","slug":"numa-tool"}],"relativePath":"computerScience/architecture/graphProcessingBasicNotes.md","lastUpdated":1627194884000}',n={},s=t('',47);n.render=function(t,r,n,l,i,o){return e(),a("div",null,[s])};export default n;export{r as __pageData}; diff --git a/assets/computerScience_compilers_compilersBasicNotes.md.5ebb309b.js b/assets/computerScience_compilers_compilersBasicNotes.md.5ebb309b.js new file mode 100644 index 00000000000..26ead80fe2a --- /dev/null +++ b/assets/computerScience_compilers_compilersBasicNotes.md.5ebb309b.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"Compiler Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Concepts","slug":"basic-concepts"},{"level":3,"title":"Definition of compilers","slug":"definition-of-compilers"},{"level":3,"title":"Structure of compilers","slug":"structure-of-compilers"},{"level":2,"title":"文法与语言","slug":"文法与语言"},{"level":3,"title":"符号与符号串","slug":"符号与符号串"},{"level":3,"title":"文法与语言的形式化表示","slug":"文法与语言的形式化表示"},{"level":3,"title":"乔姆斯基文法体系","slug":"乔姆斯基文法体系"},{"level":3,"title":"上下文无关文法","slug":"上下文无关文法"},{"level":3,"title":"正规文法","slug":"正规文法"},{"level":3,"title":"正则语言(Regular Expressions)","slug":"正则语言-regular-expressions"},{"level":3,"title":"分析树","slug":"分析树"},{"level":3,"title":"文法规则","slug":"文法规则"},{"level":2,"title":"Lexical Analysis","slug":"lexical-analysis"},{"level":3,"title":"Tokenizer - 词法分析器","slug":"tokenizer-词法分析器"},{"level":3,"title":"有限状态自动机(Finite Automaton)","slug":"有限状态自动机-finite-automaton"},{"level":3,"title":"自动词法分析器","slug":"自动词法分析器"},{"level":2,"title":"Syntax Analysis(语法分析)","slug":"syntax-analysis-语法分析"},{"level":3,"title":"自顶向下分析","slug":"自顶向下分析"},{"level":3,"title":"自底向上分析","slug":"自底向上分析"},{"level":3,"title":"抽象语法树","slug":"抽象语法树"},{"level":2,"title":"Semantic Analysis(语义分析)","slug":"semantic-analysis-语义分析"},{"level":3,"title":"类型系统(type system)","slug":"类型系统-type-system"},{"level":3,"title":"符号表(上下文相关)","slug":"符号表-上下文相关"},{"level":3,"title":"类型检查","slug":"类型检查"},{"level":3,"title":"作用域检查","slug":"作用域检查"},{"level":3,"title":"类型相容性","slug":"类型相容性"},{"level":3,"title":"错误诊断","slug":"错误诊断"},{"level":2,"title":"Immediate Representation","slug":"immediate-representation"},{"level":3,"title":"三地址码","slug":"三地址码"},{"level":3,"title":"控制流图","slug":"控制流图"},{"level":2,"title":"代码优化","slug":"代码优化"},{"level":3,"title":"组织管理","slug":"组织管理"},{"level":3,"title":"优化类型","slug":"优化类型"},{"level":3,"title":"局部优化","slug":"局部优化"},{"level":3,"title":"全局优化","slug":"全局优化"},{"level":2,"title":"Code Generation(代码生成)","slug":"code-generation-代码生成"},{"level":3,"title":"递归下降代码生成算法","slug":"递归下降代码生成算法"},{"level":2,"title":"Garbage Collection","slug":"garbage-collection"},{"level":3,"title":"Mark and Sweep","slug":"mark-and-sweep"},{"level":3,"title":"Stop and Copy","slug":"stop-and-copy"},{"level":2,"title":"Compilers Exercise","slug":"compilers-exercise"},{"level":3,"title":"C Declaration Interpreter","slug":"c-declaration-interpreter"},{"level":3,"title":"Cool Language","slug":"cool-language"},{"level":3,"title":"Parser Implementation","slug":"parser-implementation"}],"relativePath":"computerScience/compilers/compilersBasicNotes.md","lastUpdated":1627194884000}',t={},e=a('

Compiler Basic Notes

Basic Concepts

Definition of compilers

  • program_code ---compiler---> executable
  • data ---executable---> output

e.g Fortran(formula translation) 1 project

Structure of compilers

front-end to back-end:

  • front-end: src ---lexical analysis---> tokens ---parsing/syntax analysis---> AST(Abstract Syntax Tree) ---semantic analysis---> intermediate
  • back-end: intermediate ---...---> ... ---...---> ... ---code generation---> dist

details:

  • lexical analysis(词法分析)
  • parsing/syntax analysis(语法分析)
  • semantic analysis(语义分析): type and scope
  • optimization
  • code generation: translate to other high level language/assembly code/machine code

文法与语言

符号与符号串

  • 字母表/符号集: 元素的非空有穷集合
  • 符号串: 字母表中的符号组成的任何有穷序列
  • 固有头/尾: 非空首/尾子串
  • 闭包: Σ* = Σ0 U Σ1 U ... U Σn ...
  • 正闭包: Σ* = Σ1 U ... U Σn ...

文法与语言的形式化表示

  • 文法(Grammar) G = (Vn, Vt, P, S) - 非终结符集, 终结符集, 规则/产生式集, 开始符号
  • 语言(Language) L(G) = {x | S -> x, x <- Vt} 文法 G 一切句子的集合
  • 句型: rhs of P, 句子: 不含非终结符的右部
  • 直接推导: v -> w, 闭包推导: v -*> w, 正闭包推导: v -+> w

乔姆斯基文法体系

  • ((((3)2)1)0)
  • 0 型文法: 任意文法
  • 1 型文法: 上下文有关文法(context sensitive) αAβ -> αηβ
  • 2 型文法(语法工具): 上下文无关文法 A -> α
  • 3 型文法(词法工具): 正规文法

上下文无关文法

文法表示

G = (S, N, T, P):

  • S: 开始符
  • N: 非终结符集合
  • T: 终结符集合
  • P: 产生式规则集合 X -> beta1, beta2, ..., betaN, X <- N, beta <- N+T

形式化表示

简易表示
Sentence -> Noun Verb Noun\nNoun -> sheep\n  | tiger\n  | grass\n  | water\nVerb -> eat\n  | drink\n
1
2
3
4
5
6
7

S: Sentence, N: Sentence/Verb/Noun, T: sheep/tiger/grass/water/eat/drink

E -> num\n  |id\n  |E + E\n  |E `*` E\n
1
2
3
4

S: E, N: E, T: num/id/+/*

巴科斯范式

Backus-Naur Form:

  • ::= : "被定义为"
  • "word" : 字符本身
  • 双引号外的字 : 语法部分
  • 尖括号( < > ) : 必选项(非终结符)
  • 方括号( [ ] ) : 0/1
  • 大括号( { } ) : 0/n
  • 竖线( | ) : "OR"

正规文法

正规文法可与正则语言相互转化:

  • A -> aB|Ba
  • A -> a

正则语言(Regular Expressions)

基本定义

对于给定的字符集 C:

  • 空串 "\\0" 是正则表达式
  • 任意 char <- C 是正则表达式
  • 若 M, N 是正则表达式, 则 M|N = {M, N}, MN = {mn|m <- M, n <- N}, M* = {"\\0", M, MM, MMM, ...} (选择/连接/闭包)也是正则表达式

形式表示

// 具有顺序性\ne -> "\\0" // basic definition\n  | c // basic definition\n  | e | e // recursive definition\n  | ee // recursive definition\n  | e* // recursive definition\n
1
2
3
4
5
6

正则语法糖(Syntax Sugar)

  • [a-z] : a|...|z
  • c? : 0/1 个 c
  • c+ : 1/n 个 c
  • c{i, j} : i-j 个 c
  • "a*" : a* 自身(非 Kleene Closure)
  • . : 除 ‘\\n’ 外的任意字符
// 标识符\n[a-zA-Z\\_][a-zA-Z\\_0-9]*\n\n// decimal integer\n(+|-)?(0|[1-9][0-9]*)\n\n// decimal float\n(+|-)?(0|[1-9][0-9]*|)?\\.[0-9]+\n
1
2
3
4
5
6
7
8

分析树

进行文法推导时生成的树:

  • 根 : 开始符
  • 内部结点 : 非终结符
  • 叶子结点 : 终结符
  • 层 : 一步推导(优先级影响推导顺序)
  • 叶子结点串: 最终表达式
  • 后序遍历 : 最终结果

推导类型

  • 最左推导(leftmost)
  • 最右推导(rightmost)(规范推导)

句型分析

  • 短语: 若 S -*> Aβ, A -+> α, 则称 α 是句型 αβ 相对于非终结符 A 的短语
  • 直接短语: S -*> Aβ, A -> α
  • 一个右句型的直接短语称为该句型的句柄(用于自下而上的归约分析)
  • 最左归约: 归约最左的句柄, 最右归约: 归约最右的句柄

meaning function(多对一)

L(syntax) = semantic: 多个语法对应一个语义(不同形式的表达式对应同一个意思)

二义性文法(一对多)

  • 最左推导与最右推导得出的分析树不一致
  • 若给定文法 G, 对于句子 s, 其有 2 种不同的分析树, 则称 G 是二义性文法
  • 若一个上下文无关语言的所有文法都是二义性文法, 则称此语言是先天二义语言
文法重写
E -> E + T\n  |T\nT -> T * F\n  |F\nF -> num\n  |id\n
1
2
3
4
5
6

消除 + 与 * 的二义性, 如 3+4*5

优先级与结合性

声明优先级与结合性可在一定程度上消除文法的二义性

文法规则

  • 有害规则: 使文法产生二义性的规则
  • 多余规则: 不可达/不可终止的规则
  • 2 型文法的 ε 规则: 当语言中不含有 ε 符号串, 则一定存在终结符集不含有 ε 的等价文法(代入法消除 ε)
  • 保证非终结符 A 的有效性: S -*> αAβ, A -+> t

Lexical Analysis

Tokenizer - 词法分析器

  • Maximal match
  • Higher priority match

转移图算法

token nextToken(void) {\n  char c = getChar();\n  switch(c) {\n    case '<':\n      c = getChar();\n      switch (c) {\n        case '=':\n          return LE;\n        case '>':\n          return NE;\n        default:\n          rollback();\n          return LT;\n      }\n\n    case '=':\n      return EQ;\n    case '>':\n      c = getChar();\n      switch (c) {\n        case '=':\n          return GE;\n        case '<':\n          return NE;\n        default:\n          rollback();\n          return GT;\n      }\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

关键字(keyword)处理

  • 根据 完美哈希算法(无冲突哈希函数) , 建立所有关键字对应的关键字完美哈希表
  • 读入有效标识符(字符串型)后, 查询关键字哈希表, 检查当前标识符是否为关键字
#define KEYWORD_MAX_LEN 10\n\nhash_one(char *str, int len) {\n    unsigned int keyValue = 0;\n\n    for (int i = 0; i < len; i++) {\n        keyValue += str[i] * ((int)pow(33, len - i));\n    }\n\n    keyValue = (keyValue * 3 + 7) % KEYWORD_MAX_LEN;\n  return keyValue;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
#define KEYWORD_HASH_SEED 131\n\nhash_two(char *str, int len) {\n  unsigned int keyValue = 0,\n           hash = 0;\n\n    for (int i = 0; i < len; i++) {\n        hash = hash * KEYWORD_HASH_SEED + str[i];\n    }\n\n    keyValue = hash & 0x7fffffff;\n    return keyValue;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13

有限状态自动机(Finite Automaton)

确定有限状态自动机(Deterministic Finite Automaton)

  • Only a transition for a state with a input
  • No epsilon moves

M = (AlphaSet/InputSet, StateSet, currentState, FiniteStateSet, transferFunction)

A = {a, b}, SS = {0, 1, 2}, cS = 0, FS = {2},\ntransferFunction = {\n  (cS0, a) -> cS1, (cS0, b) -> cS0,\n  (cS1, a) -> cS2, (cS1, b) -> cS1,\n  (cS2, a) -> cS2, (cS2, b) -> cS2,\n}\n
1
2
3
4
5
6
状态转移表实现 DFA
状态\\字符ab
010
121
222

非确定有限状态自动机(Nondeterministic Finite Automaton)

transferFunction 中的次态不确定/不唯一(为一个开集合):

  • Multiple transitions for a state with a input
  • can epsilon moves

(cS0, a) -> {cS1, cS2}

自动词法分析器

RegExp --Thompson 算法--> NFA --子集构造算法--> DFA --Hopcroft 最小化算法--> 词法分析器代码

Thompson 算法

RegExp --> NFA:

  • 直接构造基本 RegExp
  • 递归构造复合 RegExp
  • epsilon : i --epsilon--> f
  • RegExp : i --NFA(RegExp)--> f
  • 选择 : i --NFA(RegExp1)--> f, i --NFA(RegExp2)--> f
  • 连接 : i --NFA(RegExp1)--> m --NFA(RegExp2)--> f
  • 闭包 : i --epsilon--> m --epsilon--> f, m --RegExp--> m

子集构造算法

NFA --> DFA:

由 Thompson 算法生成的 NFA, 当且仅当输入为 epsilon 时, 次态不唯一

  • 将所有可达到次态作为一个集合 s, 视为单一次态 s
  • delta(Sigma) + epsilon-closure(深度/广度优先遍历找寻可达到次态边界)
DFA subset_construction(NFA nfa) {\n  s0 = eps_closure(n0);\n\n  StateSet += s0;\n  enqueue(s0);\n\n  while (work_queue != []) {\n    dequeue(s);\n\n    foreach (ch in InputSet) {\n      next_state = eps_closure(delta(s, ch));\n      Fn[s, ch] = next_state;  // DFA 中的转移函数\n\n      if (next_state not in StateSet) {\n        StateSet += next_state;\n        enqueue(next_state);\n      }\n    }\n  }\n\n  return DFA(StateSet, Fn);\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Hopcroft 算法

最小化 DFA(数字逻辑中的最简状态表), 合并等价状态(等价类)

split(StateSet S) {\n  foreach (char ch) {\n    if (ch can split S) {\n      split S into S1, ..., Sk;\n    }\n  }\n}\n\nhopcroft(DFA) {\n  split all nodes into InitStateSet and FiniteStateSet (Two State Sets);\n\n  while (set is still changes) {\n    split(S);\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

实现

DFA
有向图
转移表
  • 行: 现态
  • 列: 输入
  • 值: 次态/ERROR/-1

驱动代码: table 用于实现 switch/case, stack 用于实现最长匹配

next_token() {\n  state = 0;\n  stack = [];\n\n  while (state != ERROR) {\n    c = getChar();\n\n    if (state is ACCEPT/FINITE) {\n      clear(stack);\n    }\n\n    push(state);\n    state = table[state][c];\n  }\n\n  while (state is not ACCEPT/FINITE) {\n    state = pop();\n    rollback();\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Syntax Analysis(语法分析)

Tokens + Grammar --Syntax Analysis--> AST(Abstract Syntax Tree)

自顶向下分析

  • 从开始符号出发推导任意句子 t, 与给定句子 s 进行比较分析
  • 利用分析树进行逐叶子匹配, 若匹配失败则进行回溯
bool top_down_parsing(tokens[]) {\n  i = 0;\n  stack = [S];\n\n  while (stack != []) {\n    if (stack[top] is a terminal t) {\n      t == tokens[i] ? pop(i++) : backtrack();\n    } else if (stack[top] is a non_terminal T) {\n      pop();\n      push(T next expansion); // 自右向左压栈, e.g pop(S), push(N_right), push(V), push(N_left)\n    } else {\n      throw new SyntaxError();\n    }\n  }\n\n  return i >= tokens.length && is_empty(stack) ? true : false;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

避免回溯

利用前看符号避免回溯

Sentence -> Noun Verb Noun\nNoun -> sheep\n  | tiger\n  | grass\n  | water\nVerb -> eat\n  | drink\n
1
2
3
4
5
6
7

tiger eat water: 向前看非终结符推导出的所有终结符中匹配 tiger 的终结符; 不向前看,则先推导 N, 再推导 n, 但 n 不一定匹配 tiger, 则需进行回溯; 向前看一个字符, 直接推导 N --> n, 同时直接找寻匹配 tiger 的终结符

S -> N V N\nN -> (sheep)tiger\nV -> eat\nN -> (sheep-tiger-grass)water\n
1
2
3
4

递归下降分析算法(Recursive Descent/预测分析算法)

  • 分治算法: 每个非终结符构造一个分析函数
  • 前看符号: 用前看符号指导产生式规则的选择(expansion)
parse_S(tokens[]) {\n  parse_N(tokens[0]);\n  parse_V(tokens[1]);\n  parse_N(tokens[2]);\n}\n\nparse_N(token) {\n  if (token == s|t|g|w) {\n    return true;\n  } else {\n    throw new SyntaxError();\n  }\n}\n\nparse_V(token) {\n  if (token == e|d) {\n    return true;\n  } else {\n    throw new SyntaxError();\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
算法实现
  • tip one: use save pointer to implement roll back
  • tip two: use logical OR expression to replace nested if-else structure
bool term(TOKEN tok) {\n    return *next++ == tok;\n}\n\nbool E1(void) {\n    return T();\n}\n\nbool E2(void) {\n    return T()\n        && term(PLUS)\n        && E();\n}\n\nbool E(void) {\n    // roll back pointer\n    TOKEN *save = next;\n\n    return (next = save, E1())\n        || (next = save, E2());\n}\n\nbool T1(void) {\n    return term(INT);\n}\n\nbool T2(void) {\n    return term(INT)\n        && term(TIMES)\n        && T();\n}\n\nbool T3(void) {\n    return term(OPEN)\n        && E()\n        && term(CLOSE);\n}\n\nbool T(void) {\n    // roll back pointer\n    TOKEN *save = next;\n\n    return (next = save, T1())\n        || (next = save, T2())\n        || (next = save, T3());\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// X -> a\n//  | XX\n//  | aXXX\n//  | aXXXXb\nparse_X() {\n  token = nextToken();\n\n  switch (token) {\n    case ...: // i: token == atom_char or parse_XX();\n    case ...: // j: token == atom_char, token = nextToken(), parse_XXX();\n    // k: token == atom_char, token = nextToken(),\n    // parse_XXXX(), token=nextToken(), token == b\n    case ...:\n    default: throw new SyntaxError();\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

LL(1)分析算法

  • 从左(L)向右读入程序(left to right scan)
  • 最左(L)推导: 优先推导最左侧非终结符(leftmost derivation)
  • 一个(1)前看符号(look ahead)
  • 分治算法: 每个非终结符构造一个first set 和一个 follow set, 最后为每个规则构造一个 select set
  • 分析表驱动(由 first sets/follow sets/select sets 推导分析表)
bool ll1_parsing(tokens[]) {\n  i = 0;\n  stack = [S];\n\n  while (stack != []) {\n    if (stack[top] is a terminal t) {\n      t == tokens[i] ? pop(i++) : throw new SyntaxError();\n    } else if (stack[top] is a non_terminal T) {\n      pop();\n      // push(T correct expansion);\n      // 自右向左压栈, e.g pop(S), push(N_right), push(V), push(N_left)\n      push(select_table[T][tokens[i]] 对应项(规则编号)所对应规则的右边式子);\n    } else {\n      throw new SyntaxError();\n    }\n  }\n\n  return i >= tokens.length && is_empty(stack) ? true : false;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
nullable sets
  • 存在规则: X -> epsilon
  • 或者 : X -> Y1Y2...Yn, 且存在规则 Y1 -> epsilon, ..., Yn -> epsilon
  • 即 : X -*> epsilon (epsilon <- first(X))
nullable = {};\n\nwhile (nullable is still changing) {\n  foreach (production p: X -> beta) {\n    if ((beta == epsilon) || (beta == Y1...Yn\n      && Y1 <- nullable && ... && Yn <- nullable)) {\n      nullable += X;\n    }\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
first sets

first(X) = {t | X -> talpha} U {epsilon | X->epsilon} :

  • first(t) = {t}
  • epsilon<-first(X): X -> epsilon or X -> A1...An, epsilon<-first(Ai)
  • first(alpha)<-first(X): X -> A1..Analpha, epsilon<-first(Ai)

first sets 不动点算法:

foreach (non_terminal N) {\n  first(N) = {};\n}\n\nwhile (some sets is changing) {\n  foreach (production p: N->beta1...beta_n) {\n    foreach (beta_i from beta1 up to beta_n) {\n      if (beta_i == a) {\n      // e.g N->abX: first(N) += {a}\n        first(N) += {a};\n        break;\n      } else if (beta_i == M) {\n        first(N) += first(M);\n        if (M is not in nullable) {\n          break;\n        } // else continue this loop to add first(beta_next) into first(N)\n      }\n    }\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
NonTerminalFirst Set
S{s, t, g, w}
N{s, t, g, w}
V{e, d}
follow sets

follow(X) = {t | S -*> beta X t epsilon} :

  • for X -> AB:
    • first(B) <- follow(A), follow(X) <- follow(B)
    • if B -*> epsilon: follow(X) <- follow(A)
  • A -> alpha X beta: first(beta) - {epsilon} <- follow(X)
  • $ <- follow(S)

follow sets 不动点算法:

foreach (non_terminal N) {\n  follow(N) = {};\n}\n\nwhile (some sets is changing) {\n  foreach (production p: N->beta1...beta_n) {\n\n        // temp: follow(left) <- follow(right)\n    temp = follow(N);\n\n    foreach (beta_i from beta_n down to beta1) {\n      if (beta_i == a) {\n        temp = {a};\n      } else if (beta_i == M) {\n        follow(M) += temp\n        temp = (M is not nullable) ? (first(M)) : (temp + first(M))\n      }\n    }\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
select sets
  • 当 N -> Y1...Yn 右边 Y 全为 nullable 时, select(p) += follow(N)

select sets 不动点算法:

foreach (production p) {\n  select(p) = {}\n}\n\ncalculate_select_set(production p: N->beta1...beta_n) {\n  foreach (beta_i from beta1 up to beta_n) {\n    if (beta_i == a) {\n      select(p) += {a};\n      break;\n    } else if (beta_i == M) {\n      select(p) += first(M);\n      if (M is not in nullable) {\n        break;\n      }\n    }\n  }\n\n  // all betas are in nullable (当前规则的所有右边符号都是可空集)\n  // 故, select(p) 必须包括 follow(M) (当推导出右边符号都为空时, first(p) 即为 follow(M))\n  if (i > n) {\n    first(N) += follow(N);\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
分析表
  • 结合 nullable sets 准确求出 first sets
  • 再利用 first sets 准确求出 follow sets
  • 再利用 first sets, 并结合 follow sets(全空集修正) 准确求出 分析表:
0: z -> d\n1: | X Y Z\n2: Y -> c\n3: |\n4: X -> Y\n5: | a\n
1
2
3
4
5
6

nullable = {X, Y}

XYZ
first{a, c}{c}{a, c, d}
follow{a, c, d}{a, c, d}{}
production012345
select{d}{a, c, d}{c}{a, c, d}{a, c, d}{a}

|Non\\Terminal|a|c|d| |Z|1|1|0, 1| |Y|3|2, 3|3| |X|4, 5|4|4|

数字为规则编号

解决冲突(分析表某项有多个编号)

通过文法重写消除左递归, 使文法适应 L(最左推导):

  • 改写成右递归文法
  • 规定优先级与结合性
  • 提取左公因式(Common Prefix)
E -> T+E\n    |T\n\nE -> TX\nX -> +E\n    |epsilon\n
1
2
3
4
5
6
消除直接左递归
S -> Salpha1\n    |Salpha2\n    ...\n    |Salpha_n\n    |beta1\n    |beta2\n    ...\n    |beta_m\n\nS -> beta1S'\n    |beta2S'\n    ...\n    |beta_nS'\nS'-> alpha1S'\n    |alpha2S'\n    ...\n    |alpha_nS'\n    |epsilon\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
消除间接左递归
  • 把文法 G 的所有非终结符按任一顺序排列, e.g A1, A2, …, An
  • 消除 Ai 规则中的直接左递归: 把形如 Ai→Ajγ 的产生式 改写成 Ai→δ1γ /δ2γ /…/δkγ(其中 Aj→δ1 /δ2 /…/δk 是关于的 Aj 全部规则)
  • 去掉多余的规则(不可达规则)
#include <iostream>\n#include <string>\n#include <fstream>\n\nusing namespace std;\n\nstruct WF {\n    string left; //定义产生式的左部\n    string right; //定义产生式的右部\n};\n\n/*\n * count: number of non-terminal symbols\n */\nvoid Removing(WF *p,char *q,int n,int count) {\n\n    int count1 = n;\n    int flag = 0;\n\n  // 判断第一个非终结符是否存在直接左递归 if(p[i].left[0]==q[0])\n    for (int i = 0; i < n; i++) {\n        if (p[i].left[0] == p[i].right[0]) {\n            flag++;\n        }\n    }\n\n  // 如果存在直接左递归则消除直接左递归\n    if (flag != 0)\n    {\n        for (int i = 0; i < n; i++) {\n            if (p[i].left[0] == q[0]) {\n                if (p[i].left[0] == p[i].right[0]) {\n                  string str;\n                    str = p[i].right.substr(1,int (p[i].right.length())); // 取右部第二位开始的字串赋给str\n\n                    // E->E+T => E'->+TE'\n                    string temp = p[i].left;\n                    string temp1 = "'";\n                    p[i].left = temp+temp1;\n                    p[i].right = str+p[i].left;\n                } else {\n                    // E->T => E->TE'\n                    string temp=p[i].left;\n                    string temp1="'";\n                    temp=temp+temp1;\n                    p[i].right=p[i].right+temp;\n                }\n            }\n        }\n\n        string str="'";\n        p[count1].left=p[0].left[0]+str;\n        p[count1].right="ε";\n    }\n\n\n  // 对每一个非终结符迭代\n    for ( int i = 0; i <= count; i++) {\n\n      // 对每一个小于 i 的非终结符\n        for (int j = 0; j < i; j++) {\n\n          // 对每一个产生式\n            for (int g = 0; g < n; g++) {\n\n              // i 非终结符与第 g 产生式左边第一个字母相等\n                if (q[i] == p[g].left[0]) {\n\n          // g 产生式右边产生式第一个符号与第 j 个非终结符相等\n                    if (p[g].right[0] == q[j]) {\n                        for (int h = 0; h < n*n; h++) {\n                            if (p[h].left[0] == q[j]\n                              && int (p[h].left.length()) == 1) {\n                                string str;\n                                str = p[g].right.substr(\n                                  1,\n                                  int (p[g].right.length ()));\n                                p[++count1].left = p[g].left;\n                                p[count1].right = p[h].right + str;\n                            }\n                        }\n\n                        p[g].left="";\n                        p[g].right="";\n                    }\n                }\n            }\n        }\n    }\n\n  // 去除间接递归产生式\n    for(int i = 0; i <= count; i++) {\n        flag = 0;\n\n        for (int j = 0; j < n*n; j++) {\n            if (p[j].left[0] == q[i]) {\n                if(p[j].left[0] == p[j].right[0]) {\n                    flag++;\n                }\n            }\n        }\n\n        if (flag != 0) {\n            for (int j = 0; j <= n*n; j++) {\n                if (p[j].left[0] == q[i]) {\n                    if (p[j].left[0] == p[j].right[0]) {\n                        string str;\n                        str = p[j].right.substr(1,int (p[j].right.length()));\n                        string temp = p[j].left;\n                        string temp1 = "'";\n                        p[j].left = temp + temp1;\n                        p[j].right = str + p[j].left;\n                    } else {\n                        string temp = p[j].left;\n                        string temp1 = "'";\n                        temp = temp + temp1;\n                        p[j].right = p[j].right + temp;\n                    }\n                }\n            }\n\n            string str = "'";\n            p[++count1].left = q[i] + str;\n            p[count1].right = "ε";\n        }\n    }\n}\n\nint Delete(WF *p,int n) {\n    return 0;\n}\n\nint main() {\n    ofstream OutFile("result.txt");\n\n    int i,\n      j,\n      flag = 0,\n      count = 1,\n      n;\n\n    cout<<"请输入文法产生式个数n:"<<endl;\n    cin>>n;\n    WF *p=new WF[50];\n    cout<<"请输入文法的个产生式:"<<endl;\n\n  // input productions\n    for (i = 0; i < n; i++) {\n        cin>>p[i].left;\n        cout<<"->"<<endl;\n        cin>>p[i].right;\n        cout<<endl;\n    }\n    cout<<endl;\n    OutFile<<"即输入的文法产生式为:"<<endl;\n\n    // cout<<"即输入的文法产生式为:"<<endl;\n    for (i = 0; i < n; i++) {\n        // cout<<p[i].left<<"-->"<<p[i].right<<endl;\n        OutFile<<p[i].left<<"-->"<<p[i].right<<endl;\n    }\n    OutFile<<"*********************"<<endl;\n\n    // cout<<"*********************"<<endl;\n    char q[20];    // 对产生式的非终结符排序并存取在字符数组q\n    q[0] = p[0].left[0]; // 把产生式的第一个非终结符存入q中\n\n  // 对非终结符排序并存取\n    for (i = 1; i < n; i++) {\n        flag = 0;\n        for (j = 0; j < i; j++) {\n      // 根据j<i循环避免重复非终结符因此由标志位判断\n            if(p[i].left==p[j].left) {\n              flag++;\n            }\n        }\n\n        if(flag==0) {\n          // 没有重复加入q数组中\n            q[count++]=p[i].left[0];\n        }\n    }\n    count--;\n\n  // 调用消除递归子函数\n    Removing(p,q,n,count);\n    // 删除无用产生式\n    Delete(p,n);\n\n    OutFile<<"消除递归后的文法产生式为:"<<endl;\n    // cout<<"消除递归后的文法产生式为:"<<endl;\n    for (i = 0; i <= count; i++) {\n        for (int j = 0; j <= n*n; j++) {\n            if ((p[j].left[0] == q[i]) && int (p[j].left.length ()) == 1) {\n                OutFile<<p[j].left<<"-->"<<p[j].right<<endl;\n        // cout<<p[j].left<<"-->"<<p[j].right<<endl;\n            } else {\n              continue;\n            }\n        }\n\n        for (j = 0; j <= n*n; j++) {\n            if((p[j].left[0] == q[i]) && int (p[j].left.length ()) == 2) {\n                OutFile<<p[j].left<<"-->"<<p [j].right<<endl;\n            // cout<<p[j].left<<"-->"<<p[j].right<<endl;\n            } else {\n              continue;\n            }\n        }\n    }\n    return 0;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
非 LL(1) 文法/语言
  • ambiguous grammar
  • left recursive grammar
  • not left factored grammar(未提取展开式的公因子)

自底向上分析

0: S -> E\n1: E -> E + T\n2: | T\n3: T -> T * F\n4: | F\n5: F -> n\n
1
2
3
4
5
6
2 + 3 * 4\n=> F + 3 * 4\n=> T + 3 * 4\n=> E + 3 * 4\n=> E + T * 4\n=> E + T * F\n=> E + T\n=> E\n=> S\n
1
2
3
4
5
6
7
8
9

最右推导(优先推导最右侧非终结符)逆过程

LR(0) 分析算法(移进-归约(reduce)算法)

  • 从左向右读入程序(left to right scan), 逆向最右推导(rightmost derivation), 不用前看符号
  • 添加伪开始符号: S' -> . S$ $表示 tokens/file 结束符
  • 移进 : 读入记号 push(token[i])
  • 归约(reduce): pop(right expansion) push(left expansion)
短语(Handles)
S -*> αXω -> αβω\n
1

β 是 αβω 的一个短语(Handle)

分析表构造

LR(0) 分析表构造算法: (原理同于 Hopcroft 算法)

  • E -> A, A -> B, B -> C ... : Recursively, right hand side of C production will be reduced to E finally
closure(production_set p) {\n  while (p is still changing) {\n    foreach (p's item i: A -> b . B ...) {\n      p += {B -> . y...}\n    }\n  }\n}\n\ngoto(production_set p, token x) {\n  temp = {}\n\n  foreach (p's item i: A -> b . x...) {\n    temp += {A -> bx . ...}\n  }\n\n  return closure(temp)\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
p0 = closure(S'' -> . S $)\n(production_with_dot_)set = {p0}\nQ = enqueue(p0)\n\nwhile (Q is not empty) {\n  p = dequeue(Q)\n\n  foreach (x <- NonTerminal||Terminal) {\n    q = goto(p, x)\n\n    if (x <- Terminal) {\n      ACTION[p, x] = q\n    } else {\n      GOTO[p, x] = q\n    }\n\n    if (q not <- set) {\n      set += {q}\n      enqueue(q)\n    }\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
驱动代码

LR(0) 驱动算法:

stack[];\npush($)\npush(state1)\n\nwhile (true) {\n  token t = nextToken()\n  state s = stack[top]\n\n  if (ACTION[s, t] == shift_i) {\n    push(t)\n    push(state_i)\n  } else if (ACTION[s, t] == reduce_j) {\n    // X is the left side of production j: X->beta\n    // beta is the right side of production j: X->beta\n\n    // pop up right side\n    pop(beta && bundle state variables)\n\n    // current state after pop up all bundle state(of beta)\n    state s = stack[top]\n\n    // push left side\n    push(X)\n\n    // transfer state after reduce\n    push(GOTO[s, X])\n  } else {\n    throw new SyntaxError();\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
解决冲突

SLR, LR(1), LALR, 采取与 first/follow/select sets 以及 前看符号 类似策略:

  • production_with_dot_set 中的 item 修改为 X -> [beta1 . beta_n..., a] 二元组
  • closure(production_set p) 中闭包规则从 X -> [a . Y beta,a] 修改为 Y -> [.y, b] b <- select(beta a)

LALR-K

LALR(k)

SLR

Simple LR: improves LR(k) shift/reduce heuristic

New reduce rule:

  • state contains item X -> β.
  • next_token <- follow(X)
SLR 实现
  • stack pair: <input, state>
  • state i: if has item X -> α.aβ , goto[i, a] = j then action[i, a] = shift j(shift then to state j)
  • state i: if has item X -> α. , a <- follow(X) then action[i, a] = reduce(X -> α)
  • state i: if has item S' -> S then action[i, $] = accept
  • otherwise: action[i, a] = error

抽象语法树

语法制导翻译(Syntax-Directed Translation)

在进行归约(reduce)的同时, 进行语义动作:

  • 给每条产生规则附加一个语义动作
exp : exp '+' exp { $$ = $1 + $3; }\n;\n
1
2
  • 在分析栈中压入 symbol, value, state (原本只压入 symbol, state)
push(right side symbol);\npush(right side value);\npush(next state);\n
1
2
3

抽象语法

  • 表达语法结构的内部表示, 作为前端(词法语法分析)和后端(代码生成)的中间件, tokens --语法分析器--> 抽象语法(树) --代码生成器--> 目标代码
  • 抽象语法无需考虑左/右递归, 左公因子提取, 分隔符等
// 具体语法\nE: E + T\n | T\n ;\nT: T * F\n | F\n ;\nF: n\n | (E)\n ;\n\n// 抽象语法\nE: n\n | E + E\n | E * E\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

AST 的实现

数据结构
E: n\n | E + E\n | E * E\n
1
2
3
enum kind {\n  E_INT,\n  E_ADD,\n  E_TIMES\n};\n\nstruct exp {\n  enum kind kind;\n};\n\nstruct exp_int {\n  enum kind kind;\n  int value;\n};\n\nstruct exp_add {\n  enum kind kind;\n  struct exp *left;\n  struct exp *right;\n};\n\nstruct exp_times {\n  enum kind kind;\n  struct exp *left;\n  struct exp *right;\n};\n\nstruct exp_int *new_exp_int(int value) {\n  struct exp_int *p = (struct exp_int *)malloc(sizeof(struct exp_int));\n  if (!p) throw new Error();\n  p->kind = E_INT;\n  p->value = value;\n  return p;\n}\n\nstruct exp_add *new_exp_add(exp *left, exp *right) {\n  struct exp_add *p = (struct exp_add *)malloc(sizeof(struct exp_add));\n  if (!p) throw new Error();\n  p->kind = E_ADD;\n  p->left = left;\n  p->right = right;\n  return p;\n}\n\nstruct exp_times *new_exp_times(exp *left, exp *right) {\n  struct exp_times *p = (struct exp_times *)malloc(sizeof(struct exp_times));\n  if (!p) throw new Error();\n  p->kind = E_TIMES;\n  p->left = left;\n  p->right = right;\n  return p;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
相关算法
int nodes_num(exp *e) {\n  switch (e->kind) {\n    case E_INT:\n      return 1;\n    case E_ADD: // fall through\n    case E_TIMES:\n      return 1 + nodes_num(e->left) + nodes_num(e->right);\n    default:\n      throw new SyntaxError("compile bug");\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
int pretty_print(exp *e) {\n  switch (e->kind) {\n    case E_INT:\n      printf("%d", e->value);\n      return 1;\n    case E_ADD:\n      printf("(");\n      pretty_print(e->left);\n      printf(")");\n      printf("+");\n      printf("(");\n      pretty_print(e->right);\n      printf(")");\n      return 1;\n    case E_TIMES:\n      printf("(");\n      pretty_print(e->left);\n      printf(")");\n      printf("*");\n      printf("(");\n      pretty_print(e->right);\n      printf(")");\n      return 1;\n    default:\n      throw new SyntaxError();\n      break;\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
构造算法

利用语法制导翻译, 在语法动作(action)/语法归约(reduce)中加入生成语法树的代码(自底(叶子)向上(根)构造函数)

E: E + E { $$ = new_exp_add($1, $3); }\n | E * E { $$ = new_exp_times($1, $3); }\n | n     { $$ = new_exp_int($1); }\n ;\n
1
2
3
4

Semantic Analysis(语义分析)

  • 声明检查(identifiers declaration)
  • 定义检查:
    • class 仅可定义一次
    • method 在同一 class 中仅可定义一次
  • 类型检查(types)
  • 作用域检查
  • 继承关系(inheritance relationships)
  • 上下文相关分析(检查抽象语法树上下文相关的属性)

AST + semantic of programming language --semantic analysis--> intermediate

e.g 变量/函数必须先声明再使用; 每个表达式必须有合适类型(左值/右值); 函数调用与函数定义保持一致(函数签名)

P: D S\n ;\n\nD: T id ';' D\n |\n ;\n\nT: int\n | bool\n ;\n\nS: id = E\n | printi (E)\n | printb (E)\n ;\n\nE: n\n | id\n | true\n | false\n | E + E\n | E && E\n ;\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

类型系统(type system)

Type Checking

├ e: T meas e 可计算为类型为 T 的值

Type Environments

  • Object(identifier) = Type
  • O Type environments 是一个函数, 将 object identifiers 映射成 types
  • O ├ e: T 表示在 O 函数作用下, 可证明 e 的类型为 T
// input x\n// output T\nO[T/x](x) = T\n\nO[T/x](y) = O(y)\n
1
2
3
4
5
[Var]\nO(x) = T\n----------\nO ├ x: T\n\n[Let without Init]\nO[T0/x] ├ e1: T1\n--------------------\nO ├ let x: T0 in e1: T1\n
1
2
3
4
5
6
7
8
9

Typing Methods

  • Method(ClassName, functionName) = (Type1, ..., Type_n, Type_n+1)
  • Type_n+1 为返回值的类型, 即方法自身的类型
[Dispatch]\nO,M ├ e0: T0\nO,M ├ e1: T1\n...\nO,M ├ en: Tn\nM(T0, func) = (T1', ..., Tn', Tn+1')\nTi <= Ti'\n----------\nO,M ├ e0.func(e1, ..., en): Tn+1'\n
1
2
3
4
5
6
7
8
9

符号表(上下文相关)

用来存储程序中变量的相关信息:

  • 类型: 字典结构 (key, type)
  • 作用域:
    • 进入作用域, 插入元素(插入哈希表首, 屏蔽外部同名变量); 离开作用域, 删除元素
    • 进入作用域, 压入新符号表; 离开作用域, 弹出栈顶符号表
  • 访问控制权
  • 命名空间: 变量, 标签, 形参, 标号 各自拥有一类符号表

可将符号表实现为:

  • 哈希表: 查找时间复杂度 O(1);
  • 红黑树: 查找时间复杂度 O(lg N);
#ifndef XX_SEMANTIC_TABLE_H\n#define XX_SEMANTIC_TABLE_H\n\ntypedef enum type type_t;\ntypedef char * key_t;    // typedef int hash_t; typedef hash_t key_t;\ntypedef struct value {\n  type_t type;\n  scope_t scope;\n} value_t;\n\ntypedef ... table_t;// 符号表数据结构\n\ntable_t new_table(void);\nint table_insert(table_t table, key_t id, value_t info);\nvalue_t table_search(table_t table, key_t id);\n\n#endif\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

类型检查

  • table: 字典结构 (key, type) (Hash Table/Red Black Tree)
  • type environment(Object, Methods, Class) is passed from parent to child(down the tree)
  • types are passed from child to parent(up the tree)
TypeCheck(Environment/OMC, e1+e2) = {\n    T1 = TypeCheck(OMC, e1);\n    T2 = TypeCheck(OMC, e2);\n    Check T1 == T2 == Int;\n    return Int;\n}\n\nTypeCheck(OMC, let x: T <- e0 in e1) = {\n    T0 = TypeCheck(OMC, e0);\n    T1 = TypeCheck(OMC.add(O(x)=T), e1);\n    Check subtype(T0, T1);  // T0 <= T1\n    return T1;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
enum type {INT, BOOL};\ntable_t table;// symbol table\n\n// dec_t, stm_t, exp_t: AST 中的结点\n\nenum type check_prog(dec_t d, stm_t s) {\n  // 生成符号表\n  table = check_dec(d);\n  // 根据符号表检查语句\n  return check_stm(s);\n}\n\n// 生成符号表\ntable_t check_dec(dec_t d){\n  foreach(T id <- d) {\n    table_insert(table, id, T);\n  }\n}\n\nenum type check_stm(table_t table, stm_t s) {\n  switch (s->kind) {\n    case STM_ASSIGN:\n      t1 = table_search(table, id);\n      t2 = check_exp(table, s->exp);\n      if (t1 != t2) {\n        throw new SemanticError("type mismatch");\n      } else {\n        return t1;\n      }\n    case STM_PRINTI:\n      t = check_exp(s->exp);\n      if (t != INT) {\n        throw new SemanticError("type mismatch");\n      } else {\n        return INT;\n      }\n    case STM_PRINTB:\n      t = check_exp(s->exp);\n      if (t != BOOL) {\n        throw new SemanticError("type mismatch");\n      } else {\n        return BOOL;\n      }\n  }\n}\n\nenum type check_exp(exp_t e) {\n  switch (e->kind) {\n    case EXP_INT:\n      return INT;\n    case EXP_ID:\n      t = table_search(table, id);// 查询符号表, 得到变量类型\n      if (id not exist) {\n        throw new SemanticError("id not found");\n      } else {\n        return t;\n      }\n    case EXP_TRUE:\n      return BOOL;\n    case EXP_FALSE:\n      return BOOL;\n    case EXP_ADD:\n      enum type t1 = check_exp_type(e->left);\n      enum type t2 = check_exp_type(e->right);\n      if (t1 != INT || t2 != INT) {\n        throw new SemanticError();\n        break;\n      } else {\n        return INT;\n      }\n    case EXP_AND:\n      enum type t1 = check_exp_type(e->left);\n      enum type t2 = check_exp_type(e->right);\n      if (t1 != BOOL || t2 != BOOL) {\n        throw new SemanticError();\n        break;\n      } else {\n        return BOOL;\n      }\n    default:\n      throw new SemanticError();\n      break;\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

作用域检查

table:

  • 进入作用域, 插入元素(插入哈希表首, 屏蔽外部同名变量); 离开作用域, 删除元素
  • 进入作用域, 压入新符号表; 离开作用域, 弹出栈顶符号表

类型相容性

  • 名字不同但结构相同的类型是否相等
  • 面向对象的继承类: is-a 关系 Parent parent = child;

错误诊断

  • 准确的错误信息: 出错位置等
  • 大量的错误信息
  • 一定的自纠功能

Immediate Representation

IR:

  • 树与有向无环图(DAG)
  • 三地址码(3-address code)
  • 控制流图(CFG)
  • 静态单赋值形式(SSA)
  • 连续传递风格(CPS)

三地址码

  • 原子表达式
  • 简单控制流 cjmp/jmp
  • 抽象的机器代码(伪代码)

控制流图

Block

  • block_t: { label_t; stm_list; jmp_t; }
  • 扫描三地址码, 生成 blocks
  • 图论算法:结点为 blocks, 边为跳转边

死基本块删除优化:删除遍历不到的语句块

数据流分析与程序重写

  • 根据数据流分析得到的信息, 对三地址码/控制流图进行重写
  • 后端的每一个阶段都可进行数据流分析

常量传播优化: 将赋值语句右端变量直接替换为常量, 减少访存

数据分析方法
到达定义分析

分析变量的哪些定义点可以到达变量的使用点处, 若可达定义唯一则可进行常量传播优化:

  • in set = prior out set
  • out set = self set + in set - kill set(重复定义点)
活性分析
  • 寄存器分配优化 活跃区间不相交的变量可共用一个寄存器
  • 并行优化 使用区间并行的计算可并行执行

代码优化

组织管理

Activation Record and Frame

AR:

用于管理过程活性(procedure activation)的信息:

  • result: 置于记录的顶层, 便于访问此结果
  • argument
  • return address
  • control link: 指向调用者(上级)

全局变量

不存于 AR 中, 存于静态数据段

堆区

new/malloc 得到的变量/对象不存于 AR 中, 存于堆区

优化类型

  • Local optimizations
  • Global optimizations
  • Inter-procedural optimizations

局部优化

  • 常量折叠优化: 所有代入常量的地方全部代入常量 1 + 2 => 3
  • 代数化简优化: a=1*b => a=b 2*a=>a<<1 (all tips from CSAPP)
  • 复制传播(copy propagation)优化: 利用前面计算出来的结果, 直接替换后面所有出现在右边的已计算左式(寄存器)

全局优化

Dead Code Elimination

  • CFG 中(控制流分析) 死代码块删除优化

Constant Propagation

CFG 中(数据流分析-可达定义分析) 常量传播(constant propagation)优化:

  • forwards analysis
  • C(stm, x, in) = value of x before stm ; C(stm, x, out) = value of x after stm
  • bottom < c < top => C(stm, x, in) = least_upper_bound{ C(prev_stm_i, x, out) }:
    • C(prev_stm, x, out) = top(nondeterministic) => C(stm, x, in) = top
    • C(prev_stm1, x, out) != C(prev_stm2, x, out) => C(stm, x, in) = top
    • C(prev_stm_i, x, out) = c/bottom(dead code) => C(stm, x, in) = c
  • C(stm, x, in) = bottom => C(stm, x, out) = bottom
  • C(x := c, x, out) = c
  • C(x := f(), x, out) = top
  • init: set entry to C = top, set anywhere else to C = bottom

Liveness Analysis

CFG 中 数据流分析-活性分析(liveness analysis), 可用于复制传播优化与寄存器分配优化:

  • backwards analysis
  • L(stm, x, out) = V { L(next_stm, x, in)}
  • L(... := f(x), x, in) = true
  • L(x := e, x, in) = false
  • L(none x, x, in) = L(none x, x, out)
  • init: L(...) = false

寄存器分配

Register Allocation and Graph Coloring:

  • 当 t1 与 t2 同时具有活性时, 不可共享寄存器; 反之, t1 与 t2 不同时具有活性, 可以共享寄存器
  • 当 t1 与 t2 同时具有活性时, 添加一条边连接 t1 与 t2, 构建 register interference graph(RIG)
  • colors number = registers number, k-colorable problem

Code Generation(代码生成)

为数据分配计算资源:

  • 数据: 全局变量, 局部变量, 动态分配变量
  • 资源: 寄存器(register), 数据区(.data, .bss), 代码区(.code), 栈区(runtime stack), 堆区(user heap)

当前局部变量应该放在寄存器还是内存区?

为代码选择计算指令(等价性):

  • 代码: 表达式/语句/函数代码
  • 指令: 算术/比较/跳转/调用/返回指令
P: D S\n ;\n\nD: T id ';' D\n |\n ;\n\nT: int\n | bool\n ;\n\nS: id = E\n | printi (E)\n | printb (E)\n ;\n\nE: n\n | id\n | true\n | false\n | E + E\n | E && E\n ;\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

递归下降代码生成算法

基于栈计算机

Mem + Stack + ALU

JVM(Java Virtual Machine)

s: push NUM\n | load x\n | store x\n | add\n | sub\n | times\n | div\n ;\n
1
2
3
4
5
6
7
8
gen_prog(dec_t d, stm_t s) {\n  gen_dec(d);\n  gen_stm(s);\n}\n\ngen_dec(T id; D) {\n  // stack_code(".int id")\n  gen_type(T);\n  emit(" id");\n\n  gen_dec(D);\n}\n\ngen_type(type_t t) {\n  switch(t-kind) {\n    case INT:// fall through\n    case BOOL:\n      emit(".int");\n      break;\n  }\n}\n\ngen_stm(stm_t s) {\n  switch (s->kind) {\n    STM_ASSIGN:\n      gen_exp(s->exp);\n      emit("store s->id");\n      break;\n    STM_PRINTI:\n      gen_exp(s->exp);\n      emit("printi");\n      break;\n    STM_PRINTB:\n      gen_exp(s->exp);\n      emit("printb");\n      break;\n  }\n}\n\ngen_exp(exp_t e) {\n  switch (e->kind) {\n    case EXP_INT:\n      emit("push e->value");// n\n      break;\n    case EXP_ID:\n      emit("load e->value");// id\n      break;\n    case EXP_BOOL:\n      emit("push e->value");// 1/0\n      break;\n    case EXP_ADD:\n      gen_exp(e->left);\n      gen_exp(e->right);\n      emit("add");\n      break;\n    case EXP_AND:\n      gen_exp(e->left);\n      gen_exp(e->right);\n      emit("and");\n      break;\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

基于寄存器计算机 (RISC)

Mem + Reg + ALU

MIPS ISA

// src -> dist\ns: mov_n n, r\n | mov r1, r2\n | load [x], r\n | store r, [x]\n | add r1, r2, r3\n | sub r1, r2, r3\n | times r1, r2, r3\n | div r1, r2, r3\n
1
2
3
4
5
6
7
8
9
void gen_prog(dec_t d, stm_t s) {\n  gen_dec(d);\n  gen_stm(s);\n}\n\nvoid gen_dec(T id; D) {\n  // reg_code(".int id")\n  // 为变量分配寄存器\n  gen_type(T);\n  emit(" id");\n\n  gen_dec(D);\n}\n\nvoid gen_type(type_t t) {\n  switch(t-kind) {\n    case INT:// fall through\n    case BOOL:\n      emit(".int");\n      break;\n  }\n}\n\nvoid gen_stm(stm_t s) {\n  switch (s->kind) {\n    STM_ASSIGN:\n      r = gen_exp(s->exp);\n      emit("mov r, e->id");\n      break;\n    STM_PRINTI:\n      r = gen_exp(s->exp);\n      emit("printi r");\n      break;\n    STM_PRINTB:\n      r = gen_exp(s->exp);\n      emit("printb r");\n      break;\n  }\n}\n\nreg_t gen_exp(exp_t e) {\n  switch (e->kind) {\n    case EXP_INT:\n      r = random_reg();\n      emit("mov_n e->value, r");// n\n      return r;\n    case EXP_ID:\n      r = random_reg();\n      emit("mov e->value, r");// id\n      return r;\n    case EXP_BOOL:\n      r = random_reg();\n      emit("mov_n e->value, r");// 1/0\n      return r;\n    case EXP_ADD:\n      r1 = gen_exp(e->left);\n      r2 = gen_exp(e->right);\n      r = random_reg();\n      emit("add r1, r2, r");\n      return r;\n    case EXP_AND:\n      r1 = gen_exp(e->left);\n      r2 = gen_exp(e->right);\n      r = random_reg();\n      emit("and r1, r2, r");\n      return r;\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

Garbage Collection

Mark and Sweep

  • mark phase: traces reachable objects (mark_bit |= 1)
let todo = {all roots}\nwhile todo != nil do\n    pick v <- todo\n    todo -= {v}\n    if mark(v) == 0 then\n        mark(v) |= 1\n        let v1, ..., vn be the pointers contained in v\n        todo += {v1, ..., vn}\n    fi\nod\n
1
2
3
4
5
6
7
8
9
10
  • sweep phase: collects garbage objects (mark_bit == 0)
p = bottom of heap\nwhile p < top of heap do\n    if mark(p) == 1 then\n        mark(p) = 0\n    else\n        add block p...(p + sizeof(p) - 1)  to freeList\n    fi\n    p += sizeof(p)\nod\n
1
2
3
4
5
6
7
8
9

Stop and Copy

Copy all reachable objects in old space to new space(reserved for GC):

  • Copied objects
  • Scanned objects: pointers have been restored

Compilers Exercise

C Declaration Interpreter

#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n#include <stdlib.h>\n\n#define MAX_TOKENS 100\n#define MAX_TOKEN_LEN 64\n\nenum type_tag {\n    IDENTIFIER,\n    QUALIFIER,\n    TYPE,\n};\n\nstruct token {\n    char type;\n    char string[MAX_TOKEN_LEN];\n};\n\nint top = -1;\nstruct token stack[MAX_TOKENS];\nstruct token ts;\n\n#define pop stack[top--]\n#define push(s) stack[++top] = s\n\nenum type_tag classify_string(void) {\n    char *s = ts.string;\n\n    if (!strcmp(s, "const")) {\n        strcpy(s, "read-only ");\n        return QUALIFIER;\n    }\n\n    if (!strcmp(s, "volatile")) return QUALIFIER;\n    if (!strcmp(s, "void")) return TYPE;\n    if (!strcmp(s, "char")) return TYPE;\n    if (!strcmp(s, "signed")) return TYPE;\n    if (!strcmp(s, "unsigned")) return TYPE;\n    if (!strcmp(s, "short")) return TYPE;\n    if (!strcmp(s, "int")) return TYPE;\n    if (!strcmp(s, "long")) return TYPE;\n    if (!strcmp(s, "float")) return TYPE;\n    if (!strcmp(s, "double")) return TYPE;\n    if (!strcmp(s, "struct")) return TYPE;\n    if (!strcmp(s, "union")) return TYPE;\n    if (!strcmp(s, "enum")) return TYPE;\n\n    return IDENTIFIER;\n}\n\nvoid get_token(void) {\n    char *p = ts.string;\n\n    /* 略过空白字符 */\n    while ((*p = getchar()) == ' ');\n\n    if (isalnum(*p)) {\n        /* 读入得标识符以a-Z,0-9开头 */\n        while (isalnum(*++p = getchar()));\n        ungetc(*p, stdin);\n        *p = '\\0';\n        ts.type = classify_string();\n        return;\n    }\n\n    if (*p == '*') {\n        strcpy(ts.string, "pointer to");\n        ts.type = '*';\n        return;\n    }\n\n    ts.string[1] = '\\0';\n    ts.type = *p;\n    return;\n}\n\nvoid read_to_first_identifer(void) {\n    get_token();\n\n  // read til identifier\n    while (ts.type != IDENTIFIER) {\n        push(ts);\n        get_token();\n    }\n\n    printf("%s is ", ts.string);\n    get_token();\n}\n\nvoid deal_with_arrays(void) {\n    while (ts.type == '[') {\n        printf("array ");\n        get_token();\n\n    /* 数字或']' */\n        if (isdigit(ts.string[0])) {\n            printf("0..%d ", atoi(ts.string) - 1);\n            get_token();\n        }\n\n        get_token();\n        printf("of ");\n    }\n}\n\nvoid deal_with_pointers(void) {\n    while (stack[top].type == '*')\n    {\n        printf("%s ", pop.string);\n    }\n}\n\nvoid deal_with_function_args(void) {\n    while (ts.type != ')') {\n        get_token();\n    }\n\n    get_token();\n    printf("function returning ");\n}\n\nvoid deal_with_declarator(void) {\n    /* 处理标识符之后可能存在的数组/函数 */\n\n    switch (ts.type) {\n      case '[':\n          deal_with_arrays();\n          break;\n      case '(':\n          deal_with_function_args();\n          break;\n    }\n\n    deal_with_pointers();\n\n    /* 处理在读入到标识符之前压入到堆栈中的符号 */\n    while (top >= 0) {\n        if (stack[top].type == '(') {\n            pop;\n            get_token();  //读取')'之后的符号\n            deal_with_declarator();\n        } else {\n            printf("%s", pop.string);\n        }\n    }\n}\n\nint main(void) {\n    /* 将标记压入堆栈中, 直到遇见标识符 */\n    read_to_first_identifer();\n    deal_with_declarator();\n    printf("\\n");\n\n    system("pause");\n    return 0;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

Cool Language

Classroom Object-Oriented Language:

Parser Implementation

// 返回下一个Token(只测试该Token,不向前移动Token List的offset指针)\nToken Peek(void);\n
1
2
// 消费下一个Token\nToken Next(void);\n
1
2
// void Expect(expectedToken)\nif (Peek() != expectedToken) {\n    Error("expect %s, but got %s\\n", expectedToken, Peek());\n}\n
1
2
3
4
// void Try(expectedToken)\nif (Peek() == expectedToken) {\n    Next(); // 消费之\n    return true;\n}\n\nreturn false;\n
1
2
3
4
5
6
7
',320);t.render=function(a,p,t,o,c,l){return n(),s("div",null,[e])};export default t;export{p as __pageData}; diff --git a/assets/computerScience_compilers_compilersBasicNotes.md.5ebb309b.lean.js b/assets/computerScience_compilers_compilersBasicNotes.md.5ebb309b.lean.js new file mode 100644 index 00000000000..4f2e9b02437 --- /dev/null +++ b/assets/computerScience_compilers_compilersBasicNotes.md.5ebb309b.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"Compiler Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Concepts","slug":"basic-concepts"},{"level":3,"title":"Definition of compilers","slug":"definition-of-compilers"},{"level":3,"title":"Structure of compilers","slug":"structure-of-compilers"},{"level":2,"title":"文法与语言","slug":"文法与语言"},{"level":3,"title":"符号与符号串","slug":"符号与符号串"},{"level":3,"title":"文法与语言的形式化表示","slug":"文法与语言的形式化表示"},{"level":3,"title":"乔姆斯基文法体系","slug":"乔姆斯基文法体系"},{"level":3,"title":"上下文无关文法","slug":"上下文无关文法"},{"level":3,"title":"正规文法","slug":"正规文法"},{"level":3,"title":"正则语言(Regular Expressions)","slug":"正则语言-regular-expressions"},{"level":3,"title":"分析树","slug":"分析树"},{"level":3,"title":"文法规则","slug":"文法规则"},{"level":2,"title":"Lexical Analysis","slug":"lexical-analysis"},{"level":3,"title":"Tokenizer - 词法分析器","slug":"tokenizer-词法分析器"},{"level":3,"title":"有限状态自动机(Finite Automaton)","slug":"有限状态自动机-finite-automaton"},{"level":3,"title":"自动词法分析器","slug":"自动词法分析器"},{"level":2,"title":"Syntax Analysis(语法分析)","slug":"syntax-analysis-语法分析"},{"level":3,"title":"自顶向下分析","slug":"自顶向下分析"},{"level":3,"title":"自底向上分析","slug":"自底向上分析"},{"level":3,"title":"抽象语法树","slug":"抽象语法树"},{"level":2,"title":"Semantic Analysis(语义分析)","slug":"semantic-analysis-语义分析"},{"level":3,"title":"类型系统(type system)","slug":"类型系统-type-system"},{"level":3,"title":"符号表(上下文相关)","slug":"符号表-上下文相关"},{"level":3,"title":"类型检查","slug":"类型检查"},{"level":3,"title":"作用域检查","slug":"作用域检查"},{"level":3,"title":"类型相容性","slug":"类型相容性"},{"level":3,"title":"错误诊断","slug":"错误诊断"},{"level":2,"title":"Immediate Representation","slug":"immediate-representation"},{"level":3,"title":"三地址码","slug":"三地址码"},{"level":3,"title":"控制流图","slug":"控制流图"},{"level":2,"title":"代码优化","slug":"代码优化"},{"level":3,"title":"组织管理","slug":"组织管理"},{"level":3,"title":"优化类型","slug":"优化类型"},{"level":3,"title":"局部优化","slug":"局部优化"},{"level":3,"title":"全局优化","slug":"全局优化"},{"level":2,"title":"Code Generation(代码生成)","slug":"code-generation-代码生成"},{"level":3,"title":"递归下降代码生成算法","slug":"递归下降代码生成算法"},{"level":2,"title":"Garbage Collection","slug":"garbage-collection"},{"level":3,"title":"Mark and Sweep","slug":"mark-and-sweep"},{"level":3,"title":"Stop and Copy","slug":"stop-and-copy"},{"level":2,"title":"Compilers Exercise","slug":"compilers-exercise"},{"level":3,"title":"C Declaration Interpreter","slug":"c-declaration-interpreter"},{"level":3,"title":"Cool Language","slug":"cool-language"},{"level":3,"title":"Parser Implementation","slug":"parser-implementation"}],"relativePath":"computerScience/compilers/compilersBasicNotes.md","lastUpdated":1627194884000}',t={},e=a('',320);t.render=function(a,p,t,o,c,l){return n(),s("div",null,[e])};export default t;export{p as __pageData}; diff --git a/assets/computerScience_database_databaseBasicNotes.md.aff8f197.js b/assets/computerScience_database_databaseBasicNotes.md.aff8f197.js new file mode 100644 index 00000000000..69ee0f03c6a --- /dev/null +++ b/assets/computerScience_database_databaseBasicNotes.md.aff8f197.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const e='{"title":"Database Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Concepts","slug":"basic-concepts"},{"level":3,"title":"Feature","slug":"feature"},{"level":3,"title":"Common Words","slug":"common-words"},{"level":2,"title":"Data Format","slug":"data-format"},{"level":3,"title":"XML","slug":"xml"},{"level":3,"title":"JSON","slug":"json"},{"level":2,"title":"Relational Algebra","slug":"relational-algebra"},{"level":3,"title":"Operators","slug":"operators"},{"level":2,"title":"Higher-Level Database Design Models","slug":"higher-level-database-design-models"},{"level":3,"title":"UML","slug":"uml"},{"level":3,"title":"Entity Relationship Model","slug":"entity-relationship-model"},{"level":2,"title":"SQL","slug":"sql"},{"level":2,"title":"Relational Design","slug":"relational-design"},{"level":3,"title":"Decomposition","slug":"decomposition"},{"level":3,"title":"functional dependencies","slug":"functional-dependencies"},{"level":3,"title":"BCNF","slug":"bcnf"},{"level":3,"title":"Multi Valued Dependencies","slug":"multi-valued-dependencies"},{"level":3,"title":"4NF","slug":"_4nf"},{"level":3,"title":"Normalized Design","slug":"normalized-design"},{"level":2,"title":"Indexes","slug":"indexes"},{"level":2,"title":"Transactions","slug":"transactions"},{"level":3,"title":"Transaction Standard","slug":"transaction-standard"},{"level":3,"title":"ACID Properties","slug":"acid-properties"},{"level":2,"title":"Integrity Constraints","slug":"integrity-constraints"},{"level":2,"title":"Triggers (DBMS Level Constraints)","slug":"triggers-dbms-level-constraints"},{"level":2,"title":"Views","slug":"views"},{"level":3,"title":"Modifications on Views","slug":"modifications-on-views"},{"level":2,"title":"MongoDB Basic Notes","slug":"mongodb-basic-notes"},{"level":3,"title":"MongoDB Set Up","slug":"mongodb-set-up"},{"level":3,"title":"Shell Instruction","slug":"shell-instruction"}],"relativePath":"computerScience/database/databaseBasicNotes.md","lastUpdated":1627194884000}',p={},t=a('

Database Basic Notes

Basic Concepts

Feature

  • Massive
  • Persistent
  • Safe
  • Multi-user
  • Convenient
  • Efficient
  • Reliable

Common Words

  • create/drop(from)/insert into/delete from/update
  • restricts
  • sub-queries
  • views(shorthand for queries)
  • left/right join on ...
  • primary/foreign key
  • references

(id, name, birth, major, grade) is not normalized, because grade is not relevant to student id (id, name, birth) + (id, major, grade) is normalized (name, os, lang) is not normalized, because os isn't relevant to lang (name, os) + (name, lang) is normalized Data-intensive applications may not use DBMS/Query Language at all e.g Hadoop, all operations on data stores in files

Data Format

XML

  • single root element
  • matched tags with proper nesting
  • unique attributes within elements

DTD

Document Type Definition:

  • similar grammar to regular expression(*?)
  • ID/IDRef should be unique
  • CDATA: character data
  • (#PCDATA): parsed character data (plain text between tags)
<?xml version="1.0" encoding="utf-8"?>\n\n<!DOCTYPE Bookstore [\n    <!ELEMENT Bookstore (Book)*>\n    <!ELEMENT Book (Title, Authors, Remark?)>\n    <!ATTLIST Book  ISBN ID #REQUIRED\n                    Price CDATA #REQUIRED\n                    Authors IDREFS #REQUIRED>\n    <!ELEMENT Title (#PCDATA)>\n    <!ELEMENT Remark (#PCDATA | BookRef)*>\n    <!ELEMENT BookRef EMPTY>\n    <!ATTLIST BookRef book IDREF #REQUIRED>\n    <!ELEMENT Authors (Author)*>\n    <!ELEMENT Author (First_Name, Last_Name)>\n    <!ATTLIST Author Ident ID #REQUIRED>\n    <!ELEMENT First_Name (#PCDATA)>\n    <!ELEMENT Last_Name (#PCDATA)>\n]>\n\n<Bookstore>\n    <Book ISBN="ISBN-0-23-23333-233" Price="233" Authors="Sabertazimi">\n        <Title>Kind of sword</Title>\n        <Authors>\n            <Author Ident="Sabertazimi">\n                <First_Name>Yilong</First_Name>\n                <Last_Name>Liu</Last_Name>\n            </Author>\n        </Authors>\n    </Book>\n</Bookstore>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

Tools: XML Copy Editor, XML Linter:

xmllint --valid --noout Bookstore.xml\n
1

XSD

XML Schema Definition:

xmllint -schema Bookstore.xsd -noout Bookstore.xml\n
1

JSON

JavaScript Object Notation:

  • serializing data objects in files
  • human-readable data
  • semi-structured data
  • number/boolean/string/array/object(empty or key-value pair) recursive constructs

Relational Algebra

Operators

  • select operator σ(sigma): σ(sID < 100 ^ sAge > 20)Table_Name set constraints
  • project operator π(pi) : π(sID, GPA)Table_Name select certain columns
  • cross-product operator x: Table1 x Table2, m tuples(rows) x n tuples(rows) => m*n tuples(rows)
  • natural join operator ∞: σ(E1.A1 = E2.A1 ^ E1.A2 = E2.A2 ...) (E1 x E2)
  • theta join operator ∞(condition): σ(condition) (E1 x E2), call condition as ϴ
  • difference operator -: matching schemas => change rows/tuples
  • union/intersection operator ∪ / ∩: matching schemas => change rows/tuples
  • rename operator ρ: change schemas(attributes name), different schemas <=> same schemas (union/intersection/self-join)
  • assign statement :=
  • tree notation
π(sID, GPA) (σ(sID < 100 ^ GPA > 3.7) Student)\n
1

Higher-Level Database Design Models

Higher-Level Database Design Models -Translator-> Relational implemented by RDBMS

UML

Unified Modeling Language: PlantUML

Classes

for data modeling:

  • add PK(primary key)
  • drop methods
-----------\n| student |\n|---------|\n|sID   PK |\n|sName    |\n|GPA      |\n|---------|\n|<methods>|\n-----------\n
1
2
3
4
5
6
7
8
9

Associations

relationships between objects of 2 classes):

  • one to one: 1..1 --- 1..1
  • many to one: * --- 1..1
  • one to many:1..1 --- *
  • many to many: * --- *
-----------                   ---------\n| student |                   |college|\n|---------|                   |       |\n|sID   PK |x..y   Apply   m..n|       |\n|sName    |-------------------|       |\n|GPA      |                   |       |\n|---------|                   |       |\n|<methods>|                   |       |\n-----------                   ---------\n
1
2
3
4
5
6
7
8
9

Associations Classes

  • classes store information of relationship edge between 2 data classes
  • unnecessary if 0..1 or 1..1
c1 * --- 1..1 c2\ninformation of relationship edge can stored in c1\nowing to every object of c1 only associated with 1 object of c2\n
1
2
3

Subclasses

children classes

Composition and Aggregation

Entity Relationship Model

SQL

  • select ... from ... where
  • insert into ... ...
  • delete from ... where ...
  • update ... set ... = ... where ...
DROP VIEW IF EXISTS Standings;\nDROP VIEW IF EXISTS Count;\nDROP VIEW IF EXISTS Wins;\nDROP TABLE IF EXISTS Matches;\nDROP TABLE IF EXISTS Players;\n\n-- Players Table\nCREATE TABLE Players (\n  id SERIAL primary key,\n  name varchar(255)\n);\n\n-- Matches Table\nCREATE TABLE Matches (\n  id SERIAL primary key,\n  player int references Players(id),\n  opponent int references Players(id),\n  result int\n);\n\n-- Wins View shows number of wins for each Player\nCREATE VIEW Wins AS\n  SELECT Players.id, COUNT(Matches.opponent) AS n\n  FROM Players\n  LEFT JOIN (SELECT * FROM Matches WHERE result>0) as Matches\n  ON Players.id = Matches.player\n  GROUP BY Players.id;\n\n-- Count View shows number of matches for each Player\nCREATE VIEW Count AS\n  SELECT Players.id, Count(Matches.opponent) AS n\n  FROM Players\n  LEFT JOIN Matches\n  ON Players.id = Matches.player\n  GROUP BY Players.id;\n\n-- Standings View shows number of wins and matches for each Player\nCREATE VIEW Standings AS\n  SELECT Players.id,Players.name,Wins.n as wins,Count.n as matches\n  FROM Players,Count,Wins\n  WHERE Players.id = Wins.id and Wins.id = Count.id;\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Relational Design

Decomposition

  • start with mega-relations: including all attributes
  • decompose into smaller relations(BCNF/4NF)

functional dependencies

  • A -> B => 1-1/n-1 mapping
  • key sets: closure of sets contains all attributes

assuming relation R(A, B, C, D, ..., G) and closure of A, B {A, B} + A->C->D, B->E->F, F->G => {A, B}+ = {A, B, C, ..., G} then, {A, B} is a key if there no exists such closure, then treat all-attributes as a key

BCNF

boyce-codd normal form:

  • for each A -> B having A is super key && B isn't key
  • not exists A -> B -> C
  • here's the algorithm:
/*\n * @brief fixed point algorithm just like most algorithms from compiler\n *\n * by decomposing to transform non-key dependent attributes to key dependent attributes\n */\n\ncompute FDs for R\ncompute key for R using its FDs\n\nwhile (there is relation R' aren't in BCNF) {\n    pick any R' with A -> B that violates BCNF (A is not its key)\n    decompose R' into R1(A, B) and R2(A, rest)\n    compute FDs for R1 and R2\n    compute keys for R1 and R2 using their FDs\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Multi Valued Dependencies

  • A -> B && rest attributes => A ->> B
  • A ->> B(1-n mapping), A ->> C(1-n mapping), no B -> C/C ->> B, B * C redundant tuples/rows
  • A ->>B && A ->>C => A ->> B∩C
  • A ->>B && B ->>C => A ->> C-B

4NF

Forth normal form:

  • if A ->> B then A is key && B isn't key
  • here's the algorithm:
/*\n * @brief fixed point algorithm just like most algorithms from compiler\n *\n * by decomposing to transform non-key dependent attributes to key dependent attributes\n */\n\ncompute FDs and MVDs for R\ncompute key for R using its FDs\n\nwhile (there is relation R' aren't in 4NF) {\n    pick any R' with A ->> B that violates 4NF(A is not its key)\n    decompose R' into R1(A, B) and R2(A, rest)\n    compute FDs and MVDs for R1 and R2\n    compute keys for R1 and R2 using their FDs\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Normalized Design

  • every row has the same number of columns
  • every row has a unique key(PRIMARY KEY)
  • everything in a row is all relevant to unique key
  • everything in a row is all relevant to each other

Indexes

  • primary mechanism to improve performance of database
  • persist data structures stored in database (hash tables/B trees/B+ trees)
  • trade off: scale of database and workload(query/update rate) as input of physical design advisors
CREATE INDEX IndexName on T(A)\nCREATE INDEX IndexName on T(A1, A2, ..., An)\nCREATE UNIQUE INDEX IndexName on T(A)\nDROP INDEX IndexName\n
1
2
3
4

Transactions

  • a sequence of one/more SQL operations treated as a unit
  • target: concurrency and failures recovery

Transaction Standard

  • all or nothing(atomicity)
  • transaction begins automatically on first SQL statement
  • on "commit": old transaction ends, new one begins
  • on session termination: current transaction ends
  • "AutoCommit" turns each statement into transaction

ACID Properties

  • Atomicity(Logging)
  • Consistency
  • Isolation: guarantee serializability(Locking)
  • Durability(Logging)

Isolation Level

weaker isolation level: read uncommitted < read committed < repeatable read < serializable

  • increased concurrency + decreased overhead = increased performance
  • weaker consistency guarantees
  • some system default: repeatable read
SET TRANSACTION READ ONLY;\nSET TRANSACTION ISOLATION LEVEL REPEATABLE READ;\n
1
2

Integrity Constraints

CREATE TABLE TableName (\n    ... PRIMARY KEY,\n    ... UNIQUE,\n    ... CHECK (Condition),\n    ... references TableName(ForeignKey),\n    ... references TableName(ForeignKey) ON DELETE/UPDATE RESTRICT/SET NULL/CASCADE,\n    ... ,\n    PRIMARY KEY (Attr1, Attr2, ...),\n    UNIQUE (Attr1, Attr2, ...),\n    CHECK (Condition),\n    FOREIGN KEY (Attr1, Attr2, ...) references\n      TableName(Bttr1, Bttr2, ...) [ ON ... (default RESTRICT) ]\n);\n\nCREATE ASSERTION AssertionName\nCHECK (Condition);\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Triggers (DBMS Level Constraints)

CREATE TRIGGER TriggerName\nBEFORE|AFTER|INSTEAD OF Events(INSERT/UPDATE OF/DELETE ON TableName)\n[ referencing-variables ]\n[ FOR EACH ROW ]\nWHEN ( Condition )\n[ BEGIN ]\nAction\n[ END ];\n\nCREATE TRIGGER Cascade\nAfter DELETE ON S\nREFERENCING OLD ROW AS O\nFOR EACH ROW\nDELETE FROM R WHERE A = O.B (R.A = S.B)\n\nCREATE TRIGGER Cascade\nAfter DELETE ON S\nREFERENCING OLD TABLE AS OT\nDELETE FROM R WHERE A IN (SELECT B FROM BT)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Views

  • logical layer: hiding data from users
  • modularity and reuse of query
VIEW ViewName = VIEWQUERY (R1, R2, ..., Rn)\n\nCREATE VIEW ViewName (T1, T2, ..., Tn) AS\n< Query >\n
1
2
3
4

Modifications on Views

owing to views are logical layer, it's senseless to modify data on views

Implements Modification with Triggers

CREATE TRIGGER TriggerName\nINSTEAD OF DELETE/UPDATE OF/INSERT ON ViewName\n[ referencing-variables ]\n[ FOR EACH ROW ]\nWHEN ( Condition )\n[ BEGIN ]\nAction\n[ END ];\n
1
2
3
4
5
6
7
8

SQL Standard - Updatable Views

  • SELECT (no DISTINCT) on single table T
  • no GROUP BY/HAVING or Aggregation
  • attributes can't be NULL/default values
  • sub-queries cant' refer to table T
CREATE VIEW\n...\nWITH CHECK OPTION;\n
1
2
3

MongoDB Basic Notes

MongoDB Set Up

MongoDB Installation

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927\necho "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse"\n\\ | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list\nsudo apt-get update\nsudo apt-get install -y mongodb-org mongodb-org-server\n\\ mongodb-org-shell mongodb-org-mongos mongodb-org-tools\n
1
2
3
4
5
6

MongoDB Not Upgrade

echo "mongodb-org hold" | sudo dpkg --set-selections\necho "mongodb-org-server hold" | sudo dpkg --set-selections\necho "mongodb-org-shell hold" | sudo dpkg --set-selections\necho "mongodb-org-mongos hold" | sudo dpkg --set-selections\necho "mongodb-org-tools hold" | sudo dpkg --set-selections\n
1
2
3
4
5

MongoDB Control

sudo service mongod start\nsudo service mongod stop\nsudo service mongod restart\n
1
2
3

MongoDB Uninstall

sudo service mongod stop\nsudo apt-get purge mongodb-org*\nsudo rm -r /var/log/mongodb\nsudo rm -r /var/lib/mongodb\n
1
2
3
4

Shell Instruction

create and drop

create
use test\nshow dbs\n
1
2
drop
use dbToDrop\ndb.dropDatabase()\n
1
2

query

db.collection.find().pretty()\n
1

insert

db.collection.insert();\n
1

information

database
db.getName()\ndb.stats()\ndb.version()\ndb.getMongo()\n
1
2
3
4
collection
db.getCollectionNames()\ndb.printCollectionStats()\n
1
2
',119);p.render=function(a,e,p,o,l,r){return n(),s("div",null,[t])};export default p;export{e as __pageData}; diff --git a/assets/computerScience_database_databaseBasicNotes.md.aff8f197.lean.js b/assets/computerScience_database_databaseBasicNotes.md.aff8f197.lean.js new file mode 100644 index 00000000000..c2c0510a936 --- /dev/null +++ b/assets/computerScience_database_databaseBasicNotes.md.aff8f197.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const e='{"title":"Database Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Concepts","slug":"basic-concepts"},{"level":3,"title":"Feature","slug":"feature"},{"level":3,"title":"Common Words","slug":"common-words"},{"level":2,"title":"Data Format","slug":"data-format"},{"level":3,"title":"XML","slug":"xml"},{"level":3,"title":"JSON","slug":"json"},{"level":2,"title":"Relational Algebra","slug":"relational-algebra"},{"level":3,"title":"Operators","slug":"operators"},{"level":2,"title":"Higher-Level Database Design Models","slug":"higher-level-database-design-models"},{"level":3,"title":"UML","slug":"uml"},{"level":3,"title":"Entity Relationship Model","slug":"entity-relationship-model"},{"level":2,"title":"SQL","slug":"sql"},{"level":2,"title":"Relational Design","slug":"relational-design"},{"level":3,"title":"Decomposition","slug":"decomposition"},{"level":3,"title":"functional dependencies","slug":"functional-dependencies"},{"level":3,"title":"BCNF","slug":"bcnf"},{"level":3,"title":"Multi Valued Dependencies","slug":"multi-valued-dependencies"},{"level":3,"title":"4NF","slug":"_4nf"},{"level":3,"title":"Normalized Design","slug":"normalized-design"},{"level":2,"title":"Indexes","slug":"indexes"},{"level":2,"title":"Transactions","slug":"transactions"},{"level":3,"title":"Transaction Standard","slug":"transaction-standard"},{"level":3,"title":"ACID Properties","slug":"acid-properties"},{"level":2,"title":"Integrity Constraints","slug":"integrity-constraints"},{"level":2,"title":"Triggers (DBMS Level Constraints)","slug":"triggers-dbms-level-constraints"},{"level":2,"title":"Views","slug":"views"},{"level":3,"title":"Modifications on Views","slug":"modifications-on-views"},{"level":2,"title":"MongoDB Basic Notes","slug":"mongodb-basic-notes"},{"level":3,"title":"MongoDB Set Up","slug":"mongodb-set-up"},{"level":3,"title":"Shell Instruction","slug":"shell-instruction"}],"relativePath":"computerScience/database/databaseBasicNotes.md","lastUpdated":1627194884000}',p={},t=a('',119);p.render=function(a,e,p,o,l,r){return n(),s("div",null,[t])};export default p;export{e as __pageData}; diff --git a/assets/computerScience_latex_latexBasicNotes.md.d95b2977.js b/assets/computerScience_latex_latexBasicNotes.md.d95b2977.js new file mode 100644 index 00000000000..ff30cd7bafd --- /dev/null +++ b/assets/computerScience_latex_latexBasicNotes.md.d95b2977.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const t='{"title":"Latex Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Workflow","slug":"workflow"},{"level":3,"title":"Installation","slug":"installation"},{"level":3,"title":"Build Script","slug":"build-script"},{"level":2,"title":"Basis","slug":"basis"},{"level":3,"title":"Layout","slug":"layout"},{"level":2,"title":"Math","slug":"math"},{"level":3,"title":"Symbol","slug":"symbol"},{"level":2,"title":"template","slug":"template"},{"level":2,"title":"Tools","slug":"tools"},{"level":3,"title":"PDF to Tex","slug":"pdf-to-tex"},{"level":3,"title":"tlmgr","slug":"tlmgr"}],"relativePath":"computerScience/latex/latexBasicNotes.md","lastUpdated":1627194884000}',p={},e=a('

Latex Basic Notes

Workflow

Installation

sudo apt-get install libdigest-perl-md5-perl perl-tk\nwget http://mirrors.ustc.edu.cn/CTAN/systems/texlive/Images/texlive2018.iso\nsudo mount -o loop texlive2018.iso /mnt/iso\ncd /mnt/iso\nsudo ./install-tl -gui perltk\nsudo umount /mnt/iso\n\nexport MANPATH=${MANPATH}:/usr/local/texlive/2018/texmf-dist/doc/man\nexport INFOPATH=${INFOPATH}:/usr/local/texlive/2018/texmf-dist/doc/info\nexport PATH=${PATH}:/usr/local/texlive/2018/bin/x86_64-linux\n
1
2
3
4
5
6
7
8
9
10
wget https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet/install-tl-unx.tar.gz\ntar -xzf install-tl-unx.tar.gz\ncd install-tl-201*\nsudo apt-get install perl-tk perl-doc\nsudo ./install-tl -gui -repository https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet/\n\nsudo cp /usr/local/texlive/2018/texmf-var/fonts/conf/texlive-fontconfig.conf /etc/fonts/conf.d/09-texlive.conf\nsudo fc-cache -fv\n\nsudo tlmgr option repository https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet/\nsudo tlmgr update --list\nsudo tlmgr update --self --all\n
1
2
3
4
5
6
7
8
9
10
11
12

Build Script

filename=main\n\npdf:\n  pdflatex -synctex=1 -shell-escape -interaction=nonstopmode ${filename}.tex\n  bibtex ${filename}\n  pdflatex -synctex=1 -shell-escape -interaction=nonstopmode ${filename}.tex\n  pdflatex -synctex=1 -shell-escape -interaction=nonstopmode ${filename}.tex\n\nclean:\n  rm -f ${filename}.{ps,pdf,log,aux,out,dvi,bbl,blg,synctex.gz,toc,bcf,run.xml,tex.bbl,tex.blg}\n  rm -f ${filename}-blx.bib\n
1
2
3
4
5
6
7
8
9
10
11

Basis

Layout

\\footnote{}\n\\emph{}\n\\raggedright\n\\centering\n\\raggedleft\n
1
2
3
4
5

Page Size

\\usepackage[left=1cm,right=1cm,top=2cm,bottom=3cm]{geometry}\n
1

Font Size

\\zihao{0}  % 初号\n\\zihao{-6} % 小六\n
1
2

Math

Typeset Equations

Symbol

$\\gamma \\delta \\sigma \\phi \\psi \\rho \\mu \\pi$\n$\\sim \\in$\n$\\geq \\leq$\n$\\infty$\n$\\rightarrow$\n$\\surd \\sqrt{2} \\frac{x}{y}$\n
1
2
3
4
5
6

template

\\usepackage{mathbbold}\n\\usepackage{CJK,CJKnumb}\n\\usepackage{indentfirst}        %首行缩进宏包\n\\usepackage{latexsym,bm}        % 处理数学公式中和黑斜体的宏包\n\\usepackage{amsmath,amssymb}    % AMSLaTeX宏包 用来排出更加漂亮的公式\n\\usepackage{graphicx}\n\\usepackage{cases}\n\\usepackage{pifont}\n\\usepackage{txfonts}\n\n%%%%%%%%%%% CJK下设置中文字体 %%%%%%%%%%%%%\n\\newcommand{\\song}{\\CJKfamily{song}}    % 宋体   (Windows自带simsun.ttf)\n\\newcommand{\\fs}{\\CJKfamily{fs}}        % 仿宋体 (Windows自带simfs.ttf)\n\\newcommand{\\kai}{\\CJKfamily{kai}}      % 楷体   (Windows自带simkai.ttf)\n\\newcommand{\\hei}{\\CJKfamily{hei}}      % 黑体   (Windows自带simhei.ttf)\n\\newcommand{\\li}{\\CJKfamily{li}}        % 隶书   (Windows自带simli.ttf)\n\n%%%%%%%%%%%  设置字体大小 %%%%%%%%%%%%%\n\\newcommand{\\chuhao}{\\fontsize{42pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\xiaochuhao}{\\fontsize{36pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\yihao}{\\fontsize{28pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\erhao}{\\fontsize{21pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\xiaoerhao}{\\fontsize{18pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\sanhao}{\\fontsize{15.75pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\sihao}{\\fontsize{14pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\xiaosihao}{\\fontsize{12pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\wuhao}{\\fontsize{10.5pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\xiaowuhao}{\\fontsize{9pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\liuhao}{\\fontsize{7.875pt}{\\baselineskip}\\selectfont}\n\\newcommand{\\qihao}{\\fontsize{5.25pt}{\\baselineskip}\\selectfont}\n\n%%%%%%%% 设置版心 %%%%%%%%%%%%%\n\\setlength{\\textwidth}{14cm}\n\\setlength{\\textheight}{20cm}\n\\setlength{\\hoffset}{0cm}\n\\setlength{\\voffset}{0cm}\n\\setlength{\\parindent}{2em}                 % 首行两个汉字的缩进量\n\\setlength{\\parskip}{3pt plus1pt minus1pt} % 段落之间的竖直距离\n\\renewcommand{\\baselinestretch}{1.2}        % 定义行距\n\\setlength{\\abovedisplayskip}{2pt plus1pt minus1pt}     %公式前的距离\n\\setlength{\\belowdisplayskip}{6pt plus1pt minus1pt}     %公式后面的距离\n\\setlength{\\arraycolsep}{2pt}   %在一个array中列之间的空白长度, 因为原来的太宽了\n\n\\allowdisplaybreaks[4] % \\eqnarray如果很长,影响分栏、换行和分页\n                        %(整块挪动,造成页面空白),可以设置成为自动调整模式\n\n\n\\CJKtilde   %用于解决英文字母和汉字的间距问题。例如:变量~$x$~的值。\n\\renewcommand{\\CJKglue}{\\hskip 0pt plus 0.08\\baselineskip}\n%它于必要时在汉字之间插入一个附加的空隙,以解决行的超长问题。\n\n%\\numberwithin{equation}{section}\n\n%================= 一些自定义命令 =============%\n\\newcommand{\\R}{\\ensuremath{\\mathbb{R}}}\n\\newcommand{\\p}{\\partial}\n\\newcommand{\\g}{\\gamma}\n%=================== End ======================%\n\n\\usepackage{fancyhdr}\n\\pagestyle{fancy}\n\\renewcommand{\\headrulewidth}{0.6pt}\n%\\addtolength{\\parinddent}{2em plus 1em minus 1em]\n\n%===============标题名称中文化 ===================%\n\\renewcommand\\abstractname{\\hei\\ }\n\\renewcommand\\refname{\\hei 参考文献}\n\\renewcommand\\figurename{\\hei}\n\\renewcommand\\tablename{\\hei}\n\\newtheorem{dingyi}{\\hei 定义~}[section]\n\\newtheorem{dingli}{\\hei 定理~}[section]\n\\newtheorem{yinli}[dingli]{\\hei 引理~}\n\\newtheorem{tuilun}[dingli]{\\hei 推论~}\n\\newtheorem{mingti}[dingli]{\\hei 命题~}\n\n%% 注:在印刷出版上,中文字号制与点数制的对照关系如下:\n%% 1770年法国人狄道(F.A.Didot)制定点数制,规定1法寸为72点,即:1点=0.3759毫米。\n%% 狄道点数制在法国、德国、奥地利、比利时、丹麦、匈牙利等国比较流行。\n%% 1886年全美活字铸造协会以派卡(pica)为基准制定派卡点数制,规定1pica=12point(点),即:\n%% 1点=0.013837英寸=0.35146毫米\n%% 20世纪初派卡点数制传入我国,并得到逐步推广。在实用中对常用点数以号数命名而产生了号数制,\n%% 二者换算如下(以pt代表“点”):\n%% 初号……42pt   小初号……36pt    一号……28pt     二号……21pt    小二号……18pt   三号……15.75pt\n%% 四号……14pt   小四号……12pt    五号……10.5pt   小五号……9pt   六号 …… 7.875pt  七号 …… 5.25pt\n\n%%%%% xeCJK下设置中文字体 %%%%%%%%%%%\n\\setCJKfamilyfont{song}{SimSun}                                 %宋体 song\n\\newcommand{\\song}{\\CJKfamily{song}}\n\\setCJKfamilyfont{xs}{NSimSun}                            %新宋体 xs\n\\newcommand{\\xs}{\\CJKfamily{xs}}\n\\setCJKfamilyfont{fs}{FangSong_GB2312}        %仿宋2312 fs\n\\newcommand{\\fs}{\\CJKfamily{fs}}\n\\setCJKfamilyfont{kai}{KaiTi_GB2312}                        %楷体2312  kai\n\\newcommand{\\kai}{\\CJKfamily{kai}}\n\\setCJKfamilyfont{yh}{Microsoft YaHei}                    %微软雅黑 yh\n\\newcommand{\\yh}{\\CJKfamily{yh}}\n\\setCJKfamilyfont{hei}{SimHei}                                    %黑体  hei\n\\newcommand{\\hei}{\\CJKfamily{hei}}\n\\setCJKfamilyfont{msunicode}{Arial Unicode MS}            %Arial Unicode MS: msunicode\n\\newcommand{\\msunicode}{\\CJKfamily{msunicode}}\n\\setCJKfamilyfont{li}{LiSu}                                            %隶书  li\n\\newcommand{\\li}{\\CJKfamily{li}}\n\\setCJKfamilyfont{yy}{YouYuan}                             %幼圆  yy\n\\newcommand{\\yy}{\\CJKfamily{yy}}\n\\setCJKfamilyfont{xm}{MingLiU}                                        %细明体  xm\n\\newcommand{\\xm}{\\CJKfamily{xm}}\n\\setCJKfamilyfont{xxm}{PMingLiU}                             %新细明体  xxm\n\\newcommand{\\xxm}{\\CJKfamily{xxm}}\n\\setCJKfamilyfont{hwsong}{STSong}                            %华文宋体  hwsong\n\\newcommand{\\hwsong}{\\CJKfamily{hwsong}}\n\\setCJKfamilyfont{hwzs}{STZhongsong}                        %华文中宋  hwzs\n\\newcommand{\\hwzs}{\\CJKfamily{hwzs}}\n\\setCJKfamilyfont{hwfs}{STFangsong}                            %华文仿宋  hwfs\n\\newcommand{\\hwfs}{\\CJKfamily{hwfs}}\n\\setCJKfamilyfont{hwxh}{STXihei}                                %华文细黑  hwxh\n\\newcommand{\\hwxh}{\\CJKfamily{hwxh}}\n\\setCJKfamilyfont{hwl}{STLiti}                                        %华文隶书  hwl\n\\newcommand{\\hwl}{\\CJKfamily{hwl}j\n\\setCJKfamilyfont{hwxw}{STXinwei}                                %华文新魏  hwxw\n\\newcommand{\\hwxw}{\\CJKfamily{hwxw}}\n\\setCJKfamilyfont{hwk}{STKaiti}                                    %华文楷体  hwk\n\\newcommand{\\hwk}{\\CJKfamily{hwk}}\n\\setCJKfamilyfont{hwxk}{STXingkai}                            %华文行楷  hwxk\n\\newcommand{\\hwxk}{\\CJKfamily{hwxk}}\n\\setCJKfamilyfont{hwcy}{STCaiyun}                                 %华文彩云 hwcy\n\\newcommand{\\hwcy}{\\CJKfamily{hwcy}}\n\\setCJKfamilyfont{hwhp}{STHupo}                                 %华文琥珀   hwhp\n\\newcommand{\\hwhp}{\\CJKfamily{hwhp}}\n\\setCJKfamilyfont{fzsong}{Simsun (Founder Extended)}     %方正宋体超大字符集   fzsong\n\\newcommand{\\fzsong}{\\CJKfamily{fzsong}}\n\\setCJKfamilyfont{fzyao}{FZYaoTi}                                    %方正姚体  fzy\n\\newcommand{\\fzyao}{\\CJKfamily{fzyao}}\n\\setCJKfamilyfont{fzshu}{FZShuTi}                                    %方正舒体 fzshu\n\\newcommand{\\fzshu}{\\CJKfamily{fzshu}}\n\\setCJKfamilyfont{asong}{Adobe Song Std}                        %Adobe 宋体  asong\n\\newcommand{\\asong}{\\CJKfamily{asong}}\n\\setCJKfamilyfont{ahei}{Adobe Heiti Std}                            %Adobe 黑体  ahei\n\\newcommand{\\ahei}{\\CJKfamily{ahei}}\n\\setCJKfamilyfont{akai}{Adobe Kaiti Std}                            %Adobe 楷体  akai\n\\newcommand{\\akai}{\\CJKfamily{akai}}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

Tools

PDF to Tex

sudo apt install abiword\nabiword --to=tex a.pdf\n
1
2

tlmgr

sudo apt-get install xzdec\ncd ~\nmkdir texmf\ntlmgr init-usertree\ntlmgr install <package>\n
1
2
3
4
5
tlmgr update --self --all\ntlmgr update --list\n
1
2
',30);p.render=function(a,t,p,o,c,l){return n(),s("div",null,[e])};export default p;export{t as __pageData}; diff --git a/assets/computerScience_latex_latexBasicNotes.md.d95b2977.lean.js b/assets/computerScience_latex_latexBasicNotes.md.d95b2977.lean.js new file mode 100644 index 00000000000..94b5d25aa06 --- /dev/null +++ b/assets/computerScience_latex_latexBasicNotes.md.d95b2977.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const t='{"title":"Latex Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Workflow","slug":"workflow"},{"level":3,"title":"Installation","slug":"installation"},{"level":3,"title":"Build Script","slug":"build-script"},{"level":2,"title":"Basis","slug":"basis"},{"level":3,"title":"Layout","slug":"layout"},{"level":2,"title":"Math","slug":"math"},{"level":3,"title":"Symbol","slug":"symbol"},{"level":2,"title":"template","slug":"template"},{"level":2,"title":"Tools","slug":"tools"},{"level":3,"title":"PDF to Tex","slug":"pdf-to-tex"},{"level":3,"title":"tlmgr","slug":"tlmgr"}],"relativePath":"computerScience/latex/latexBasicNotes.md","lastUpdated":1627194884000}',p={},e=a('',30);p.render=function(a,t,p,o,c,l){return n(),s("div",null,[e])};export default p;export{t as __pageData}; diff --git a/assets/computerScience_network_networkBasicNotes.md.81a62821.js b/assets/computerScience_network_networkBasicNotes.md.81a62821.js new file mode 100644 index 00000000000..6090e0b3174 --- /dev/null +++ b/assets/computerScience_network_networkBasicNotes.md.81a62821.js @@ -0,0 +1 @@ +import{o as e,c as a,a as r}from"./app.6dd2a1b7.js";const s='{"title":"Computer NetWorking Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Computer Network and Internet","slug":"computer-network-and-internet"},{"level":3,"title":"What is Internet","slug":"what-is-internet"},{"level":2,"title":"Application Layer","slug":"application-layer"},{"level":3,"title":"Application Layer Protocol","slug":"application-layer-protocol"},{"level":3,"title":"HTTP","slug":"http"},{"level":3,"title":"Process","slug":"process"},{"level":2,"title":"Transport Layer","slug":"transport-layer"},{"level":3,"title":"TCP(Transmission Control Protocol)","slug":"tcp-transmission-control-protocol"},{"level":3,"title":"UDP","slug":"udp"},{"level":3,"title":"ICMP","slug":"icmp"},{"level":3,"title":"Error Detection","slug":"error-detection"},{"level":3,"title":"Slide Window and Retransmission","slug":"slide-window-and-retransmission"},{"level":2,"title":"Network Layer","slug":"network-layer"},{"level":3,"title":"IP Service Model","slug":"ip-service-model"},{"level":3,"title":"IPv4 Addresses","slug":"ipv4-addresses"},{"level":3,"title":"ARP(Address Resolution Protocol)","slug":"arp-address-resolution-protocol"},{"level":3,"title":"Packet Format","slug":"packet-format"},{"level":2,"title":"WireShark","slug":"wireshark"},{"level":3,"title":"WireShark SetUp","slug":"wireshark-setup"},{"level":2,"title":"Other Tools","slug":"other-tools"}],"relativePath":"computerScience/network/networkBasicNotes.md","lastUpdated":1627194884000}',n={},t=r('

Computer NetWorking Notes

Computer Network and Internet

What is Internet

end-to-end principle: implement features in the end-system/hosts where possible

congestion implemented on Transport Layer

Consist

Internet Service Provider -> Packet Switch/Communication Link -> Host/End System

Delay

nodal = proc + queue + trans + prop: 总时延 = 产生/到达时延 + 排队时延 + 传输时延 + 传播时延

Layer

  • Application Layer Protocol: HTTP SMTP (message, stream of data)
  • Transport Layer Protocol: TCP UDP (segment, segment of data)
  • Network Layer Protocol: IP (因特网的粘合剂) (unreliable datagram, packet of data)
  • Data Link Layer Protocol: WiFi PPP(点对点) 以太网 (frame)
  • Physical Layer Protocol

Layering Principle

  • modularity
  • well defined service: simple service model provided by lower level, providing for higher level
  • reuse
  • separation of concerns
  • continuous improvement: change inner structure of layer independently

Application Layer

Application Layer Protocol

defines:

  • types of messages exchanged
  • syntax of various message types(fields definition)
  • semantics of fields
  • rules for when/how to send/respond to messages

HTTP

  • HTTP -> Socket Interface -> TCP
  • stateless protocol

Non-Persistent Connections and Persistent Connections

  • non-persistent connections: 1 http request with 1 tcp connection
  • persistent connections: multiple http request with 1 tcp connection

HTTP Message Format

  • http request format
request line -> (method field, object url field, protocol version)\nheader lines -> Host/Connections(close -> non-persistent connection)/User-agent/Accept-language\n\\r\\n\nentity body\n
1
2
3
4
  • http response format
status line -> (protocol version, status code, corresponding status message)\nheader lines -> Connections/Date/Server/Last-Modified/Content-Length(bytes)/Content-Type\n\\r\\n\nentity body\n
1
2
3
4

Cookies

  • first request header -> without cookie
  • first response header -> Set-Cookie: number to client
  • client store identification number for specific site into cookies files
  • second request header -> Cookie: number (extract identification number for specific site from cookies files)
  • function: create User Session Layer on top of stateless HTTP

Process

Port to Transport Layer

  • bandwidth-sensitive application: UDP
  • reliable-sensitive application: TCP
email                   - SMTP              - TCP\nremote terminal access  - Telnet            - TCP\nWeb                     - HTTP/HTTPS        - TCP\nfile transfer            - FTP               - TCP\nstreaming multimedia    - HTTP/HTTPS/RTP    - TCP/UDP\ninternet telephony      - SIP/RTP           - UDP\n
1
2
3
4
5
6

Address

  • (32 bits network layer )IP : find specific host/end-system
  • (16 bits transport layer)port: find specific process

Transport Layer

TCP(Transmission Control Protocol)

  • connection-oriented service
  • in-sequence stream of bytes service
  • reliable delivery
  • congestion control

3-way handshake

  • SYN -> SYN/ACK -> ACK
  • FIN(toB) -> [Data+]ACK(toA) -> FIN(toA) -> ACK(toB)

UDP

User Datagram Protocol:

  • lightweight and connectionless
  • datagram service
  • unreliable delivery
  • udp header: source port(16 bit), destination port(16 bit), checksum(16 bit), length(16 bit)

ICMP

Internet Control Message Protocol:

  • report error conditions back
  • help diagnose problems
  • site above IP

ICMP Message Type

  • 0 type 0 code: echo reply(by ping)
  • 3 type 0 code: destination network unreachable
  • 3 type 1 code: destination host unreachable
  • 3 type 3 code: destination port unreachable
  • 8 type 0 code: echo request(by ping)
  • 11 type 0 code: TTL(time to live) Expired(by ping)

Error Detection

  • TCP/IP: checksums(1 bit)
  • Ethernet(Link Layer): cyclic redundancy code(2 bit/more)
  • SSL(Secure Sockets Layer)/TLS(Transport Layer Security): message authentication code(MAC)(cryptographic data) - prevent malicious attacks

Slide Window and Retransmission

  • SWZ N and RWS 1: go back N
  • SWZ N and RWZ N: selective repeat

Network Layer

IP Service Model

  • prevent packets looping forever(TTL/time to live field in header): if TTL gets decreased to zero, then drop this datagram
  • limit fragment packets size(Packet ID, Flags, Fragment Offset)
  • reduce changes of wrong destination(Checksum, Destination Address)

IPv4 Addresses

  • 32 bits long: a.b.c.d

Address Structure

  • historical: class A: 0-network(7 bits)-host(24 bits) class B: 10-network(14 bits)-host(16 bits) class C: 110-network(21 bits)-host(8 bits)
  • today: 171.64.0.0/16 means 171.64.0.0 to 171.64.255.255, A/24 describes 256 addresses, A/20 describes 4096 addresses
  • longest prefix matching and netmask(A/16 e.g 0.0.0.0/0 => matching all addresses)

ARP(Address Resolution Protocol)

  • generates mappings between link layer and network layer addresses cached in nodes
  • request-reply protocol: who has network address X => I have network address X
  • request sent to link layer broadcast address, reply sent to requesting address
  • when request to dest ARP packet header with empty DEST HARDWARE ADDRESS field and opcode 1(request)
  • when reply to src ARP packet header with dest hardware address as SRC HARDWARE ADDRESS field, src hardware address as DEST HARDWARE ADDRESS field and opcode 2(reply)
  • if A and B aren't in same sub-net, they delivery ARP broadcast with third public gateway

Packet Format

Endian

  • network is big-endian
  • in x86 processor, use htons()/ntohs()/htonl()/ntohl() host: network -short/long helper function to transform format
#include <arpa/inet.h>\n\nuint16_t http_port = 80;\nuint16_t packet_port = ntohs(packet->port);\n\nif (packet_port == http_port) {\n    // OK\n}\n
1
2
3
4
5
6
7
8

WireShark

WireShark SetUp

sudo dpkg-reconfigure wireshark-common\nsudo gpasswd -a $USER wireshark\n
1
2

Other Tools

  • ifconfig + egrep
  • netstat + egrep
  • tcpdump
  • nslookup
',70);n.render=function(r,s,n,i,l,o){return e(),a("div",null,[t])};export default n;export{s as __pageData}; diff --git a/assets/computerScience_network_networkBasicNotes.md.81a62821.lean.js b/assets/computerScience_network_networkBasicNotes.md.81a62821.lean.js new file mode 100644 index 00000000000..d343079b7b7 --- /dev/null +++ b/assets/computerScience_network_networkBasicNotes.md.81a62821.lean.js @@ -0,0 +1 @@ +import{o as e,c as a,a as r}from"./app.6dd2a1b7.js";const s='{"title":"Computer NetWorking Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Computer Network and Internet","slug":"computer-network-and-internet"},{"level":3,"title":"What is Internet","slug":"what-is-internet"},{"level":2,"title":"Application Layer","slug":"application-layer"},{"level":3,"title":"Application Layer Protocol","slug":"application-layer-protocol"},{"level":3,"title":"HTTP","slug":"http"},{"level":3,"title":"Process","slug":"process"},{"level":2,"title":"Transport Layer","slug":"transport-layer"},{"level":3,"title":"TCP(Transmission Control Protocol)","slug":"tcp-transmission-control-protocol"},{"level":3,"title":"UDP","slug":"udp"},{"level":3,"title":"ICMP","slug":"icmp"},{"level":3,"title":"Error Detection","slug":"error-detection"},{"level":3,"title":"Slide Window and Retransmission","slug":"slide-window-and-retransmission"},{"level":2,"title":"Network Layer","slug":"network-layer"},{"level":3,"title":"IP Service Model","slug":"ip-service-model"},{"level":3,"title":"IPv4 Addresses","slug":"ipv4-addresses"},{"level":3,"title":"ARP(Address Resolution Protocol)","slug":"arp-address-resolution-protocol"},{"level":3,"title":"Packet Format","slug":"packet-format"},{"level":2,"title":"WireShark","slug":"wireshark"},{"level":3,"title":"WireShark SetUp","slug":"wireshark-setup"},{"level":2,"title":"Other Tools","slug":"other-tools"}],"relativePath":"computerScience/network/networkBasicNotes.md","lastUpdated":1627194884000}',n={},t=r('',70);n.render=function(r,s,n,i,l,o){return e(),a("div",null,[t])};export default n;export{s as __pageData}; diff --git a/assets/computerScience_operatingSystems_CSAPP.md.fa06cc16.js b/assets/computerScience_operatingSystems_CSAPP.md.fa06cc16.js new file mode 100644 index 00000000000..72451a0949d --- /dev/null +++ b/assets/computerScience_operatingSystems_CSAPP.md.fa06cc16.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as t}from"./app.6dd2a1b7.js";const e='{"title":"Computer Systems: A Programmer\'s Perspective - System Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"机器码与进制转换","slug":"机器码与进制转换"},{"level":3,"title":"浮点数","slug":"浮点数"},{"level":3,"title":"Stack Frame","slug":"stack-frame"},{"level":2,"title":"Architecture","slug":"architecture"},{"level":3,"title":"Control Signal","slug":"control-signal"},{"level":3,"title":"Special Control Signal","slug":"special-control-signal"},{"level":3,"title":"Procedure Control Signal","slug":"procedure-control-signal"},{"level":3,"title":"Forwarding","slug":"forwarding"},{"level":3,"title":"Exception","slug":"exception"},{"level":3,"title":"Efficiency","slug":"efficiency"},{"level":2,"title":"Optimization","slug":"optimization"},{"level":3,"title":"Principles","slug":"principles"},{"level":3,"title":"Tips","slug":"tips"},{"level":2,"title":"异常控制流","slug":"异常控制流"},{"level":3,"title":"异常","slug":"异常"},{"level":3,"title":"进程","slug":"进程"},{"level":3,"title":"信号","slug":"信号"},{"level":3,"title":"非本地跳转","slug":"非本地跳转"},{"level":2,"title":"系统级 I/O","slug":"系统级-i-o"},{"level":3,"title":"socket I/O","slug":"socket-i-o"},{"level":2,"title":"网络","slug":"网络"},{"level":2,"title":"并发","slug":"并发"},{"level":3,"title":"锁","slug":"锁"},{"level":2,"title":"调试/测试","slug":"调试-测试"},{"level":3,"title":"日志","slug":"日志"}],"relativePath":"computerScience/operatingSystems/CSAPP.md","lastUpdated":1627194884000}',l={},o=a("h1",{id:"computer-systems-a-programmer-s-perspective-system-basic-notes"},[a("a",{class:"header-anchor",href:"#computer-systems-a-programmer-s-perspective-system-basic-notes","aria-hidden":"true"},"#"),t(" Computer Systems: A Programmer's Perspective - System Basic Notes")],-1),c=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#computer-systems-a-programmer-s-perspective-system-basic-notes"},"Computer Systems: A Programmer's Perspective - System Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#机器码与进制转换"},"机器码与进制转换")]),a("li",null,[a("a",{href:"#architecture"},"Architecture")]),a("li",null,[a("a",{href:"#optimization"},"Optimization")]),a("li",null,[a("a",{href:"#异常控制流"},"异常控制流")]),a("li",null,[a("a",{href:"#系统级-i-o"},"系统级 I/O")]),a("li",null,[a("a",{href:"#网络"},"网络")]),a("li",null,[a("a",{href:"#并发"},"并发")]),a("li",null,[a("a",{href:"#调试-测试"},"调试/测试")])])])])])],-1),p=a("h2",{id:"机器码与进制转换"},[a("a",{class:"header-anchor",href:"#机器码与进制转换","aria-hidden":"true"},"#"),t(" 机器码与进制转换")],-1),u=a("h3",{id:"浮点数"},[a("a",{class:"header-anchor",href:"#浮点数","aria-hidden":"true"},"#"),t(" 浮点数")],-1),r=a("p",null,[t("规格化浮点数: "),a("a",{href:"http://1.xxx",target:"_blank",rel:"noopener noreferrer"},"1.xxx"),t(" _ 2^(exp - 127) - e.g "),a("code",null,"5 = 5.0 = 1.25 _ 2^(129 - 127)")],-1),i=a("ul",null,[a("li",null,"xxx: 尾数"),a("li",null,"exp: 阶码")],-1),k=a("h3",{id:"stack-frame"},[a("a",{class:"header-anchor",href:"#stack-frame","aria-hidden":"true"},"#"),t(" Stack Frame")],-1),b=a("p",null,[a("img",{src:"/awesome-notes/assets/stack_frame.daa8403a.png",alt:"stack frame"})],-1),d=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[t("# 准备阶段\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),t(),a("span",{class:"token expression"},[t("Caller"),a("span",{class:"token operator"},"-"),t("Save"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token operator"},"%"),t("eax "),a("span",{class:"token operator"},"%"),t("ecx "),a("span",{class:"token operator"},"%"),t("edx")])]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),t(),a("span",{class:"token expression"},[t("Callee"),a("span",{class:"token operator"},"-"),t("Save"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token operator"},"%"),t("ebx "),a("span",{class:"token operator"},"%"),t("esi "),a("span",{class:"token operator"},"%"),t("edi")])]),t("\n# 传参顺序"),a("span",{class:"token operator"},":"),t(" rdi"),a("span",{class:"token punctuation"},","),t(" dsi"),a("span",{class:"token punctuation"},","),t(" rdx"),a("span",{class:"token punctuation"},","),t(" rcx"),a("span",{class:"token punctuation"},","),t(" r8"),a("span",{class:"token punctuation"},","),t(" r9"),a("span",{class:"token punctuation"},","),t(" stack\n\npushl "),a("span",{class:"token operator"},"%"),t("ebp\nmovl "),a("span",{class:"token operator"},"%"),t("esp"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"%"),t("ebp\npushl "),a("span",{class:"token operator"},"%"),t("ebx\n\n# 结束阶段\n\nmovl "),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"%"),t("ebp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"%"),t("ebx\nmovl "),a("span",{class:"token operator"},"%"),t("ebp"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"%"),t("esp\npopl "),a("span",{class:"token operator"},"%"),t("ebp\nret\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br")])],-1),m=a("p",null,[a("code",null,"x86_64"),t(": 可使用超出 Stack Pointer 128 bytes 的内存区域, 称为 Red Zone.")],-1),h=a("h2",{id:"architecture"},[a("a",{class:"header-anchor",href:"#architecture","aria-hidden":"true"},"#"),t(" Architecture")],-1),f=a("h3",{id:"control-signal"},[a("a",{class:"header-anchor",href:"#control-signal","aria-hidden":"true"},"#"),t(" Control Signal")],-1),g=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"left"}},"State"),a("th",{style:{"text-align":"left"}},"Signal")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"left"}},"Fetch"),a("td",{style:{"text-align":"left"}},"icode,ifun rA,rB valC,valP")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Decode"),a("td",{style:{"text-align":"left"}},"valA,srcA valB,srcB")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Execute"),a("td",{style:{"text-align":"left"}},"valE Condition")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Memory"),a("td",{style:{"text-align":"left"}},"valM read/write")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"WriteBack"),a("td",{style:{"text-align":"left"}},"E port,dstE M port,dstM")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"PCUpdate"),a("td",{style:{"text-align":"left"}},"PC")])])],-1),y=a("h3",{id:"special-control-signal"},[a("a",{class:"header-anchor",href:"#special-control-signal","aria-hidden":"true"},"#"),t(" Special Control Signal")],-1),w=a("ul",null,[a("li",null,[t("handle ret: "),a("code",null,"IRET in {D_icode, E_icode, M_icode}")]),a("li",null,[t("load/use hazard: "),a("code",null,"E_icode in {IMRMOVL, IPOPL} && E_dstM in {d_srcA, d_srcB}")]),a("li",null,[t("mispredicted branch: "),a("code",null,"E_icode in {IJXX} && !e_Cnd")]),a("li",null,[t("exception: "),a("code",null,"m_stat in {SADR, SINS, SHLT} || W_stat in {SADR, SINS, SHLT}")])],-1),_=a("h3",{id:"procedure-control-signal"},[a("a",{class:"header-anchor",href:"#procedure-control-signal","aria-hidden":"true"},"#"),t(" Procedure Control Signal")],-1),v=a("p",null,"Branch, Loop, Jump:",-1),x=a("p",null,[t("PrectPC | "),a("code",null,"W_valM"),t("(无法预测) | "),a("code",null,"M_valP/M_valA"),t(" (在译码阶段合并信号量 valA 与 valP: PCUpdate 位于 Fetch,无需传递 valP, 只剩 call/jump 需要 valP)")],-1),S=a("ul",null,[a("li",null,"AT: always taken"),a("li",null,"NT: never taken"),a("li",null,"BTFNT: backward taken forward not taken")],-1),I=a("h3",{id:"forwarding"},[a("a",{class:"header-anchor",href:"#forwarding","aria-hidden":"true"},"#"),t(" Forwarding")],-1),L=a("p",null,"流水线中最早阶段的转发源, 优先级最高 execute > memory > write",-1),C=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),t(" d_valA "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),t("\n D_icode in "),a("span",{class:"token operator"},"{"),t("ICALL"),a("span",{class:"token punctuation"},","),t(" IJXX"),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},":"),t(" D_valP"),a("span",{class:"token punctuation"},";"),t("\n\n d_srcA "),a("span",{class:"token operator"},"=="),t(" e_dstE "),a("span",{class:"token punctuation"},":"),t(" e_valE"),a("span",{class:"token punctuation"},";"),t("\n d_srcA "),a("span",{class:"token operator"},"=="),t(" M_dstM "),a("span",{class:"token punctuation"},":"),t(" m_valM"),a("span",{class:"token punctuation"},";"),t("\n d_srcA "),a("span",{class:"token operator"},"=="),t(" M_dstE "),a("span",{class:"token punctuation"},":"),t(" M_valE"),a("span",{class:"token punctuation"},";"),t("\n d_srcA "),a("span",{class:"token operator"},"=="),t(" W_dstM "),a("span",{class:"token punctuation"},":"),t(" W_valM"),a("span",{class:"token punctuation"},";"),t("\n d_srcA "),a("span",{class:"token operator"},"=="),t(" W_dstE "),a("span",{class:"token punctuation"},":"),t(" W_valE"),a("span",{class:"token punctuation"},";"),t("\n\n # register file\n "),a("span",{class:"token number"},"1"),t(),a("span",{class:"token punctuation"},":"),t(" d_rvalA"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),E=a("h3",{id:"exception"},[a("a",{class:"header-anchor",href:"#exception","aria-hidden":"true"},"#"),t(" Exception")],-1),A=a("p",null,"流水线中最深的指令引起的异常, 优先级最高 e.g 访存阶段地址越界异常优先级高于取指阶段地址越界异常优先级",-1),N=a("h3",{id:"efficiency"},[a("a",{class:"header-anchor",href:"#efficiency","aria-hidden":"true"},"#"),t(" Efficiency")],-1),P=a("p",null,"CPI = 1.0 + lp + mp + rp:",-1),D=a("ul",null,[a("li",null,"lp: load penalty(load/use hazard)"),a("li",null,"mp: mispredicted branch penalty"),a("li",null,"rp: return penalty")],-1),M=a("h2",{id:"optimization"},[a("a",{class:"header-anchor",href:"#optimization","aria-hidden":"true"},"#"),t(" Optimization")],-1),z=a("h3",{id:"principles"},[a("a",{class:"header-anchor",href:"#principles","aria-hidden":"true"},"#"),t(" Principles")],-1),O=a("ul",null,[a("li",null,"合适的算法和数据结构"),a("li",null,"编写编译器能够有效优化的代码 e.g 寄存器别名,存储器别名,函数的副作用 导致编译器无法(不敢)合并/删除冗余代码"),a("li",null,"提高代码的并行性"),a("li",null,"消除不必要的访存 e.g 多次访存(可用中间量优化), 多次函数调用(可用宏/内联函数优化)")],-1),T=a("h3",{id:"tips"},[a("a",{class:"header-anchor",href:"#tips","aria-hidden":"true"},"#"),t(" Tips")],-1),B=a("h4",{id:"replacement"},[a("a",{class:"header-anchor",href:"#replacement","aria-hidden":"true"},"#"),t(" Replacement")],-1),U=a("ul",null,[a("li",null,"用多条 Shift/Add/Sub 指令, 代替 Mul/Div")],-1),G=a("h4",{id:"branch"},[a("a",{class:"header-anchor",href:"#branch","aria-hidden":"true"},"#"),t(" Branch")],-1),H=a("ul",null,[a("li",null,"利用条件表达式代替条件分支语句: 降低预测错误惩罚")],-1),R=a("h4",{id:"code-motion"},[a("a",{class:"header-anchor",href:"#code-motion","aria-hidden":"true"},"#"),t(" Code Motion")],-1),j=a("ul",null,[a("li",null,"将不变测试条件/常变量的计算从循环中移出"),a("li",null,"将多次访存利用中间自动/寄存器变量改写成一次访存")],-1),F=a("h4",{id:"unrolling-duff-s-device"},[a("a",{class:"header-anchor",href:"#unrolling-duff-s-device","aria-hidden":"true"},"#"),t(" Unrolling (Duff's Device)")],-1),W=a("p",null,"循环展开: 增大循环的步长 - Duff's Device 以 7 为步长:",-1),X=a("ul",null,[a("li",null,"提升循环的运行效率"),a("li",null,"一次循环内: 可先将所有数据先读出来(Memory State),将进行计算(Execute State), 从而消除 Load/Use 冒险而产生的 Bubble")],-1),K=a("h2",{id:"异常控制流"},[a("a",{class:"header-anchor",href:"#异常控制流","aria-hidden":"true"},"#"),t(" 异常控制流")],-1),J=a("p",null,"理解异常控制流,有助于理解以下概念:",-1),V=a("ul",null,[a("li",null,"陷阱(trap)/系统调用(system call)"),a("li",null,"系统级 I/O"),a("li",null,"线程/进程(concurrency)"),a("li",null,"虚拟存储器"),a("li",null,"软件异常")],-1),Z=a("h3",{id:"异常"},[a("a",{class:"header-anchor",href:"#异常","aria-hidden":"true"},"#"),t(" 异常")],-1),q=a("h4",{id:"分类"},[a("a",{class:"header-anchor",href:"#分类","aria-hidden":"true"},"#"),t(" 分类")],-1),Q=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"left"}},"类别"),a("th",{style:{"text-align":"left"}},"原因"),a("th",{style:{"text-align":"left"}},"异步/同步"),a("th",{style:{"text-align":"left"}},"返回行为"),a("th",{style:{"text-align":"left"}},"例子")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"left"}},"interrupt"),a("td",{style:{"text-align":"left"}},"输入/输出外部中断"),a("td",{style:{"text-align":"left"}},"async"),a("td",{style:{"text-align":"left"}},"next(concurrency)"),a("td",{style:{"text-align":"left"}},"磁盘")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"trap"),a("td",{style:{"text-align":"left"}},"主动异常/系统调用"),a("td",{style:{"text-align":"left"}},"sync"),a("td",{style:{"text-align":"left"}},"next"),a("td",{style:{"text-align":"left"}},"write/intN")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"fault"),a("td",{style:{"text-align":"left"}},"潜在可恢复的错误"),a("td",{style:{"text-align":"left"}},"sync"),a("td",{style:{"text-align":"left"}},"current/abort"),a("td",{style:{"text-align":"left"}},"seg/float exp")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"abort"),a("td",{style:{"text-align":"left"}},"不可恢复的错误"),a("td",{style:{"text-align":"left"}},"sync"),a("td",{style:{"text-align":"left"}},"abort(not return)"),a("td",{style:{"text-align":"left"}},"硬件错误")])])],-1),Y=a("h4",{id:"异常处理程序"},[a("a",{class:"header-anchor",href:"#异常处理程序","aria-hidden":"true"},"#"),t(" 异常处理程序")],-1),$=a("p",null,"异常处理程序主要分为 3 类:",-1),nn=a("ul",null,[a("li",null,"控制权返回给 Instruction_current"),a("li",null,"控制权返回给 Instruction_next"),a("li",null,"abort/exit")],-1),sn=a("h3",{id:"进程"},[a("a",{class:"header-anchor",href:"#进程","aria-hidden":"true"},"#"),t(" 进程")],-1),an=a("ul",null,[a("li",null,"一个独立的逻辑控制流(并行执行)"),a("li",null,"一个私有的地址空间(缓存与虚拟存储器)")],-1),tn=a("h4",{id:"上下文"},[a("a",{class:"header-anchor",href:"#上下文","aria-hidden":"true"},"#"),t(" 上下文")],-1),en=a("ul",null,[a("li",null,"代码/数据,堆/栈,通用寄存器,程序计数器,环境变量,文件描述符集合"),a("li",null,"上下文切换:用户模式与内核模式的切换"),a("li",null,"高速缓存污染(pollution): 每次切换后,总是会发生 cold cache miss")],-1),ln=a("h4",{id:"进程控制"},[a("a",{class:"header-anchor",href:"#进程控制","aria-hidden":"true"},"#"),t(" 进程控制")],-1),on=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),cn=a("h5",{id:"创建和终止进程"},[a("a",{class:"header-anchor",href:"#创建和终止进程","aria-hidden":"true"},"#"),t(" 创建和终止进程")],-1),pn=a("ul",null,[a("li",null,"父进程与子进程获得 2 份独立的私有空间与 2 份独立的上下文, 不同的 PID(process id)"),a("li",null,"由于指针(如打开的文件描述符),有可能互相影响,但大体上互不影响")],-1),un=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/*\n * output: parent: x=0\n * output: child: x=2\n * 独立上下文代表拥有独立的通用寄存器与栈,代表拥有拷贝的自动变量(局部变量),互不影响地进行修改\n */"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),t(" x "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(),a("span",{class:"token comment"},"// child"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"child: x=%d\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"++"),t("x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// parent"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"parent: x=%d\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"--"),t("x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),rn=a("h5",{id:"回收子进程"},[a("a",{class:"header-anchor",href:"#回收子进程","aria-hidden":"true"},"#"),t(" 回收子进程")],-1),kn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),t(),a("span",{class:"token macro-name"},"N"),t(),a("span",{class:"token expression"},[a("span",{class:"token number"},"2")])]),t("\n\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" status"),a("span",{class:"token punctuation"},","),t(" i"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},"["),t("N"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t(" ret_pid"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" N"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("pid"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(),a("span",{class:"token comment"},"// child"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"100"),a("span",{class:"token operator"},"+"),t("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// parent reaps(回收) N children in order"),t("\n i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"while"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("ret_pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"waitpid"),a("span",{class:"token punctuation"},"("),t("pid"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("status"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"WIFEXITED"),a("span",{class:"token punctuation"},"("),t("statue"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"child: %d terminated normally with exit status=%d\\n"'),a("span",{class:"token punctuation"},","),t("\n ret_pid"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token function"},"WEXITSTATUS"),a("span",{class:"token punctuation"},"("),t("status"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"child %d terminated abnormally\\n"'),a("span",{class:"token punctuation"},","),t(" ret_pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// only if there are no more children, it can exit normally"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"!="),t(" ECHILD"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"waitpid error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// exit with error log"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// exit normally"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br")])],-1),bn=a("h3",{id:"信号"},[a("a",{class:"header-anchor",href:"#信号","aria-hidden":"true"},"#"),t(" 信号")],-1),dn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),mn=a("ul",null,[a("li",null,"一个只发出而未被处理的信号为待处理信号"),a("li",null,[t("一种类型至多有一个待处理信号, 多余待处理信号"),a("strong",null,"不会进入处理队列"),t(",只是"),a("strong",null,"被简单丢弃")]),a("li",null,"不可以用信号对其他事件进行计数, 同一事件多次发生产生的信号有可能被简单丢弃")],-1),hn=a("h4",{id:"处理信号"},[a("a",{class:"header-anchor",href:"#处理信号","aria-hidden":"true"},"#"),t(" 处理信号")],-1),fn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"handler"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" sig"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"waitpid"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Handler reaped child %d\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),t("pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"!="),t(" ECHILD"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"waitpid error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"sleep"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" i"),a("span",{class:"token punctuation"},","),t(" n"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"char"),t(" buf"),a("span",{class:"token punctuation"},"["),t("MAX_BUF"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"signal"),a("span",{class:"token punctuation"},"("),t("SIGCHLD"),a("span",{class:"token punctuation"},","),t(" handler"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(" SIG_ERR"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"signal error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},";"),t(" I"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Hello from child %d\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token function"},"getpid"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"sleep"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// manually restart the READ call"),t("\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("n "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"read"),a("span",{class:"token punctuation"},"("),t("STDIN_FILENO"),a("span",{class:"token punctuation"},","),t(" buf"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),t("buf"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"!="),t(" EINTR"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"read error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Parent processing input\\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br")])],-1),gn=a("h4",{id:"阻塞信号"},[a("a",{class:"header-anchor",href:"#阻塞信号","aria-hidden":"true"},"#"),t(" 阻塞信号")],-1),yn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// how: SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK, 是否阻塞set中的信号合集"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigprocmask"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" how"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("old_set"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigemptyset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigfillset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigaddset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" sig_num"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigdelset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" sig_num"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigismember"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set, "),a("span",{class:"token keyword"},"int"),t(" sig_num"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),wn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"handler"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" sig"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"waitpid"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"delete_job"),a("span",{class:"token punctuation"},"("),t("pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"!="),t(" ECHILD"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"waitpid error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// 保证父进程先执行 add_job, 再执行 delete_job"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" argc"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),t("argv"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"sigset_t"),t(" mask"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token function"},"signal"),a("span",{class:"token punctuation"},"("),t("SIGCHLD"),a("span",{class:"token punctuation"},","),t(" handler"),a("span",{class:"token punctuation"},";"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token function"},"init_job"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"sigemptyset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"sigaddset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},","),t(" SIGCHLD"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"sigprocmask"),a("span",{class:"token punctuation"},"("),t("SIG_BLOCK"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// block SIGCHLD"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// unblock SIGCHLD in child, make it can transfer signal"),t("\n "),a("span",{class:"token function"},"sigprocmask"),a("span",{class:"token punctuation"},"("),t("SIG_UNBLOCK"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"execve"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"/bin/date"'),a("span",{class:"token punctuation"},","),t(" argv"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// parent process"),t("\n "),a("span",{class:"token function"},"add_job"),a("span",{class:"token punctuation"},"("),t("pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// after add_job, unblock SIGCHLD, make it can handle signal"),t("\n "),a("span",{class:"token function"},"sigprocmask"),a("span",{class:"token punctuation"},"("),t("SIG_UNBLOCK"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br")])],-1),_n=a("h3",{id:"非本地跳转"},[a("a",{class:"header-anchor",href:"#非本地跳转","aria-hidden":"true"},"#"),t(" 非本地跳转")],-1),vn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),xn=a("ul",null,[a("li",null,"setjmp - catch: 返回多次"),a("li",null,"longjmp - throw: 不返回")],-1),Sn=a("h2",{id:"系统级-i-o"},[a("a",{class:"header-anchor",href:"#系统级-i-o","aria-hidden":"true"},"#"),t(" 系统级 I/O")],-1),In=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// robust I/O"),t("\n"),a("span",{class:"token class-name"},"ssize_t"),t(),a("span",{class:"token function"},"rio_read_n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" fd"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token operator"},"*"),t("usr_buf"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"size_t"),t(" n"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"size_t"),t(" n_left "),a("span",{class:"token operator"},"="),t(" n"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"ssize_t"),t(" n_read"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("buf_p "),a("span",{class:"token operator"},"="),t(" usr_buf"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("n_left "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("n_read "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"read"),a("span",{class:"token punctuation"},"("),t("fd"),a("span",{class:"token punctuation"},","),t(" buf_p"),a("span",{class:"token punctuation"},","),t(" n_left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"=="),t(" EINTR"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n n_read "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// interrupted by signal_handler, re-call read()"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("n_read "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"break"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n n_left "),a("span",{class:"token operator"},"-="),t(" n_read"),a("span",{class:"token punctuation"},";"),t("\n buf_p "),a("span",{class:"token operator"},"+="),t(" n_read"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// remove data from buf_p"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"("),t("n "),a("span",{class:"token operator"},"-"),t(" left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n\n"),a("span",{class:"token class-name"},"ssize_t"),t(),a("span",{class:"token function"},"rio_write_n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" fd"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token operator"},"*"),t("usr_buf"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"size_t"),t(" n"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"size_t"),t(" n_left "),a("span",{class:"token operator"},"="),t(" n"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"ssize_t"),t(" n_written"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("buf_p "),a("span",{class:"token operator"},"="),t(" usr_buf"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("n_left "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("n_written "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"read"),a("span",{class:"token punctuation"},"("),t("fd"),a("span",{class:"token punctuation"},","),t(" buf_p"),a("span",{class:"token punctuation"},","),t(" n_left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"=="),t(" EINTR"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n n_written "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// interrupted by signal_handler, re-call read()"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n n_left "),a("span",{class:"token operator"},"-="),t(" n_written"),a("span",{class:"token punctuation"},";"),t("\n buf_p "),a("span",{class:"token operator"},"+="),t(" n_written"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// remove data from buf_p"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" n"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br")])],-1),Ln=a("h3",{id:"socket-i-o"},[a("a",{class:"header-anchor",href:"#socket-i-o","aria-hidden":"true"},"#"),t(" socket I/O")],-1),Cn=a("h4",{id:"限制"},[a("a",{class:"header-anchor",href:"#限制","aria-hidden":"true"},"#"),t(" 限制")],-1),En=a("p",null,"输出函数+输入函数: 中间必须插入 fflush, fseek, fsetpos, rewind 输入函数+输出函数: 中间必须插入 fseek, fsetpos, rewind",-1),An=a("h4",{id:"i-o-函数的选择"},[a("a",{class:"header-anchor",href:"#i-o-函数的选择","aria-hidden":"true"},"#"),t(" I/O 函数的选择")],-1),Nn=a("ul",null,[a("li",null,"sprintf+rio_written: 格式化输出至套接口"),a("li",null,"rio_readlineb + sscanf: 格式化输入")],-1),Pn=a("h2",{id:"网络"},[a("a",{class:"header-anchor",href:"#网络","aria-hidden":"true"},"#"),t(" 网络")],-1),Dn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" argc"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("argv"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),t("pp"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"struct"),t(),a("span",{class:"token class-name"},"in_addr"),t(" addr"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"struct"),t(),a("span",{class:"token class-name"},"hostent"),t(),a("span",{class:"token operator"},"*"),t("host_p"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("argc "),a("span",{class:"token operator"},"!="),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"usage: %s \\n"'),a("span",{class:"token punctuation"},","),t(" argv"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"inet_aton"),a("span",{class:"token punctuation"},"("),t("argv"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("addr"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"!="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n host_p "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"gethostbyaddr"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token keyword"},"char"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),t("addr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),t("addr"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t(" AF_INET"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n host_p "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"gethostbyname"),a("span",{class:"token punctuation"},"("),t("argv"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"official hostname: %s\\n:"'),a("span",{class:"token punctuation"},","),t(" host_p"),a("span",{class:"token operator"},"->"),t("h_name"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("pp "),a("span",{class:"token operator"},"="),t(" host_p"),a("span",{class:"token operator"},"->"),t("h_aliases"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token operator"},"*"),t("pp "),a("span",{class:"token operator"},"!="),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},";"),t(" pp"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"alias: %s\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"*"),t("pp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("pp "),a("span",{class:"token operator"},"="),t(" host_p"),a("span",{class:"token operator"},"->"),t("h_addr_list"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token operator"},"*"),t("pp "),a("span",{class:"token operator"},"!="),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},";"),t(" pp"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n addr"),a("span",{class:"token punctuation"},"."),t("s_addr "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"struct"),t(),a("span",{class:"token class-name"},"in_addr"),t(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"*"),t("pp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"->"),t("s_addr"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"address: %s\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token function"},"inet_ntoa"),a("span",{class:"token punctuation"},"("),t("addr"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br")])],-1),Mn=a("h2",{id:"并发"},[a("a",{class:"header-anchor",href:"#并发","aria-hidden":"true"},"#"),t(" 并发")],-1),zn=a("h3",{id:"锁"},[a("a",{class:"header-anchor",href:"#锁","aria-hidden":"true"},"#"),t(" 锁")],-1),On=a("p",null,"防止死锁: 每对互斥锁(s, t), 每个线程顺序请求锁, 逆序释放锁",-1),Tn=a("h2",{id:"调试-测试"},[a("a",{class:"header-anchor",href:"#调试-测试","aria-hidden":"true"},"#"),t(" 调试/测试")],-1),Bn=a("h3",{id:"日志"},[a("a",{class:"header-anchor",href:"#日志","aria-hidden":"true"},"#"),t(" 日志")],-1),Un=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("msg"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"%s: %s\\n"'),a("span",{class:"token punctuation"},","),t(" msg"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token function"},"strerror"),a("span",{class:"token punctuation"},"("),t("errno"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"posix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" code"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("msg"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"%s: %s\\n"'),a("span",{class:"token punctuation"},","),t(" msg"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token function"},"strerror"),a("span",{class:"token punctuation"},"("),t("code"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"dns_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("msg"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"%s: DNS error %d\\n"'),a("span",{class:"token punctuation"},","),t(" msg"),a("span",{class:"token punctuation"},","),t(" h_errno"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"app_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("msg"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"%s\\n"'),a("span",{class:"token punctuation"},","),t(" msg"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1);l.render=function(a,t,e,l,Gn,Hn){return n(),s("div",null,[o,c,p,u,r,i,k,b,d,m,h,f,g,y,w,_,v,x,S,I,L,C,E,A,N,P,D,M,z,O,T,B,U,G,H,R,j,F,W,X,K,J,V,Z,q,Q,Y,$,nn,sn,an,tn,en,ln,on,cn,pn,un,rn,kn,bn,dn,mn,hn,fn,gn,yn,wn,_n,vn,xn,Sn,In,Ln,Cn,En,An,Nn,Pn,Dn,Mn,zn,On,Tn,Bn,Un])};export default l;export{e as __pageData}; diff --git a/assets/computerScience_operatingSystems_CSAPP.md.fa06cc16.lean.js b/assets/computerScience_operatingSystems_CSAPP.md.fa06cc16.lean.js new file mode 100644 index 00000000000..72451a0949d --- /dev/null +++ b/assets/computerScience_operatingSystems_CSAPP.md.fa06cc16.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as t}from"./app.6dd2a1b7.js";const e='{"title":"Computer Systems: A Programmer\'s Perspective - System Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"机器码与进制转换","slug":"机器码与进制转换"},{"level":3,"title":"浮点数","slug":"浮点数"},{"level":3,"title":"Stack Frame","slug":"stack-frame"},{"level":2,"title":"Architecture","slug":"architecture"},{"level":3,"title":"Control Signal","slug":"control-signal"},{"level":3,"title":"Special Control Signal","slug":"special-control-signal"},{"level":3,"title":"Procedure Control Signal","slug":"procedure-control-signal"},{"level":3,"title":"Forwarding","slug":"forwarding"},{"level":3,"title":"Exception","slug":"exception"},{"level":3,"title":"Efficiency","slug":"efficiency"},{"level":2,"title":"Optimization","slug":"optimization"},{"level":3,"title":"Principles","slug":"principles"},{"level":3,"title":"Tips","slug":"tips"},{"level":2,"title":"异常控制流","slug":"异常控制流"},{"level":3,"title":"异常","slug":"异常"},{"level":3,"title":"进程","slug":"进程"},{"level":3,"title":"信号","slug":"信号"},{"level":3,"title":"非本地跳转","slug":"非本地跳转"},{"level":2,"title":"系统级 I/O","slug":"系统级-i-o"},{"level":3,"title":"socket I/O","slug":"socket-i-o"},{"level":2,"title":"网络","slug":"网络"},{"level":2,"title":"并发","slug":"并发"},{"level":3,"title":"锁","slug":"锁"},{"level":2,"title":"调试/测试","slug":"调试-测试"},{"level":3,"title":"日志","slug":"日志"}],"relativePath":"computerScience/operatingSystems/CSAPP.md","lastUpdated":1627194884000}',l={},o=a("h1",{id:"computer-systems-a-programmer-s-perspective-system-basic-notes"},[a("a",{class:"header-anchor",href:"#computer-systems-a-programmer-s-perspective-system-basic-notes","aria-hidden":"true"},"#"),t(" Computer Systems: A Programmer's Perspective - System Basic Notes")],-1),c=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#computer-systems-a-programmer-s-perspective-system-basic-notes"},"Computer Systems: A Programmer's Perspective - System Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#机器码与进制转换"},"机器码与进制转换")]),a("li",null,[a("a",{href:"#architecture"},"Architecture")]),a("li",null,[a("a",{href:"#optimization"},"Optimization")]),a("li",null,[a("a",{href:"#异常控制流"},"异常控制流")]),a("li",null,[a("a",{href:"#系统级-i-o"},"系统级 I/O")]),a("li",null,[a("a",{href:"#网络"},"网络")]),a("li",null,[a("a",{href:"#并发"},"并发")]),a("li",null,[a("a",{href:"#调试-测试"},"调试/测试")])])])])])],-1),p=a("h2",{id:"机器码与进制转换"},[a("a",{class:"header-anchor",href:"#机器码与进制转换","aria-hidden":"true"},"#"),t(" 机器码与进制转换")],-1),u=a("h3",{id:"浮点数"},[a("a",{class:"header-anchor",href:"#浮点数","aria-hidden":"true"},"#"),t(" 浮点数")],-1),r=a("p",null,[t("规格化浮点数: "),a("a",{href:"http://1.xxx",target:"_blank",rel:"noopener noreferrer"},"1.xxx"),t(" _ 2^(exp - 127) - e.g "),a("code",null,"5 = 5.0 = 1.25 _ 2^(129 - 127)")],-1),i=a("ul",null,[a("li",null,"xxx: 尾数"),a("li",null,"exp: 阶码")],-1),k=a("h3",{id:"stack-frame"},[a("a",{class:"header-anchor",href:"#stack-frame","aria-hidden":"true"},"#"),t(" Stack Frame")],-1),b=a("p",null,[a("img",{src:"/awesome-notes/assets/stack_frame.daa8403a.png",alt:"stack frame"})],-1),d=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[t("# 准备阶段\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),t(),a("span",{class:"token expression"},[t("Caller"),a("span",{class:"token operator"},"-"),t("Save"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token operator"},"%"),t("eax "),a("span",{class:"token operator"},"%"),t("ecx "),a("span",{class:"token operator"},"%"),t("edx")])]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),t(),a("span",{class:"token expression"},[t("Callee"),a("span",{class:"token operator"},"-"),t("Save"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token operator"},"%"),t("ebx "),a("span",{class:"token operator"},"%"),t("esi "),a("span",{class:"token operator"},"%"),t("edi")])]),t("\n# 传参顺序"),a("span",{class:"token operator"},":"),t(" rdi"),a("span",{class:"token punctuation"},","),t(" dsi"),a("span",{class:"token punctuation"},","),t(" rdx"),a("span",{class:"token punctuation"},","),t(" rcx"),a("span",{class:"token punctuation"},","),t(" r8"),a("span",{class:"token punctuation"},","),t(" r9"),a("span",{class:"token punctuation"},","),t(" stack\n\npushl "),a("span",{class:"token operator"},"%"),t("ebp\nmovl "),a("span",{class:"token operator"},"%"),t("esp"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"%"),t("ebp\npushl "),a("span",{class:"token operator"},"%"),t("ebx\n\n# 结束阶段\n\nmovl "),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"%"),t("ebp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"%"),t("ebx\nmovl "),a("span",{class:"token operator"},"%"),t("ebp"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"%"),t("esp\npopl "),a("span",{class:"token operator"},"%"),t("ebp\nret\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br")])],-1),m=a("p",null,[a("code",null,"x86_64"),t(": 可使用超出 Stack Pointer 128 bytes 的内存区域, 称为 Red Zone.")],-1),h=a("h2",{id:"architecture"},[a("a",{class:"header-anchor",href:"#architecture","aria-hidden":"true"},"#"),t(" Architecture")],-1),f=a("h3",{id:"control-signal"},[a("a",{class:"header-anchor",href:"#control-signal","aria-hidden":"true"},"#"),t(" Control Signal")],-1),g=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"left"}},"State"),a("th",{style:{"text-align":"left"}},"Signal")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"left"}},"Fetch"),a("td",{style:{"text-align":"left"}},"icode,ifun rA,rB valC,valP")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Decode"),a("td",{style:{"text-align":"left"}},"valA,srcA valB,srcB")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Execute"),a("td",{style:{"text-align":"left"}},"valE Condition")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Memory"),a("td",{style:{"text-align":"left"}},"valM read/write")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"WriteBack"),a("td",{style:{"text-align":"left"}},"E port,dstE M port,dstM")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"PCUpdate"),a("td",{style:{"text-align":"left"}},"PC")])])],-1),y=a("h3",{id:"special-control-signal"},[a("a",{class:"header-anchor",href:"#special-control-signal","aria-hidden":"true"},"#"),t(" Special Control Signal")],-1),w=a("ul",null,[a("li",null,[t("handle ret: "),a("code",null,"IRET in {D_icode, E_icode, M_icode}")]),a("li",null,[t("load/use hazard: "),a("code",null,"E_icode in {IMRMOVL, IPOPL} && E_dstM in {d_srcA, d_srcB}")]),a("li",null,[t("mispredicted branch: "),a("code",null,"E_icode in {IJXX} && !e_Cnd")]),a("li",null,[t("exception: "),a("code",null,"m_stat in {SADR, SINS, SHLT} || W_stat in {SADR, SINS, SHLT}")])],-1),_=a("h3",{id:"procedure-control-signal"},[a("a",{class:"header-anchor",href:"#procedure-control-signal","aria-hidden":"true"},"#"),t(" Procedure Control Signal")],-1),v=a("p",null,"Branch, Loop, Jump:",-1),x=a("p",null,[t("PrectPC | "),a("code",null,"W_valM"),t("(无法预测) | "),a("code",null,"M_valP/M_valA"),t(" (在译码阶段合并信号量 valA 与 valP: PCUpdate 位于 Fetch,无需传递 valP, 只剩 call/jump 需要 valP)")],-1),S=a("ul",null,[a("li",null,"AT: always taken"),a("li",null,"NT: never taken"),a("li",null,"BTFNT: backward taken forward not taken")],-1),I=a("h3",{id:"forwarding"},[a("a",{class:"header-anchor",href:"#forwarding","aria-hidden":"true"},"#"),t(" Forwarding")],-1),L=a("p",null,"流水线中最早阶段的转发源, 优先级最高 execute > memory > write",-1),C=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),t(" d_valA "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),t("\n D_icode in "),a("span",{class:"token operator"},"{"),t("ICALL"),a("span",{class:"token punctuation"},","),t(" IJXX"),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},":"),t(" D_valP"),a("span",{class:"token punctuation"},";"),t("\n\n d_srcA "),a("span",{class:"token operator"},"=="),t(" e_dstE "),a("span",{class:"token punctuation"},":"),t(" e_valE"),a("span",{class:"token punctuation"},";"),t("\n d_srcA "),a("span",{class:"token operator"},"=="),t(" M_dstM "),a("span",{class:"token punctuation"},":"),t(" m_valM"),a("span",{class:"token punctuation"},";"),t("\n d_srcA "),a("span",{class:"token operator"},"=="),t(" M_dstE "),a("span",{class:"token punctuation"},":"),t(" M_valE"),a("span",{class:"token punctuation"},";"),t("\n d_srcA "),a("span",{class:"token operator"},"=="),t(" W_dstM "),a("span",{class:"token punctuation"},":"),t(" W_valM"),a("span",{class:"token punctuation"},";"),t("\n d_srcA "),a("span",{class:"token operator"},"=="),t(" W_dstE "),a("span",{class:"token punctuation"},":"),t(" W_valE"),a("span",{class:"token punctuation"},";"),t("\n\n # register file\n "),a("span",{class:"token number"},"1"),t(),a("span",{class:"token punctuation"},":"),t(" d_rvalA"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),E=a("h3",{id:"exception"},[a("a",{class:"header-anchor",href:"#exception","aria-hidden":"true"},"#"),t(" Exception")],-1),A=a("p",null,"流水线中最深的指令引起的异常, 优先级最高 e.g 访存阶段地址越界异常优先级高于取指阶段地址越界异常优先级",-1),N=a("h3",{id:"efficiency"},[a("a",{class:"header-anchor",href:"#efficiency","aria-hidden":"true"},"#"),t(" Efficiency")],-1),P=a("p",null,"CPI = 1.0 + lp + mp + rp:",-1),D=a("ul",null,[a("li",null,"lp: load penalty(load/use hazard)"),a("li",null,"mp: mispredicted branch penalty"),a("li",null,"rp: return penalty")],-1),M=a("h2",{id:"optimization"},[a("a",{class:"header-anchor",href:"#optimization","aria-hidden":"true"},"#"),t(" Optimization")],-1),z=a("h3",{id:"principles"},[a("a",{class:"header-anchor",href:"#principles","aria-hidden":"true"},"#"),t(" Principles")],-1),O=a("ul",null,[a("li",null,"合适的算法和数据结构"),a("li",null,"编写编译器能够有效优化的代码 e.g 寄存器别名,存储器别名,函数的副作用 导致编译器无法(不敢)合并/删除冗余代码"),a("li",null,"提高代码的并行性"),a("li",null,"消除不必要的访存 e.g 多次访存(可用中间量优化), 多次函数调用(可用宏/内联函数优化)")],-1),T=a("h3",{id:"tips"},[a("a",{class:"header-anchor",href:"#tips","aria-hidden":"true"},"#"),t(" Tips")],-1),B=a("h4",{id:"replacement"},[a("a",{class:"header-anchor",href:"#replacement","aria-hidden":"true"},"#"),t(" Replacement")],-1),U=a("ul",null,[a("li",null,"用多条 Shift/Add/Sub 指令, 代替 Mul/Div")],-1),G=a("h4",{id:"branch"},[a("a",{class:"header-anchor",href:"#branch","aria-hidden":"true"},"#"),t(" Branch")],-1),H=a("ul",null,[a("li",null,"利用条件表达式代替条件分支语句: 降低预测错误惩罚")],-1),R=a("h4",{id:"code-motion"},[a("a",{class:"header-anchor",href:"#code-motion","aria-hidden":"true"},"#"),t(" Code Motion")],-1),j=a("ul",null,[a("li",null,"将不变测试条件/常变量的计算从循环中移出"),a("li",null,"将多次访存利用中间自动/寄存器变量改写成一次访存")],-1),F=a("h4",{id:"unrolling-duff-s-device"},[a("a",{class:"header-anchor",href:"#unrolling-duff-s-device","aria-hidden":"true"},"#"),t(" Unrolling (Duff's Device)")],-1),W=a("p",null,"循环展开: 增大循环的步长 - Duff's Device 以 7 为步长:",-1),X=a("ul",null,[a("li",null,"提升循环的运行效率"),a("li",null,"一次循环内: 可先将所有数据先读出来(Memory State),将进行计算(Execute State), 从而消除 Load/Use 冒险而产生的 Bubble")],-1),K=a("h2",{id:"异常控制流"},[a("a",{class:"header-anchor",href:"#异常控制流","aria-hidden":"true"},"#"),t(" 异常控制流")],-1),J=a("p",null,"理解异常控制流,有助于理解以下概念:",-1),V=a("ul",null,[a("li",null,"陷阱(trap)/系统调用(system call)"),a("li",null,"系统级 I/O"),a("li",null,"线程/进程(concurrency)"),a("li",null,"虚拟存储器"),a("li",null,"软件异常")],-1),Z=a("h3",{id:"异常"},[a("a",{class:"header-anchor",href:"#异常","aria-hidden":"true"},"#"),t(" 异常")],-1),q=a("h4",{id:"分类"},[a("a",{class:"header-anchor",href:"#分类","aria-hidden":"true"},"#"),t(" 分类")],-1),Q=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"left"}},"类别"),a("th",{style:{"text-align":"left"}},"原因"),a("th",{style:{"text-align":"left"}},"异步/同步"),a("th",{style:{"text-align":"left"}},"返回行为"),a("th",{style:{"text-align":"left"}},"例子")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"left"}},"interrupt"),a("td",{style:{"text-align":"left"}},"输入/输出外部中断"),a("td",{style:{"text-align":"left"}},"async"),a("td",{style:{"text-align":"left"}},"next(concurrency)"),a("td",{style:{"text-align":"left"}},"磁盘")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"trap"),a("td",{style:{"text-align":"left"}},"主动异常/系统调用"),a("td",{style:{"text-align":"left"}},"sync"),a("td",{style:{"text-align":"left"}},"next"),a("td",{style:{"text-align":"left"}},"write/intN")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"fault"),a("td",{style:{"text-align":"left"}},"潜在可恢复的错误"),a("td",{style:{"text-align":"left"}},"sync"),a("td",{style:{"text-align":"left"}},"current/abort"),a("td",{style:{"text-align":"left"}},"seg/float exp")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"abort"),a("td",{style:{"text-align":"left"}},"不可恢复的错误"),a("td",{style:{"text-align":"left"}},"sync"),a("td",{style:{"text-align":"left"}},"abort(not return)"),a("td",{style:{"text-align":"left"}},"硬件错误")])])],-1),Y=a("h4",{id:"异常处理程序"},[a("a",{class:"header-anchor",href:"#异常处理程序","aria-hidden":"true"},"#"),t(" 异常处理程序")],-1),$=a("p",null,"异常处理程序主要分为 3 类:",-1),nn=a("ul",null,[a("li",null,"控制权返回给 Instruction_current"),a("li",null,"控制权返回给 Instruction_next"),a("li",null,"abort/exit")],-1),sn=a("h3",{id:"进程"},[a("a",{class:"header-anchor",href:"#进程","aria-hidden":"true"},"#"),t(" 进程")],-1),an=a("ul",null,[a("li",null,"一个独立的逻辑控制流(并行执行)"),a("li",null,"一个私有的地址空间(缓存与虚拟存储器)")],-1),tn=a("h4",{id:"上下文"},[a("a",{class:"header-anchor",href:"#上下文","aria-hidden":"true"},"#"),t(" 上下文")],-1),en=a("ul",null,[a("li",null,"代码/数据,堆/栈,通用寄存器,程序计数器,环境变量,文件描述符集合"),a("li",null,"上下文切换:用户模式与内核模式的切换"),a("li",null,"高速缓存污染(pollution): 每次切换后,总是会发生 cold cache miss")],-1),ln=a("h4",{id:"进程控制"},[a("a",{class:"header-anchor",href:"#进程控制","aria-hidden":"true"},"#"),t(" 进程控制")],-1),on=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),cn=a("h5",{id:"创建和终止进程"},[a("a",{class:"header-anchor",href:"#创建和终止进程","aria-hidden":"true"},"#"),t(" 创建和终止进程")],-1),pn=a("ul",null,[a("li",null,"父进程与子进程获得 2 份独立的私有空间与 2 份独立的上下文, 不同的 PID(process id)"),a("li",null,"由于指针(如打开的文件描述符),有可能互相影响,但大体上互不影响")],-1),un=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/*\n * output: parent: x=0\n * output: child: x=2\n * 独立上下文代表拥有独立的通用寄存器与栈,代表拥有拷贝的自动变量(局部变量),互不影响地进行修改\n */"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"int"),t(" x "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(),a("span",{class:"token comment"},"// child"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"child: x=%d\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"++"),t("x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// parent"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"parent: x=%d\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"--"),t("x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),rn=a("h5",{id:"回收子进程"},[a("a",{class:"header-anchor",href:"#回收子进程","aria-hidden":"true"},"#"),t(" 回收子进程")],-1),kn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),t(),a("span",{class:"token macro-name"},"N"),t(),a("span",{class:"token expression"},[a("span",{class:"token number"},"2")])]),t("\n\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" status"),a("span",{class:"token punctuation"},","),t(" i"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},"["),t("N"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t(" ret_pid"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" N"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("pid"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(),a("span",{class:"token comment"},"// child"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"100"),a("span",{class:"token operator"},"+"),t("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// parent reaps(回收) N children in order"),t("\n i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"while"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("ret_pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"waitpid"),a("span",{class:"token punctuation"},"("),t("pid"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("status"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"WIFEXITED"),a("span",{class:"token punctuation"},"("),t("statue"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"child: %d terminated normally with exit status=%d\\n"'),a("span",{class:"token punctuation"},","),t("\n ret_pid"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token function"},"WEXITSTATUS"),a("span",{class:"token punctuation"},"("),t("status"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"child %d terminated abnormally\\n"'),a("span",{class:"token punctuation"},","),t(" ret_pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// only if there are no more children, it can exit normally"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"!="),t(" ECHILD"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"waitpid error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// exit with error log"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// exit normally"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br")])],-1),bn=a("h3",{id:"信号"},[a("a",{class:"header-anchor",href:"#信号","aria-hidden":"true"},"#"),t(" 信号")],-1),dn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),mn=a("ul",null,[a("li",null,"一个只发出而未被处理的信号为待处理信号"),a("li",null,[t("一种类型至多有一个待处理信号, 多余待处理信号"),a("strong",null,"不会进入处理队列"),t(",只是"),a("strong",null,"被简单丢弃")]),a("li",null,"不可以用信号对其他事件进行计数, 同一事件多次发生产生的信号有可能被简单丢弃")],-1),hn=a("h4",{id:"处理信号"},[a("a",{class:"header-anchor",href:"#处理信号","aria-hidden":"true"},"#"),t(" 处理信号")],-1),fn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"handler"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" sig"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"waitpid"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(" "),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Handler reaped child %d\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),t("pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"!="),t(" ECHILD"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"waitpid error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"sleep"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" i"),a("span",{class:"token punctuation"},","),t(" n"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"char"),t(" buf"),a("span",{class:"token punctuation"},"["),t("MAX_BUF"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"signal"),a("span",{class:"token punctuation"},"("),t("SIGCHLD"),a("span",{class:"token punctuation"},","),t(" handler"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(" SIG_ERR"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"signal error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},";"),t(" I"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Hello from child %d\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token function"},"getpid"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"sleep"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// manually restart the READ call"),t("\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("n "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"read"),a("span",{class:"token punctuation"},"("),t("STDIN_FILENO"),a("span",{class:"token punctuation"},","),t(" buf"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),t("buf"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"!="),t(" EINTR"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"read error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Parent processing input\\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br")])],-1),gn=a("h4",{id:"阻塞信号"},[a("a",{class:"header-anchor",href:"#阻塞信号","aria-hidden":"true"},"#"),t(" 阻塞信号")],-1),yn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// how: SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK, 是否阻塞set中的信号合集"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigprocmask"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" how"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("old_set"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigemptyset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigfillset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigaddset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" sig_num"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigdelset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"int"),t(" sig_num"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"sigismember"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token class-name"},"sigset_t"),t(),a("span",{class:"token operator"},"*"),t("set, "),a("span",{class:"token keyword"},"int"),t(" sig_num"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),wn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"handler"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" sig"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"pid_t"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"waitpid"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"delete_job"),a("span",{class:"token punctuation"},"("),t("pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"!="),t(" ECHILD"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"waitpid error"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// 保证父进程先执行 add_job, 再执行 delete_job"),t("\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" argc"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),t("argv"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"int"),t(" pid"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"sigset_t"),t(" mask"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token function"},"signal"),a("span",{class:"token punctuation"},"("),t("SIGCHLD"),a("span",{class:"token punctuation"},","),t(" handler"),a("span",{class:"token punctuation"},";"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token function"},"init_job"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"sigemptyset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"sigaddset"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},","),t(" SIGCHLD"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"sigprocmask"),a("span",{class:"token punctuation"},"("),t("SIG_BLOCK"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// block SIGCHLD"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("pid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// unblock SIGCHLD in child, make it can transfer signal"),t("\n "),a("span",{class:"token function"},"sigprocmask"),a("span",{class:"token punctuation"},"("),t("SIG_UNBLOCK"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"execve"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"/bin/date"'),a("span",{class:"token punctuation"},","),t(" argv"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// parent process"),t("\n "),a("span",{class:"token function"},"add_job"),a("span",{class:"token punctuation"},"("),t("pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// after add_job, unblock SIGCHLD, make it can handle signal"),t("\n "),a("span",{class:"token function"},"sigprocmask"),a("span",{class:"token punctuation"},"("),t("SIG_UNBLOCK"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("mask"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br")])],-1),_n=a("h3",{id:"非本地跳转"},[a("a",{class:"header-anchor",href:"#非本地跳转","aria-hidden":"true"},"#"),t(" 非本地跳转")],-1),vn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),xn=a("ul",null,[a("li",null,"setjmp - catch: 返回多次"),a("li",null,"longjmp - throw: 不返回")],-1),Sn=a("h2",{id:"系统级-i-o"},[a("a",{class:"header-anchor",href:"#系统级-i-o","aria-hidden":"true"},"#"),t(" 系统级 I/O")],-1),In=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// robust I/O"),t("\n"),a("span",{class:"token class-name"},"ssize_t"),t(),a("span",{class:"token function"},"rio_read_n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" fd"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token operator"},"*"),t("usr_buf"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"size_t"),t(" n"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"size_t"),t(" n_left "),a("span",{class:"token operator"},"="),t(" n"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"ssize_t"),t(" n_read"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("buf_p "),a("span",{class:"token operator"},"="),t(" usr_buf"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("n_left "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("n_read "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"read"),a("span",{class:"token punctuation"},"("),t("fd"),a("span",{class:"token punctuation"},","),t(" buf_p"),a("span",{class:"token punctuation"},","),t(" n_left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"=="),t(" EINTR"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n n_read "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// interrupted by signal_handler, re-call read()"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("n_read "),a("span",{class:"token operator"},"=="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"break"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n n_left "),a("span",{class:"token operator"},"-="),t(" n_read"),a("span",{class:"token punctuation"},";"),t("\n buf_p "),a("span",{class:"token operator"},"+="),t(" n_read"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// remove data from buf_p"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"("),t("n "),a("span",{class:"token operator"},"-"),t(" left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n\n"),a("span",{class:"token class-name"},"ssize_t"),t(),a("span",{class:"token function"},"rio_write_n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" fd"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token operator"},"*"),t("usr_buf"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"size_t"),t(" n"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token class-name"},"size_t"),t(" n_left "),a("span",{class:"token operator"},"="),t(" n"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token class-name"},"ssize_t"),t(" n_written"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("buf_p "),a("span",{class:"token operator"},"="),t(" usr_buf"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("n_left "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("n_written "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"read"),a("span",{class:"token punctuation"},"("),t("fd"),a("span",{class:"token punctuation"},","),t(" buf_p"),a("span",{class:"token punctuation"},","),t(" n_left"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("errno "),a("span",{class:"token operator"},"=="),t(" EINTR"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n n_written "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// interrupted by signal_handler, re-call read()"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n n_left "),a("span",{class:"token operator"},"-="),t(" n_written"),a("span",{class:"token punctuation"},";"),t("\n buf_p "),a("span",{class:"token operator"},"+="),t(" n_written"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// remove data from buf_p"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" n"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br")])],-1),Ln=a("h3",{id:"socket-i-o"},[a("a",{class:"header-anchor",href:"#socket-i-o","aria-hidden":"true"},"#"),t(" socket I/O")],-1),Cn=a("h4",{id:"限制"},[a("a",{class:"header-anchor",href:"#限制","aria-hidden":"true"},"#"),t(" 限制")],-1),En=a("p",null,"输出函数+输入函数: 中间必须插入 fflush, fseek, fsetpos, rewind 输入函数+输出函数: 中间必须插入 fseek, fsetpos, rewind",-1),An=a("h4",{id:"i-o-函数的选择"},[a("a",{class:"header-anchor",href:"#i-o-函数的选择","aria-hidden":"true"},"#"),t(" I/O 函数的选择")],-1),Nn=a("ul",null,[a("li",null,"sprintf+rio_written: 格式化输出至套接口"),a("li",null,"rio_readlineb + sscanf: 格式化输入")],-1),Pn=a("h2",{id:"网络"},[a("a",{class:"header-anchor",href:"#网络","aria-hidden":"true"},"#"),t(" 网络")],-1),Dn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),t(),a("span",{class:"token string"},"")]),t("\n\n"),a("span",{class:"token keyword"},"int"),t(),a("span",{class:"token function"},"main"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" argc"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("argv"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),t("pp"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"struct"),t(),a("span",{class:"token class-name"},"in_addr"),t(" addr"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"struct"),t(),a("span",{class:"token class-name"},"hostent"),t(),a("span",{class:"token operator"},"*"),t("host_p"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("argc "),a("span",{class:"token operator"},"!="),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"usage: %s \\n"'),a("span",{class:"token punctuation"},","),t(" argv"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"inet_aton"),a("span",{class:"token punctuation"},"("),t("argv"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"&"),t("addr"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"!="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n host_p "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"gethostbyaddr"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token keyword"},"char"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),t("addr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),t("addr"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t(" AF_INET"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n host_p "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"gethostbyname"),a("span",{class:"token punctuation"},"("),t("argv"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"official hostname: %s\\n:"'),a("span",{class:"token punctuation"},","),t(" host_p"),a("span",{class:"token operator"},"->"),t("h_name"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("pp "),a("span",{class:"token operator"},"="),t(" host_p"),a("span",{class:"token operator"},"->"),t("h_aliases"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token operator"},"*"),t("pp "),a("span",{class:"token operator"},"!="),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},";"),t(" pp"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"alias: %s\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token operator"},"*"),t("pp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("pp "),a("span",{class:"token operator"},"="),t(" host_p"),a("span",{class:"token operator"},"->"),t("h_addr_list"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token operator"},"*"),t("pp "),a("span",{class:"token operator"},"!="),t(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},";"),t(" pp"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n addr"),a("span",{class:"token punctuation"},"."),t("s_addr "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"struct"),t(),a("span",{class:"token class-name"},"in_addr"),t(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"*"),t("pp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"->"),t("s_addr"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"address: %s\\n"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token function"},"inet_ntoa"),a("span",{class:"token punctuation"},"("),t("addr"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br")])],-1),Mn=a("h2",{id:"并发"},[a("a",{class:"header-anchor",href:"#并发","aria-hidden":"true"},"#"),t(" 并发")],-1),zn=a("h3",{id:"锁"},[a("a",{class:"header-anchor",href:"#锁","aria-hidden":"true"},"#"),t(" 锁")],-1),On=a("p",null,"防止死锁: 每对互斥锁(s, t), 每个线程顺序请求锁, 逆序释放锁",-1),Tn=a("h2",{id:"调试-测试"},[a("a",{class:"header-anchor",href:"#调试-测试","aria-hidden":"true"},"#"),t(" 调试/测试")],-1),Bn=a("h3",{id:"日志"},[a("a",{class:"header-anchor",href:"#日志","aria-hidden":"true"},"#"),t(" 日志")],-1),Un=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"unix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("msg"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"%s: %s\\n"'),a("span",{class:"token punctuation"},","),t(" msg"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token function"},"strerror"),a("span",{class:"token punctuation"},"("),t("errno"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"posix_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),t(" code"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("msg"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"%s: %s\\n"'),a("span",{class:"token punctuation"},","),t(" msg"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token function"},"strerror"),a("span",{class:"token punctuation"},"("),t("code"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"dns_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("msg"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"%s: DNS error %d\\n"'),a("span",{class:"token punctuation"},","),t(" msg"),a("span",{class:"token punctuation"},","),t(" h_errno"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"void"),t(),a("span",{class:"token function"},"app_error"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),t(),a("span",{class:"token operator"},"*"),t("msg"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"fprintf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"stderr"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},'"%s\\n"'),a("span",{class:"token punctuation"},","),t(" msg"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1);l.render=function(a,t,e,l,Gn,Hn){return n(),s("div",null,[o,c,p,u,r,i,k,b,d,m,h,f,g,y,w,_,v,x,S,I,L,C,E,A,N,P,D,M,z,O,T,B,U,G,H,R,j,F,W,X,K,J,V,Z,q,Q,Y,$,nn,sn,an,tn,en,ln,on,cn,pn,un,rn,kn,bn,dn,mn,hn,fn,gn,yn,wn,_n,vn,xn,Sn,In,Ln,Cn,En,An,Nn,Pn,Dn,Mn,zn,On,Tn,Bn,Un])};export default l;export{e as __pageData}; diff --git a/assets/computerScience_operatingSystems_operatingSystemsBasicNotes.md.5966c145.js b/assets/computerScience_operatingSystems_operatingSystemsBasicNotes.md.5966c145.js new file mode 100644 index 00000000000..4ca30070d7b --- /dev/null +++ b/assets/computerScience_operatingSystems_operatingSystemsBasicNotes.md.5966c145.js @@ -0,0 +1 @@ +import{o as n,c as a,a as s}from"./app.6dd2a1b7.js";const e='{"title":"Operating System Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"OS Definition","slug":"os-definition"},{"level":2,"title":"GCC 内联汇编","slug":"gcc-内联汇编"},{"level":2,"title":"基本概念","slug":"基本概念"},{"level":3,"title":"操作系统的特性","slug":"操作系统的特性"},{"level":3,"title":"操作系统的资源管理功能","slug":"操作系统的资源管理功能"},{"level":3,"title":"操作系统的演变","slug":"操作系统的演变"},{"level":3,"title":"操作系统虚拟机","slug":"操作系统虚拟机"},{"level":3,"title":"操作系统的组织结构","slug":"操作系统的组织结构"},{"level":3,"title":"并发","slug":"并发"},{"level":2,"title":"启动","slug":"启动"},{"level":3,"title":"BIOS","slug":"bios"},{"level":3,"title":"启动顺序","slug":"启动顺序"},{"level":2,"title":"物理内存管理","slug":"物理内存管理"},{"level":3,"title":"Boot Loader 探测机器内存分布","slug":"boot-loader-探测机器内存分布"},{"level":3,"title":"Boot Loader 基本概念","slug":"boot-loader-基本概念"},{"level":3,"title":"连续内存分配","slug":"连续内存分配"},{"level":3,"title":"非连续内存分配","slug":"非连续内存分配"},{"level":3,"title":"内存的特权级","slug":"内存的特权级"},{"level":2,"title":"虚拟内存管理","slug":"虚拟内存管理"},{"level":3,"title":"Page Fault","slug":"page-fault"},{"level":3,"title":"覆盖与交换","slug":"覆盖与交换"},{"level":3,"title":"虚拟页式存储管理","slug":"虚拟页式存储管理"},{"level":2,"title":"中断","slug":"中断"},{"level":3,"title":"中断进入","slug":"中断进入"},{"level":3,"title":"中断实现","slug":"中断实现"},{"level":3,"title":"系统调用","slug":"系统调用"},{"level":2,"title":"进程","slug":"进程"},{"level":3,"title":"进程特权级","slug":"进程特权级"},{"level":3,"title":"进程状态与生命周期","slug":"进程状态与生命周期"},{"level":3,"title":"进程通信","slug":"进程通信"},{"level":3,"title":"线程","slug":"线程"},{"level":3,"title":"内核线程与用户进程","slug":"内核线程与用户进程"},{"level":3,"title":"Process 实现","slug":"process-实现"},{"level":2,"title":"处理机调度","slug":"处理机调度"},{"level":3,"title":"调度时机","slug":"调度时机"},{"level":3,"title":"调度策略","slug":"调度策略"},{"level":2,"title":"同步互斥","slug":"同步互斥"},{"level":3,"title":"临界区的访问原则","slug":"临界区的访问原则"},{"level":3,"title":"基于软件方法的同步互斥","slug":"基于软件方法的同步互斥"},{"level":3,"title":"高级抽象的同步互斥","slug":"高级抽象的同步互斥"},{"level":3,"title":"死锁","slug":"死锁"},{"level":3,"title":"Mutex 实现","slug":"mutex-实现"},{"level":2,"title":"文件系统","slug":"文件系统"},{"level":3,"title":"文件组成","slug":"文件组成"},{"level":3,"title":"文件系统基本数据结构","slug":"文件系统基本数据结构"},{"level":3,"title":"文件分配","slug":"文件分配"},{"level":3,"title":"空闲空间管理","slug":"空闲空间管理"},{"level":3,"title":"冗余磁盘阵列","slug":"冗余磁盘阵列"},{"level":3,"title":"FS 实现","slug":"fs-实现"},{"level":3,"title":"ELF File Format","slug":"elf-file-format"},{"level":2,"title":"设备管理详解","slug":"设备管理详解"},{"level":3,"title":"CGA EGA and ChromaText Video Buffer","slug":"cga-ega-and-chromatext-video-buffer"},{"level":3,"title":"Input and Output","slug":"input-and-output"},{"level":2,"title":"实践","slug":"实践"},{"level":3,"title":"工具","slug":"工具"},{"level":2,"title":"Memory Management Tools","slug":"memory-management-tools"},{"level":3,"title":"perf","slug":"perf"},{"level":3,"title":"trace","slug":"trace"},{"level":3,"title":"vmstat","slug":"vmstat"},{"level":3,"title":"pmap","slug":"pmap"},{"level":3,"title":"Memory Hot Plug","slug":"memory-hot-plug"},{"level":3,"title":"TCMalloc","slug":"tcmalloc"},{"level":3,"title":"Oprofile","slug":"oprofile"}],"relativePath":"computerScience/operatingSystems/operatingSystemsBasicNotes.md","lastUpdated":1627194884000}',p={},t=s('

Operating System Basic Notes

OS Definition

操作系统是一个大型的程序系统, 它负责(处理机管理, 存储管理, 设备管理, 文件系统):

  • 计算机系统软、硬件资源的分配和使用
  • 控制和协调并发活动
  • 提供用户接口, 使用户获得良好的工作环境

GCC 内联汇编

asm ( assembler template // assembly language\n  :=output operands // 约定输出\n  :input operands // 约定输入\n  :clobbers // 约定插入\n);\n
1
2
3
4
5

constraints:

  • m/v/o = memory
  • r = register
  • Q = ea/b/c/dx
  • a = eax
  • b = ebx
  • c = ecx
  • d = edx
  • D = edi
  • S = esi
  • 0/n = first/nth constraints

基本概念

操作系统的特性

  • 并发性 : 能处理多个同时性活动的能力
  • 共享性 : 多个计算任务对系统资源的共同享用
  • 不确定性: 操作系统能处理随机发生的多个事件 - 程序运行次序的不确定性, 程序运行时间的不确定性

操作系统的资源管理功能

处理器调度

  • 确定进程调度策略
  • 给出进程调度算法
  • 进行处理机的分派

存储器管理

  • 存储分配与存储无关性

为用户提供逻辑地址, 解决主存分配问题

  • 存储保护

实现系统程序与用户程序之间的隔离, 实现不同用户程序之间的隔离

  • 存储扩充

虚拟内存管理: 主存+磁盘, 为每个进程管理一个虚拟内存映射链表

设备管理

  • 设备无关性

用户向系统申请和使用的设备无关实际操作的设备, 操作系统为用户提供统一的逻辑设备(名)

  • 设备分配(独享分配/共享分配/虚拟分配)
  • 设备的传输控制(设备启动处理, 设备中断处理, 设备结束处理)

组织设备完成 I/O 操作, 并正确处理中断

文件资源管理

为用户提供一种简便、统一的存取和管理信息的方法, 解决信息的共享/数据的存取控制/数据的保密等问题:

  • 实现用户的信息组织
  • 提供存取方法
  • 实现文件共享
  • 保证文件安全
  • 保证文件完整性
  • 完成磁盘空间分配

操作系统的演变

  • 单用户系统(45-55) -> 批处理系统(55-65) -> 多道系统(65-80) -> 分时系统(70-) -> 分布式系统
  • 手工系统 -人机矛盾-> 联机批处理系统 -CPU I/O 矛盾 -> 脱机批处理系统 -响应能力-> 执行系统(中断/通道) -并行-> 多道批处理系统(粗粒度) -> 分时系统(细粒度) -> 实时系统 -> 个人/网络/分布式系统

批处理系统

作业成批送入计算机, 然后由作业调度程序自动选择作业, 在系统内(多道)运行

  • 系统吞吐率高: 脱机/多道运行
  • 作业周转时间长, 用户使用不方便, 缺少交互性

分时系统

采用时间片(time slice)轮转(round robin)的方法, 使计算机同时为多个终端用户服务, 保证对每个用户都有足够快的响应时间, 并提供交互会话功能

  • 并行性
  • 独占性
  • 交互性

单处理器系统: 处理器与设备/处理器与通道/通道与通道/设备与设备可以同时刻并行(真正意义上的同时进行)

实时系统

实时系统对外部输入的信息, 能够在规定时间内处理完毕并作出反应(实时控制/实时信息处理) e.g 嵌入式操作系统

  • 可靠性
  • 安全性
  • 及时响应

操作系统虚拟机

  • 在裸机上配置了操作系统后便构成了操作系统虚拟机
  • 裸机的指令系统: 机器指令; 操作系统虚拟机的指令系统: 系统调用

用户接口

  • 操作/命令接口(操作命令): 作业控制语言/键盘命令(CLI)/图形化用户界面(GUI)
  • 程序接口(系统功能调用): 在用户程序中可以直接使用系统功能调用(system call)请求操作系统提供的服务

操作系统的组织结构

  • 一体化结构
  • 模块化结构
  • 可扩展内核(微内核)结构
  • 层次化结构

并发

Concurrent

程序并发的特点

  • 程序执行的间断性
  • 相互通信的可能性
  • 资源分配的动态性

启动

BIOS

  • 基本输入输出程序
  • 系统设置信息
  • 开机后自检程序
  • 系统自启动程序

启动顺序

寄存器

  • CF: 初值 F000H
  • EIP: 初值 FFF0H

(FFF)F0000H+FFF0H = FFFFFFF0H, BIOS 的 EPROM(Erasable Programmable Read Only Memory) 处 加电后第一条指令一般是 ljmp(实模式下, 内存 !MB), 跳转地址为 CF<<4+EIP, 跳转至 BIOS 例行程序起始点.

BIOS Config

BIOS 根据设置(硬盘/U 盘/网络启动), 加载存储设备的主引导扇区(Master Boot Record)(第一个扇区)的 512 字节至内存 0x7c00 处, 开始执行第一条指令(Boot Loader)

Boot Loader

实模式与保护模式带来的问题:

  • 在实模式的寻址模式中, 令物理地址为 16 位段寄存器左移 4 位加 16 位逻辑地址的偏移所得的 20 位地址
  • 若要访问 1MB 之后的内存, 则必须开启 A20 Line 开关(关闭 wrap around), 将 32 位地址总线打开, 并进入保护模式(Protect Mode)
  • 在实模式中, 0~4KB 为中断向量表保留, 640KB ~ 1MB 为显存与 BIOS 保留, 实际可用的内存只有 636KB
  • 考虑到日后内核镜像的体积有超过 1MB 的可能, 所以将其装载到物理地址 1MB(0x100000) 之后连续的一块内存中更好.
  • 若要装载内核到物理地址 1MB 之后(实模式下无法访问), 可在实模式中暂时将其装载到一个临时位置, 待进入保护模式之后再移动至合适位置

解决方案:

  • 将内核镜像装入内存临时地址
  • 开启保护模式
  • 移动内核镜像至 1MB 之后合适位置
  • 跳转至内核入口(jmp addr 用以修改 cs:eip)
标志

lab1/tools/sign.c:

  • 有效字节小于 510 bytes
  • 结尾为 0x55aa
  • 总计字节小于 512 bytes
基本功能
  • 切换到保护模式, 启动段机制
  • 通过 8042 键盘控制器的端口, 开启 A20, 关闭 memory wrap around, 获取足够内存空间
; 键盘控制器的命令\n; 0xD0 Read Output Port\n; 0xD1 Write Output Port\n; 0xDD Enable A20 Address Line\n; 0xDF Disable A20 Address Line\n; 0x60 - 数据端口, 0x64 - 命令端口\ncall  empty_8042\nmov   al,0xd1\nout   0x64,al\ncall  empty_8042\nmov   al,0xdf\nout   0x60,al\ncall  empty_8042\n\nempty_8042:\n    dw    00ebh, 00ebh\n    in    al,64h\n    test  al,2\n    jnz   empty_8042\n    ret\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • 置 cr0 保护模式标志位(bit0) 为 1

  • 加载全局描述符表

  • 设置各个通用寄存器与段寄存器

  • 从硬盘上加载 某种(kernel in ELF) 格式的 os kernel(在硬盘中紧邻 MBR) 至内存的固定区域

  • 跳转到 os kernel 的入口点(entry point), 转移控制权至 os

保护模式与段机制
  • CS -> 全局描述符表(其起始地址与表大小位于 gdt 寄存器中)某项(每项存有 base/limit 等信息) -> 局部描述符表 -> 段选择子(段的基本信息) -> 基址+EIP -> 线性地址 ---页机制---> 物理地址
  • 将 cr0 寄存器 bit0 置为 1, 表示进入了保护模式, 段机制开始起作用

物理内存管理

Boot Loader 探测机器内存分布

为内存管理模块提供基础: 在进入实模式前, 调用 int 15h(88h, e801h, e820h), 借助 BIOS 中断获取内存信息

Boot Loader 基本概念

Boot Loader 基本目标

  • 抽象: 逻辑地址空间(线性物理地址映射)
  • 保护: 独立地址空间(进程间互不影响)
  • 共享: 访问相同内存(内核空间与共享库)
  • 虚拟化: 独占内存空间假象

基本管理方式

  • 重定位(relocation)
  • 分段(segmentation): 代码段/数据段
  • 分页(paging)
  • 虚拟存储(virtual memory): 内存视作硬盘的缓存, 硬盘视作虚拟内存

地址生成

地址生成时机
  • 编译时
  • 链接时
  • 加载时
  • 执行时(相对寻址)

地址映射

软硬件结合:

逻辑地址 ---> 物理地址

  • 硬件(CPU/MMU)完成映射地址
  • 操作系统建立映射规则(页表)

地址检查

软硬件结合:

  • 操作系统设置的段机制和段长度
  • 硬件(CPU/MMU)根据段信息进行地址检查(内存访问是否异常)

连续内存分配

  • malloc
  • free

内存碎片

  • 外部碎片: 已分配单元间无法利用空闲单元(请求内存单元过大)
  • 内部碎片: 已分配单元内空闲单元

动态分配策略

  • Fist-fit(最先匹配): 使用第一个可利用空闲块 - 容易产生外部碎片, 分配大块效率低
  • Best-fit(最佳匹配): 使用利用度最高的空闲块 - 外部碎片过小, 释放时需重新排列空闲块链表(升序)
  • Worst-fit(最差匹配): 使用利用度最差的空闲块 - 分配中块效率高, 释放时需重新排列空闲块链表(降序)

碎片整理策略

ucore 实现

  • 连续存放 n 个 page 结构, 形式表示内存页, 并在连续内存块的首页(header page)保存此连续块的连续页数目
  • 维护一个链表, 链表每项为大块连续内存块的起始页(header page address) 和 连续页数目, 管理分散的连续内存块
紧凑

Compaction, 将不同进程占用内存单元移至较为集中的地方:

  • 只可移动 可动态重定位程序
  • 只可移动 等待状态进程
分区对换

Swapping In and Swapping Out:

将等待状态进程的分区对换至外存,以增大可用内存单元

e.g Linux Swap 分区: 安装系统时一般切割大小为内存大小的 50%~100% 的外存作为 Swap 分区

Malloc 实现策略

启发式编程

Heuristic Programming:

  • 建立已分配 void 指针表,free 函数执行时,只回收表中存在的指针;不存在则报错
  • 对 heap 进行分区 - 小/中/大块内存请求,分别从不同区域(8/16/32 最小单位区)分配
  • 记录当前堆块的信息,如长度,空闲状态
  • 记录周围环境信息,如保留上/下一堆块的指针或记录上/下堆块空闲状态
伙伴系统

Buddy System:

  • 可分配内存单元总大小为 2^n
  • 总是将大小 小于请求大小的2 倍且为2 的幂次方 的某块内存单元分配出去(大小为 2^(i-1))
合并空闲块
  • 空闲块相邻
  • 空闲块等大
  • 低地址空闲块的 起始地址 必须为 空闲块大小 的 2 的幂次方倍(2 倍以上)

非连续内存分配

  • 支持一个程序使用非连续的物理地址空间
  • 支持共享代码与数据
  • 支持动态加载与动态链接

段式存储管理

将逻辑地址划分, 低位表示段内偏移, 高位(取摸)表示段号(类比缓存中的内存地址)

GDT
typedef struct gdt_ptr {\n    uint16_t gdt_limit;     // gdt_length - 1\n    uint32_t gdt_base;\n} __attribute__((packed)) gdt_ptr_t;\n
1
2
3
4
  • 用 16 位来表示表的长度(2 ^ 16 = 65532 bytes), 除以每一个描述符的 8 字节, 最多能创建 8192 个描述符

页式存储管理

开启页机制:

  • init page directory(首址位于 cr3 寄存器), page table
  • update GDT
  • update ds,es,ss
  • update cs with jmp instruction
  • cr0 寄存器 bit31(most bit) 置 1
虚拟地址

TLB (Translation Lookaside Buffer in CPU/PM)

  • Virtual Address = 2^(bits of virtual page offset) * virtual page number + virtual page offset
  • VPN(virtual page number point to PPN) - VPO(virtual page offset = PPO)
  • 根据 VPN 在页表中找到对应表项(VPN 表示项号), 每项保存着 PPN
  • TLBT(tag) - TLBI(index) - VPO
  • 因为内存局部性原理, TLB 一般只需要很小(比如 64 项)即可达到不错的效果
物理地址

C(cache) PPO = VPO

  • Page Frame(帧): 高位为帧号, 低位为偏移
  • Physical Address = 2^(bits of physical page offset) * physical page number/page frame number + physical page offset
  • PPN(physical page number) - PPO(physical page offset = VPO)
  • CT(tag) - CI(index) - CO(offset)
页表
  • 页表由操作系统建立, 硬件(CPU/MMU)根据页表信息将虚拟地址映射为物理地址
页表结构
  • FN/PPN
  • 标志位: resident bit(存在位)/dirty bit(修改位)/reference(clock) bit(引用位)
性能问题
  • 两次访存: 第一次获取页表项, 第二次访问实际数据
  • 页表占据大量内存单元
TLB

Translation Lookaside Buffer:

缓存页表项 - key: VPN, value: PPN  不用访问页表

多级页表
  • 切割页表: 建立子页表
  • Page Directory -> Page Table -> Physical Address
  • 将线性地址分成三部分 Directory+Table+Offset: cr3+Dir 取出 page table base, ptb+Tab 取出 physical address base, pab+offset = pa

CR3 寄存器: 保存一级页表的基址

  • 父页表表项保存子页表起始地址, 逻辑地址某部分保存偏移地址(子表项号)
  • 存在位为 0 时, 不用保存子表, 节省内存单元
反置页表
  • PPN 作为页表索引, 页表项保存 VPN(或者 Hash(VPN|PID))
  • 将 VPN 映射为 PPN 时, 需遍历整个页表(但此页表只占用少量内存单元)

段页式存储管理

在页式存储管理基础上, 引入段式存储管理

<--- vsn --- vpn --- vpo ---> 映射为 <--- ppn --- ppo --->

sn: segment number, pn:page number, po: page offset

若以 4K(limit) 为 1 个页表大小, 则下级页表首址为 段/页表中某项的值 << 12(2^12 = 4K)

  • 以 vsn 为索引在进程段表中找到段表项, 获取段(页表)基址与段大小信息(item_value/limit): base = item_value << log2(limit)
  • 以 vpn 为索引在进程页表(页表基址 = base 中找到页表项, 获取 ppn
  • ppn << log2(limit) + vpo(ppo) 为实际物理地址

内存的特权级

0: 最高特权级, 3: 最低特权级

特权级检查

  • CPL/RPL: 访问者特权级
  • DRL: 段描述符/门描述符(中断/陷阱门)中保存的特权级, 表示被访问段/中断服务/陷阱的特权级
  • CPL/RPL <= DRL e.g 0 < 3

特权级切换

Ring 0 to Ring 3
  • interrupt/trap: push SS(RPL=3) -> ESP -> EFLAGS -> CS(RPL=3) -> EIP -> Error Code
  • iret: pop above variables, move to ring 3
Ring 3 to Ring 0

特权级提升:

  • interrupt/trap: stack switch
  • push EFLAGS -> CS(RPL=0) -> EIP -> Error Code
  • iret: pop above variables, move to ring 0

TSS

Task State Segment:

保存不同特权级的堆栈信息(SS/ESP)

全局描述符表中保存一个 TSS Descriptor(TSS base + TSS limit): allocate TSS memory -> init TSS -> fill TSS descriptor in GDT -> set TSS selector(task register)

虚拟内存管理

虚拟内存 = 物理内存 + 外存

Page Fault

  • 虚拟地址越界: 访问不存在的虚拟地址
  • 对只读地址进行写操作
  • 访问未映射虚拟页(swap in/out)
  • CPU 将产生异常的的线性地址(linear address) 存储在 CR2 寄存器中, 将 errorCode(bit2-访问权限异常,bit1-写异常,bit0-物理页不存在)压入中断栈

CR0: 处理器模式(实/保护/分段/分页模式); CR2: Page Fault Linear Address; CR3: Page-Directory Base Address Register

覆盖与交换

覆盖技术

Overlay:

将程序中相互独立的模块分成一组, 为每组按最大模块分配内存单元

交换技术

Swap:

将挂起进程的整个地址空间换出至外存(swap out), 将需用进程的整个地址空间换入至内存(swap in), 进程交替运行

虚拟页式存储管理

  • 只将运行进程所必需页面装入内存, 其余页面至于外存
  • 进程发生缺页异常时, 将所要求缺页装入内存: 选择目标物理页面 -> 无未占用物理页面, 则换出闲置物理页面(访问位) -> 装入物理内存,更新页表项 (换入逻辑地址与换出逻辑地址对应的页表项的驻留位/修改位/访问位/锁定位以及物理页号 ppn)
  • 监控已经装入内存的页面, 及时将不需要页面换出至外存

标志位

  • 驻留位: 此逻辑地址对应的页面在内存中
  • 修改位: 此页面是否被修改过, 判定此页面换出时策略(写回外存/直接丢弃)
  • 访问位: 此页面是否被读/写过, 判定此页面是否需要换出至外存
  • 锁定位: 此页面不会被换出

页面置换算法

当出现缺页异常且物理内存已满时, 需要以页面置换算法为指导, 换出闲置物理页面, 换入所要求缺页, 并更新页表项:

  • 尽可能减少物理页面的换入换出次数
  • 只可交换映射到用户空间的物理页
  • 当页表项中 PTE_P 为 0 时, 对应高位地址表示扇区地址(而不是物理位移)
  • 换入时机: Page Fault 缺页; 换出时机: 积极/消极换出策略
局部置换算法

换入进程 A 的某个页面时, 只可换出进程 A 的某个页面: 为进程分配固定数目的页面

最远未用算法

Least Recently Used (LRU) Algorithm:

  • 利用链表等线性结构维护页面访问时间, 队首为最近一次访问页面(每次访问一个页面后, 把它从线性结构中间抽出至队首), 队尾为最远一次访问页面
时钟算法

Clock Algorithm:

  • 利用循环链表维护页面(指针指向最先换入内存的页面), 页表项访问位维护访问记录(访问后将访问位置 1)
  • 缺页时, 从指针处开始查找页面进行置换: 若访问位为 0, 则进行置换; 若访问位为 1, 则将此页面访问位置 0, 继续查找未访问页面
  • 改进: 增加修改位
指针扫描前指针扫描后
访问位 修改位访问位 修改位
0 0置换
0 10 0
1 00 0
1 10 1
最不常用算法

Least Frequently Used (LFU) Algorithm:

  • 利用链表等线性结构维护页面访问时间, 队首为最多访问次数页面(每次访问一个页面后, 访问次数+1, 并排序), 队尾为最少访问次数页面
全局置换算法
  • 换入进程 A 的某个页面时, 可换出其他进程的某个页面: 为进程分配可变数目的页面
  • 关键: 确定为不同进程分配的页面数目
  • 抖动(thrashing): 进程过多, 导致大部分进程的常驻集<工作集, 缺页率较高
工作集算法
  • 工作集(随时间动态变化集): 一个进程当前正在使用的逻辑页面集合, WorkingSet(currentTime, workingSetWindow(访问时间窗口)) (cT - wSW, cT + wSW)
  • 根据工作集大小(逻辑页面集合), 为该进程分配常驻集(合适数目的物理页面集合): 若常驻集 > 工作集, 则缺页率较低
  • 再额外维护一个类希 LRU 算法中的访存链表, 记录近期访问过的物理页面, 将其也加入常驻集
缺页率算法

当缺页频繁时, 缺页时将缺页的页面加入常驻集; 当缺页不频繁时, 缺页时将不属于工作集的页面移出常驻集

实现

  • 为每个进程分配一个 vma 块, 模拟一个完整的物理内存
  • vmas 按起始地址从小至大形成一个双向链表, 且地址空间没有任何交集
#define VM_READ  0x00000001\n#define VM_WRITE 0x00000002\n#define VM_EXEC  0x00000004\n#define VM_USER  (VM_READ | VM_WRITE | VM_EXEC)\n\ntypedef struct __mm {\n    list_entry_t mmap_list; // header of vmas' list\n    vma *mmap_cache;        // current accessed vma(used for speed purpose)\n    pde_t *pgdir;           // PDT for vmas\n    int map_count;          // count of vmas\n    void *sm_priv;          // private data for swap manager\n} mm;\n\ntypedef struct __vma {\n    struct mm *vm_mm;       // all vmas use the same PDT(page directory table)\n    uintptr_t vm_start;     // start address of vma (align to PGSIZE)\n    uintptr_t vm_end;       // end address of vma   (align to PGSIZE)\n    uint32_t vm_flags;      // flags of vma\n    list_entry_t vma_list;  // doubly linked list: sort all vmas by start address\n} vma;\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

中断

Interrupt Service Routine/Interrupt Quest:

  • NMI 中断(Non Maskable Interrupt) 与 INTR 中断(可屏蔽中断)
  • x86PC 中断控制芯片: 8259A PIC

中断进入

保护现场

  • push PSW(Program State Word): (cs:eip + eflags)
  • push PC(Program Counter)
  • 中断向量表(将中断向量表对应的中断的 PSW(高 2 字节) 与 PC(低 2 字节) 先后替换原 PSW 与 PC)
  • 系统堆栈
  • 中断屏蔽码: n 位 2 进制数(n 为中断总数), 允许响应 n 号中断则该位置 1

程序状态字:

  • 当前执行指令(eip)
  • 当前指令执行情况
  • 处理机所处状态
  • 中断屏蔽码(程序在执行时应屏蔽的中断)
  • 寻址方法/编址/保护键
  • 响应中断的内容

中断实现

概述

  • 硬件发生了某个事件后告诉中断控制器(PIC), 中断控制器汇报给 CPU
  • CPU 从中断控制器处获取中断号, 根据中断号调用对应中断服务例程
  • 处理完成后重新回到之前的执行流程

Interrupt 实现

  • 发生中断, PIC 报告中断号给 CPU
  • CPU 调用对应处理程序 irsr_x/irq_x
  • irsr_x/irq_x 负责压入相关信息(中断号/错误码), 然后跳转至统一处理函数 common_stub
  • common_stub: 压栈 -> 调用 fault_handler/req_handler -> 出栈
  • 进入 handler 后, 再根据中断号/错误码(结构体)以及栈帧信息(结构体), 进行实际处理(真正处理逻辑)
  • 在进入 handler 之前, 都是通过汇编代码进行最简单的处理(压入相关信息), 将实际中断处理逻辑放在 C 语言中, 再辅以内联汇编, 可大大地提升中断处理程序的编写效率以及中断处理程序的处理能力

系统调用

  • 保护系统安全, 提升可靠性与安全性
  • 调用: 用户态, 执行: 管态/内核态

进程

资源分配单位:

  • 独立性: 无副作用(确定性), 可重现
  • 并发性(宏观并行, 微观串行): 提升效率, 共享资源, 高度模块化

进程特权级

  • 处理机的态/处理机的特权级: 根据对资源和机器指令的使用权限, 将处理执行时的工作状态区分为不同的状态
  • 管理(supervisor mode)/系统态: 使用全部机器指令(包括特权指令), 可使用所有资源, 允许访问整个内存区, 运行系统程序
  • 用户态: 禁止使用特权指令(I/O 设备指令, 直接修改特殊寄存器指令, 改变机器状态指令), 不可直接取用资源与改变机器状态, 只可访问自己的存储区域, 运行用户程序
  • 用户态切换至管态: 错误/异常状态(除 0/缺页), 外部中断(I/O), 系统调用, 这一过程是由硬件完成

进程状态与生命周期

创建, 就绪(ready), 运行(running), 等待(wait/sleeping), 挂起(suspend: 进程由内存换出至外存), 结束(抢占, 唤醒): 进程优先级与剩余内存单元在一定程度上会影响进程状态

  • 进程首先在 cpu 初始化或者 sys_fork 的时候被创建,当为该进程分配了一个进程控制块之后,该进程进入 uninit 态
  • 当进程完全完成初始化之后,该进程转为 runnable 态
  • 当到达调度点时,由调度器 sched_class 根据运行队列 rq 的内容来判断一个进程是否应该被运行, 即把处于 runnable 态的进程转换成 running 状态,从而占用 CPU 执行
  • running 态的进程通过 wait 等系统调用被阻塞,进入 sleeping 态
  • sleeping 态的进程被 wake up 变成 runnable 态的进程
  • running 态的进程主动 exit 变成 zombie 态, 然后由其父进程完成对其资源的最后释放,子进程的进程控制块成为 unused
  • 所有从 runnable 态变成其他状态的进程都要出运行队列(dequeue), 反之,被放入某个运行队列中(enqueue)

进程控制块

Process Control Block:

通过组织管理 PCB(链表/索引表) 来组织管理进程; 在进程创建/终止的同时, 生成/回收改进程的 PCB:

  • 进程信息: 名字/pid/uid
  • 链表信息: 父进程指针/所属队列指针(就绪队列/IO 等待队列/挂起队列)
  • CPU 调度/运行时信息: eflags/cr3/状态
  • 内存资源信息: 堆指针/栈指针/虚拟内存页面指针
  • 上下文信息(用于进程/上下文切换时保存/恢复上下文): trap frame/context(register files)

进程通信

  • 直接通信: send(proc, msg), receive(proc, msg) shmctl() shm*
  • 间接通信: send(msg_que, msg), receive(msg_que, msg) msgctl() msg*

线程

CPU 调度单位:

  • 进程缺陷: 共享数据不便, 系统开销大
  • 线程共享段表/共享库/数据/代码/环境变量/文件描述符集合/地址空间, 但拥有独立的堆/栈/通用寄存器
  • 线程控制块(Thread Control Block)
  • 用户线程与内核线程: 多为 1 对 1

IDLE Process

0 号内核线程:

  • 工作就是不停地查询,直至有其他内核线程处于就绪状态, 令调度器执行那个内核线程
  • IDLE 内核线程是在操作系统没有其他内核线程可执行的情况下才会被调用
  • 在所有进程中,只有 IDLE (内核创建的第一个内核线程) 没有父进程

内核线程与用户进程

  • 内核线程只运行在内核态
  • 用户进程会交替运行在用户态和内核态(系统调用/外设中断/异常中断)
  • 所有内核线程直接使用共同的内核内存空间, 拥有相同的内核虚拟地址空间(包括物理地址空间)
  • 每个用户进程拥有单独的用户内存空间(虚拟内存单元)

Process 实现

Process Context

执行现场/执行上下文:

  • 设置好执行现场后, 一旦调度器选择了 initproc 执行, 就需要根据 initproc->context 中保存的执行现场来恢复 initproc 的执行
  • 通过 proc_run 和进一步的 switch_to 函数完成两个执行现场的切换,具体流程如下:
    • 让 current 指向 next 内核线程 initproc
    • 设置任务状态段 ts 中特权态 0 下的栈顶指针 esp0 为 next 内核线程 initproc 的内核栈的栈顶, 即 next->kstack + KSTACKSIZE
    • 设置 CR3 寄存器的值为 next 内核线程 initproc 的页目录表起始地址 next->cr3
    • 由 switch_to 函数完成具体的两个线程的执行现场切换, 即切换各个寄存器

Process Fork Function

  • 分配并初始化进程控制块(alloc_proc 函数)
  • 分配并初始化内核栈(setup_stack 函数)
  • 根据 clone_flag 标志复制或共享进程内存管理结构(copy_mm 函数)
  • 设置进程在内核(或用户态)正常运行和调度所需的中断帧和执行上下文(copy_thread 函数)
  • 把设置好的进程控制块放入 hash_list 和 proc_list 两个全局进程链表中
  • 把进程状态设置为“就绪”态
  • 设置返回码为子进程的 id 号

fork() 的主要行为:

  • 申请 pid 与进程结构
  • 设置 ppid 为父进程的 pid
  • 复制用户相关的字段, 如 p_pgrp/p_gid/p_ruid/p_euid/p_rgid/p_egid
  • 复制调度相关的字段, 如 p_cpu/p_nice/p_pri
  • 复制父进程的文件描述符 (p_ofile), 并增加引用计数
  • 复制父进程的信号处理例程 (p_sigact)
  • 通过 vm_clone(), 复制父进程的地址空间(p_vm)
  • 复制父进程的寄存器状态 (p_context)
  • 复制父进程的中断上下文, 并设置 tf->eax 为 0, 使 fork()在子进程中返回 0。

Execution function

exec() 的主要行为:

  • 读取文件的第一个块, 检查 Magic Number 是否正确
  • 保存参数(argv)到临时分配的几个物理页, 其中的每个字符串单独一页
  • 清空旧的进程地址空间(vm_clear()), 并结合可执行文件的 header, 初始化新的进程地址空间(vm_renew())
  • 将 argv 与 argc 压入新地址空间中的栈
  • 释放临时存放参数的几个物理页
  • 关闭带有 FD_CLOEXEC 标识的文件描述符
  • 清理信号处理例程
  • 通过_retu()返回用户态

处理机调度

Schedule:

  • 从就绪队列中挑选下一个占用 CPU 的进程(挑选进程的内核函数)
  • 从多个可用 CPU 中挑选使用 CPU 资源

调度时机

  • 进程停止运行, 进入等待/挂起/终止状态
  • 进程的中断请求完成时, 由等待状态进入就绪状态, 准备抢占 CPU 资源(准备从内核态返回用户态)

六大调度时机

  • proc.c::do_exit 用户线程执行结束,主动放弃 CPU 控制权
  • proc.c::do_wait 用户线程等待子进程结束,主动放弃 CPU 控制权
  • proc.c::init_main
    • initproc 内核线程等待所有用户进程结束,如果没有结束,就主动放弃 CPU 控制权
    • initproc 内核线程在所有用户进程结束后,让 kswapd 内核线程执行 10 次,用于回收空闲内存资源
  • proc.c::cpu_idle idleproc 内核线程的工作就是等待有处于就绪态的进程或线程,如果有就调用 schedule 函数
  • sync.h::lock 在获取锁的过程中,如果无法得到锁,则主动放弃 CPU 控制权
  • trap.c::trap 如果在当前进程在用户态被打断,且当前进程控制块的成员变量 need_resched 设置为 1,则当前线程会放弃 CPU 控制权

调度策略

算法目标

  • CPU 有效使用率
  • 吞吐量 (高带宽): 单位时间内完成进程数量
  • 等待时间 (低延迟): 进程在就绪队列等待总时间
  • 周转时间 (低延迟): 进程从初始化到结束总时间
  • 响应时间 (低延迟): 从提交请求到产生响应总时间

先来先服务算法

First Come First Served (FCFS):

  • 依次执行就绪队列中的各进程(先进入就绪队列先执行)
  • CPU 利用率较低

短进程优先算法

Shortest Process Next/Shortest Remaining Time:

  • 优先执行周转耗时/剩余耗时最短的进程
  • 若短进程过多, 则导致长进程一直无法执行

最高响应比优先算法

Highest Response Ratio Next:

  • R = (waitTime + serviceTime) / serviceTime: 已等待时间越长, 优先级上升
  • 修正短进程优先算法的缺点

时间片轮转算法

Round Robin:

  • 在 FCFS 基础上, 设定一个基本时间单元, 每经过一个时间单元, 轮转至下一个先到进程(并进行循环轮转)
  • 额外的上下文切换
  • 时间片合适大小: 10 ms

多级队列调度算法

MQ:

  • 将就绪队列分成多个独立子队列, 每个队列可采取不同调度算法
  • 前台交互队列使用时间片轮转算法, 后台 IO 队列使用先来先服务算法
  • 队列间使用时间片轮转算法

多级反馈队列算法

MLFQ:

  • 优先级高的子队列时间片小, 优先级低的子队列时间片大
  • CPU 密集型进程(耗时高)优先级下降很快
  • IO 密集型进程(耗时低)停留在高优先级

同步互斥

  • 互斥 (Mutual Exclusion)
  • 死锁 (Deadlock)
  • 饥饿 (Starvation)

临界区的访问原则

  • 空闲则入
  • 忙则等待
  • 有限等待

基于软件方法的同步互斥

int turn; // 表示谁该进入临界区\nbool flag[]; // 表示进程是否准备好进入临界区\n
1
2
// 对于 2 个线程的情况\n// Peterson Algorithm\n// 线程 i\nflag[i] = true;\nturn = j; // 后设置 turn 标志的进程不可进入临界区, 会在 while 循环进行等待\nwhile (flag[j] && turn == j) ;\n\n...// critical section\n\nflag[i] = false;\n
1
2
3
4
5
6
7
8
9
10
// Dekkers Algorithm\n// 线程 i\nturn = 0;\nflag[] = {false};\n\ndo {\n  flag[i] = true;\n  while (flag[j] == true) {\n    if (turn != i) {\n      flag[i] = false;\n      while (turn != i) ;\n      flag[i] = true;\n    }\n  }\n\n  // critical section\n\n  turn = j;\n  flag[i] = false;\n} while (true);\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

高级抽象的同步互斥

利用原子操作实现互斥数据结构

Lock and Semaphore

struct lock/semaphore {\n  bool locked/sem = n; // n: 并发数/可用资源数\n  wait_queue q;\n\n  void acquire/prolaag() {\n    locked/sem--;\n    if (locked/sem < 0) sleep_and_enqueue(this_thread);\n  };\n\n  void release/verhoog() {\n    locked/sem++;\n    if (locked/sem <= 0) wakeup_and_dequeue(other_thread);\n  };\n};\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Monitor

  • 与 semaphore 相反, 初始 0, wait(++ && sleep), signal(-- && wakeup)
  • 管程内可以中断执行, 并 notify 其他等待线程

死锁

非抢占持有互斥循环等待

Mutex 实现

Prolaag and Verhoog

Prolaag Semaphore
  • 关中断
  • 判断当前信号量的 value 是否大于 0
  • 如果是>0,则表明可以获得信号量,故让 value 减一,并打开中断返回
  • 如果不是>0,则表明无法获得信号量,故需要将当前的进程加入到等待队列中,并打开中断,然后运行调度器选择另外一个进程执行
  • 如果被 V 操作唤醒,则把自身关联的 wait 从等待队列中删除(此过程需要先关中断,完成后开中断)
Verhoog Semaphore
  • 关中断
  • 如果信号量对应的 wait queue 中没有进程在等待,直接把信号量的 value 加一,然后开中断返回
  • 如果有进程在等待且进程等待的原因是 semaphore 设置的, 则调用 wakeup_wait 函数将 wait queue 中等待的第一个 wait 删除, 且把此 wait 关联的进程唤醒, 最后开中断返回

管程

管程由四部分组成:

  • 管程内部的共享变量(mutex): 一个二值信号量,是实现每次只允许一个进程进入管程的关键元素,确保了互斥访问性质
  • 管程内部的条件变量: 通过执行 wait_cv,会使得等待某个条件 C 为真的进程能够离开管程并睡眠,且让其他进程进入管程继续执行; 而进入管程的某进程设置条件 C 为真并执行 signal_cv 时,能够让等待某个条件 C 为真的睡眠进程被唤醒,从而继续进入管程中执行
  • 管程内部并发执行的进程
  • 对局部于管程内部的共享数据设置初始值的语句
  • 成员变量信号量 next: 配合进程对条件变量 cv 的操作而设置的, 由于发出 signal_cv 的进程 A 会唤醒睡眠进程 B, 进程 B 执行会导致进程 A 睡眠,直到进程 B 离开管程,进程 A 才能继续执行,这个同步过程是通过信号量 next 完成
  • 整形变量 next_count: 表示由于发出 signal_cv 而睡眠的进程个数
typedef struct monitor{\n    // the mutex lock for going into the routines in monitor,\n    // should be initialized to 1\n    semaphore_t mutex;\n    // the next semaphore is used to down the signaling proc itself,\n    // and the other OR woke up\n    semaphore_t next;\n    int next_count;     // the number of of sleepy signaling\n    proc condvar_t *cv; // the conditional variable in monitor\n} monitor_t;\n
1
2
3
4
5
6
7
8
9
10
Conditional Variable
  • wait_cv: 被一个进程调用, 以等待断言 Pc 被满足后该进程可恢复执行. 进程挂在该条件变量上等待时, 不被认为是占用了管程
  • signal_cv:被一个进程调用, 以指出断言 Pc 现在为真, 从而可以唤醒等待断言 Pc 被满足的进程继续执行
  • 信号量 sem: 用于让发出 wait_cv 操作的等待某个条件 C 为真的进程睡眠, 而让发出 signal_cv 操作的进程通过这个 sem 来唤醒睡眠的进程
  • count: 表示等在这个条件变量上的睡眠进程的个数
  • owner: 表示此条件变量的宿主是哪个管程

typedef struct condvar{ // the sem semaphore is used to down the waiting proc, // and the signaling proc should up the waiting semaphore_t sem; proc int count; // the number of waiters on condvar monitor_t * owner; // the owner(monitor) of this conditional variable } condvar_t;

文件系统

File System:

  • 分配文件磁盘空间: 分配与管理
  • 管理文件集合: 定位, 命名, 文件系统结构
  • 数据可靠和安全: 持久化保存, 防止数据丢失(系统崩溃时)
  • 基本操作单位: 数据块
  • 文件访问模式: 顺序访问/随机访问/索引访问
  • 文件系统种类: 磁盘/数据库/日志/网络/分布式/虚拟文件系统

文件组成

  • 文件头: 文件属性(名称/类型/大小/权限/路径/创建者/创建时间/最近修改时间)
  • 文件头: 文件存储位置与顺序
  • 文件体: 实际字节序列

文件系统基本数据结构

Super Block -> Directory Entry -> vnode/inode

文件卷控制块

Super Block:

  • 每个文件系统只有一个控制块
  • 描述该文件系统全局信息: 数据块(大小等信息), 空余块信息, 文件指针/引用计数

目录项

Directory Entry:

  • 目录是一类特殊的文件: 其内容为文件索引表(文件名/文件指针), 内部采取哈希表存储
  • 目录与文件构成树状结构
  • 每个目录项属于一个目录, 一个目录可有多个目录项
  • 保存目录相关信息: 指向文件控制块, 父目录/子目录信息

文件控制块

vnode/inode:

  • 每个文件有一个文件控制块
  • 保存该文件详细信息: 访问权限, 所属者/组, 文件大小, 数据块位置(索引)

文件描述符

操作系统在打开文件表中维护的打开文件状态与信息:

  • 文件指针: 最近一次读写位置
  • 文件打开计数(引用计数): 引用计数为 0 时, 回收相关资源
  • 文件磁盘位置
  • 访问权限

打开文件表

  • 系统打开文件表: 保存文件描述符
  • 进程打开文件表: 指向系统打开文件表某项, 并附加额外信息

文件分配

  • 分配方式: 连续分配, 琏式分配, 索引分配
  • 琏式索引分配: 琏式链接多个索引块
  • 多级索引分配: 索引分配 + 多级琏式索引块

空闲空间管理

  • bit 位图, 链表, 琏式索引: 保存空闲数据块位置与顺序

冗余磁盘阵列

Redundant Array of Inexpensive Disks (RAID):

  • RAID-0: 磁盘条带化
  • RAID-1: 磁盘镜像(冗余拷贝), 提高可靠性
  • RAID-4: 带奇偶校验(校验和)的磁盘条带化, 提高可靠性
  • RAID-5: 带分布式奇偶校验的磁盘条带化, 减少校验和所在物理磁盘的读写压力
  • RAID-6: 每组条带块有两个冗余块, 可以检查到 2 个磁盘错误

FS 实现

FS Mount

sfs_do_mount函数中:

  • 完成了加载位于硬盘上的 SFS 文件系统的超级块 superblock 和 freemap 的工作 l
  • 在内存中有了 SFS 文件系统的全局信息

FS Index

  • 对于普通文件,索引值指向的 block 中保存的是文件中的数据
  • 对于目录,索引值指向的数据保存的是目录下所有的文件名以及对应的索引节点所在的索引块(磁盘块)所形成的数组

FS Node

内存 inode 包含了 SFS 的硬盘 inode 信息, 而且还增加了其他一些信息, 这属于是便于进行是判断否改写、互斥操作、回收和快速地定位等作用. 一个内存 inode 是在打开一个文件后才创建的, 如果关机则相关信息都会消失. 而硬盘 inode 的内容是保存在硬盘中的, 只是在进程需要时才被读入到内存中, 用于访问文件或目录的具体内容数据.

struct inode {\n    union {\n        //包含不同文件系统特定inode信息的union成员变量\n        struct device __device_info; //设备文件系统内存inode信息\n        struct sfs_inode __sfs_inode_info; //SFS文件系统内存inode信息\n    } in_info;\n\n    enum {\n        inode_type_device_info = 0x1234,\n        inode_type_sfs_inode_info,\n    } in_type; //此inode所属文件系统类型\n\n    atomic_t ref_count; //此inode的引用计数\n    atomic_t open_count; //打开此inode对应文件的个数\n\n    struct fs *in_fs; //抽象的文件系统,包含访问文件系统的函数指针\n    const struct inode_ops *in_ops; //抽象的inode操作,包含访问inode的函数指针\n};\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct inode_ops {\n    unsigned long vop_magic;\n    int (*vop_open)(struct inode *node, uint32_t open_flags);\n    int (*vop_close)(struct inode *node);\n    int (*vop_read)(struct inode *node, struct iobuf *iob);\n    int (*vop_write)(struct inode *node, struct iobuf *iob);\n    int (*vop_getdirentry)(struct inode *node, struct iobuf *iob);\n    int (*vop_create)(\n        struct inode *node,\n        const char *name,\n        bool excl,\n        struct inode **node_store);\n    int (*vop_lookup)(struct inode *node, char *path, struct inode **node_store);\n\n    ……\n\n};\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Device

利用 vfs_dev_t 数据结构, 就可以让文件系统通过一个链接 vfs_dev_t 结构的双向链表找到 device 对应的 inode 数据结构, 一个 inode 节点的成员变量 in_type 的值是 0x1234, 则此 inode 的成员变量 in_info 将成为一个 device 结构。 这样 inode 就和一个设备建立了联系,这个 inode 就是一个设备文件

ELF File Format

#include "common.h"\n#include <stdlib.h>\n#include <elf.h>\n\nchar *exec_file = NULL;\n\nstatic char *strtab = NULL;\nstatic Elf32_Sym *symtab = NULL;\nstatic int nr_symtab_entry;\n\nvoid load_elf_tables(int argc, char *argv[]) {\n  int ret;\n  Assert(argc == 2, "run NEMU with format 'nemu [program]'");\n  exec_file = argv[1];\n\n  FILE *fp = fopen(exec_file, "rb");\n  Assert(fp, "Can not open '%s'", exec_file);\n\n  uint8_t buf[sizeof(Elf32_Ehdr)];\n  ret = fread(buf, sizeof(Elf32_Ehdr), 1, fp);\n  assert(ret == 1);\n\n  /* The first several bytes contain the ELF header. */\n  Elf32_Ehdr *elf = (void *)buf;\n  char magic[] = {ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3};\n\n  /* Check ELF header */\n  assert(memcmp(elf->e_ident, magic, 4) == 0); // magic number\n  assert(elf->e_ident[EI_CLASS] == ELFCLASS32); // 32-bit architecture\n  assert(elf->e_ident[EI_DATA] == ELFDATA2LSB); // little-endian\n  assert(elf->e_ident[EI_VERSION] == EV_CURRENT); // current version\n  assert(elf->e_ident[EI_OSABI] == ELFOSABI_SYSV || // UNIX System V ABI\n      elf->e_ident[EI_OSABI] == ELFOSABI_LINUX); // UNIX - GNU\n  assert(elf->e_ident[EI_ABIVERSION] == 0); // should be 0\n  assert(elf->e_type == ET_EXEC); // executable file\n  assert(elf->e_machine == EM_386); // Intel 80386 architecture\n  assert(elf->e_version == EV_CURRENT); // current version\n\n\n  /* Load symbol table and string table for future use */\n\n  /* Load section header table */\n  uint32_t sh_size = elf->e_shentsize * elf->e_shnum;\n  Elf32_Shdr *sh = malloc(sh_size);\n  fseek(fp, elf->e_shoff, SEEK_SET);\n  ret = fread(sh, sh_size, 1, fp);\n  assert(ret == 1);\n\n  /* Load section header string table */\n  char *section_header_string_table = malloc(sh[elf->e_shstrndx].sh_size);\n  fseek(fp, sh[elf->e_shstrndx].sh_offset, SEEK_SET);\n  ret = fread(section_header_string_table  sh[elf->e_shstrndx].sh_size, 1, fp);\n  assert(ret == 1);\n\n  int i;\n  for(i = 0; i < elf->e_shnum; i ++) {\n    if(sh[i].sh_type == SHT_SYMTAB &&\n        strcmp(section_header_string_table + sh[i].sh_name, ".symtab") == 0) {\n      /* Load symbol table from exec_file */\n      symtab = malloc(sh[i].sh_size);\n      fseek(fp, sh[i].sh_offset, SEEK_SET);\n      ret = fread(symtab, sh[i].sh_size, 1, fp);\n      assert(ret == 1);\n      nr_symtab_entry = sh[i].sh_size / sizeof(symtab[0]);\n    }\n    else if(sh[i].sh_type == SHT_STRTAB &&\n        strcmp(section_header_string_table + sh[i].sh_name, ".strtab") == 0) {\n      /* Load string table from exec_file */\n      strtab = malloc(sh[i].sh_size);\n      fseek(fp, sh[i].sh_offset, SEEK_SET);\n      ret = fread(strtab, sh[i].sh_size, 1, fp);\n      assert(ret == 1);\n    }\n  }\n\n  free(sh);\n  free(section_header_string_table ;\n\n  assert(strtab != NULL && symtab != NULL);\n\n  fclose(fp);\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

设备管理详解

  • CPU 一般都是通过寄存器的形式来访问外部设备
  • 外设的寄存器通常包括控制寄存器、状态寄存器与数据寄存器三类, 分别用于发送命令/读取状态/读写数据.

CGA EGA and ChromaText Video Buffer

在内存的低 1MB 中, 有许多地址被映射至外部设备, 其中就包含文字显示模块(显卡控制显示器):

  • 从 0xB8000 开始, 每 2 个字节表示屏幕上显示的一个字符(80 x 25)
  • 前一个字节为 字符 ASCII 码, 后一个字节为 字符颜色和属性的控制信息 (back_twinkle, back_r, back_g, back_b, front_light, front_r, front_g, front_b)

Input and Output

调用 io_delay() 函数: 对于一些老式总线的外部设备, 读写 I/O 端口的速度若过快就容易出现丢失数据的现象

实践

工具

Bochs

Installation
wget\n\\ http://sourceforge.net/projects/bochs/files/bochs/2.5.1/bochs-2.5.1.tar.gz/download\n\\ -O bochs.tar.gz\ntar -xvfz bochs.tar.gz\ncd bochs-2.5.1\n./configure --enable-debugger --enable-debugger-gui --enable-disasm --with-x --with-term\nmake\nsudo cp ./bochs /usr/bin/bochs-dbg\n
1
2
3
4
5
6
7
8
Config
# BIOS与VGA镜像\nromimage: file=/usr/share/bochs/BIOS-bochs-latest\nvgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest\n# 内存大小\nmegs: 128\n# 软盘镜像\nfloppya: 1_44=bin/kernel.images, status=inserted\n# 硬盘镜像\nata0-master: type=disk, path="bin/rootfs.images",\n\\ mode=flat, cylinders=2, heads=16, spt=63\n# 引导方式(软盘)\nboot: a\n# 日志输出\nlog: .bochsout\npanic: action=ask\nerror: action=report\ninfo: action=report\ndebug: action=ignore\n# 杂项\nvga_update_interval: 300000\nkeyboard_serial_delay: 250\nkeyboard_paste_delay: 100000\nmouse: enabled=0\nprivate_colormap: enabled=0\nfullscreen: enabled=0\nscreenmode: name="sample"\nkeyboard_mapping: enabled=0, map=\nkeyboard_type: at\n# 符号表(调试用)\ndebug_symbols: file=main.sym\n# 键盘类型\nkeyboard_type: at\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Run
bochs -q -f .bochsrc\n
1
  • b,vb,lb 分别为物理地址、虚拟地址、逻辑地址设置断点
  • c 持续执行,直到遇到断点或者错误
  • n 下一步执行
  • step 单步执行
  • r 显示当前寄存器的值
  • sreg 显示当前的段寄存器的值
  • info gdt, info idt, info tss, info tab 分别显示当前的 GDT、IDT、TSS、页表信息
  • print-stack 打印当前栈顶的值
  • help 显示帮助

GNU ld

ENTRY(kmain)\nSECTIONS {\n    __bios__ = 0xa0000; # 绑定BIOS保留内存的地址到__bios__\n    vgamem = 0xb8000; # 绑定vga缓冲区的地址到符号vgamem\n    .text 0x100000 : { # 内核二进制镜像中的.text段(Section),从0x100000开始\n        __kbegin__ = .; # 内核镜像的开始地址\n        __code__ = .;\n        bin/entry.o(.text) bin/main.o(.text) *(.text); # 将bin/entry.o中的.text段安排到内核镜像的最前方\n        . = ALIGN(4096); # .text段按4kb对齐\n    }\n    .data : {\n        __data__ = .;\n        *(.rodata);\n        *(.data);\n        . = ALIGN(4096);\n    }\n    .bss : {\n        __bss__ = .;\n        *(.bss);\n        . = ALIGN(4096);\n    }\n    __kend__ = .; # 内核镜像的结束地址\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Memory Management Tools

perf

trace

vmstat

check usage of virtual memory and swap region

vmstat 2\n
1

pmap

check detailed usage of memory

pmap PID\n
1

Memory Hot Plug

TCMalloc

Google TCMalloc

Oprofile

',390);p.render=function(s,e,p,l,o,i){return n(),a("div",null,[t])};export default p;export{e as __pageData}; diff --git a/assets/computerScience_operatingSystems_operatingSystemsBasicNotes.md.5966c145.lean.js b/assets/computerScience_operatingSystems_operatingSystemsBasicNotes.md.5966c145.lean.js new file mode 100644 index 00000000000..c99d5c93ba5 --- /dev/null +++ b/assets/computerScience_operatingSystems_operatingSystemsBasicNotes.md.5966c145.lean.js @@ -0,0 +1 @@ +import{o as n,c as a,a as s}from"./app.6dd2a1b7.js";const e='{"title":"Operating System Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"OS Definition","slug":"os-definition"},{"level":2,"title":"GCC 内联汇编","slug":"gcc-内联汇编"},{"level":2,"title":"基本概念","slug":"基本概念"},{"level":3,"title":"操作系统的特性","slug":"操作系统的特性"},{"level":3,"title":"操作系统的资源管理功能","slug":"操作系统的资源管理功能"},{"level":3,"title":"操作系统的演变","slug":"操作系统的演变"},{"level":3,"title":"操作系统虚拟机","slug":"操作系统虚拟机"},{"level":3,"title":"操作系统的组织结构","slug":"操作系统的组织结构"},{"level":3,"title":"并发","slug":"并发"},{"level":2,"title":"启动","slug":"启动"},{"level":3,"title":"BIOS","slug":"bios"},{"level":3,"title":"启动顺序","slug":"启动顺序"},{"level":2,"title":"物理内存管理","slug":"物理内存管理"},{"level":3,"title":"Boot Loader 探测机器内存分布","slug":"boot-loader-探测机器内存分布"},{"level":3,"title":"Boot Loader 基本概念","slug":"boot-loader-基本概念"},{"level":3,"title":"连续内存分配","slug":"连续内存分配"},{"level":3,"title":"非连续内存分配","slug":"非连续内存分配"},{"level":3,"title":"内存的特权级","slug":"内存的特权级"},{"level":2,"title":"虚拟内存管理","slug":"虚拟内存管理"},{"level":3,"title":"Page Fault","slug":"page-fault"},{"level":3,"title":"覆盖与交换","slug":"覆盖与交换"},{"level":3,"title":"虚拟页式存储管理","slug":"虚拟页式存储管理"},{"level":2,"title":"中断","slug":"中断"},{"level":3,"title":"中断进入","slug":"中断进入"},{"level":3,"title":"中断实现","slug":"中断实现"},{"level":3,"title":"系统调用","slug":"系统调用"},{"level":2,"title":"进程","slug":"进程"},{"level":3,"title":"进程特权级","slug":"进程特权级"},{"level":3,"title":"进程状态与生命周期","slug":"进程状态与生命周期"},{"level":3,"title":"进程通信","slug":"进程通信"},{"level":3,"title":"线程","slug":"线程"},{"level":3,"title":"内核线程与用户进程","slug":"内核线程与用户进程"},{"level":3,"title":"Process 实现","slug":"process-实现"},{"level":2,"title":"处理机调度","slug":"处理机调度"},{"level":3,"title":"调度时机","slug":"调度时机"},{"level":3,"title":"调度策略","slug":"调度策略"},{"level":2,"title":"同步互斥","slug":"同步互斥"},{"level":3,"title":"临界区的访问原则","slug":"临界区的访问原则"},{"level":3,"title":"基于软件方法的同步互斥","slug":"基于软件方法的同步互斥"},{"level":3,"title":"高级抽象的同步互斥","slug":"高级抽象的同步互斥"},{"level":3,"title":"死锁","slug":"死锁"},{"level":3,"title":"Mutex 实现","slug":"mutex-实现"},{"level":2,"title":"文件系统","slug":"文件系统"},{"level":3,"title":"文件组成","slug":"文件组成"},{"level":3,"title":"文件系统基本数据结构","slug":"文件系统基本数据结构"},{"level":3,"title":"文件分配","slug":"文件分配"},{"level":3,"title":"空闲空间管理","slug":"空闲空间管理"},{"level":3,"title":"冗余磁盘阵列","slug":"冗余磁盘阵列"},{"level":3,"title":"FS 实现","slug":"fs-实现"},{"level":3,"title":"ELF File Format","slug":"elf-file-format"},{"level":2,"title":"设备管理详解","slug":"设备管理详解"},{"level":3,"title":"CGA EGA and ChromaText Video Buffer","slug":"cga-ega-and-chromatext-video-buffer"},{"level":3,"title":"Input and Output","slug":"input-and-output"},{"level":2,"title":"实践","slug":"实践"},{"level":3,"title":"工具","slug":"工具"},{"level":2,"title":"Memory Management Tools","slug":"memory-management-tools"},{"level":3,"title":"perf","slug":"perf"},{"level":3,"title":"trace","slug":"trace"},{"level":3,"title":"vmstat","slug":"vmstat"},{"level":3,"title":"pmap","slug":"pmap"},{"level":3,"title":"Memory Hot Plug","slug":"memory-hot-plug"},{"level":3,"title":"TCMalloc","slug":"tcmalloc"},{"level":3,"title":"Oprofile","slug":"oprofile"}],"relativePath":"computerScience/operatingSystems/operatingSystemsBasicNotes.md","lastUpdated":1627194884000}',p={},t=s('',390);p.render=function(s,e,p,l,o,i){return n(),a("div",null,[t])};export default p;export{e as __pageData}; diff --git a/assets/computerScience_virtualization_virtBasicNotes.md.f20c0b3e.js b/assets/computerScience_virtualization_virtBasicNotes.md.f20c0b3e.js new file mode 100644 index 00000000000..8aca551ad84 --- /dev/null +++ b/assets/computerScience_virtualization_virtBasicNotes.md.f20c0b3e.js @@ -0,0 +1 @@ +import{o as a,c as n,a as s}from"./app.6dd2a1b7.js";const e='{"title":"Virtualization Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"How to read paper","slug":"how-to-read-paper"},{"level":2,"title":"LXC","slug":"lxc"},{"level":3,"title":"Linux Namespaces","slug":"linux-namespaces"},{"level":3,"title":"chroot","slug":"chroot"},{"level":3,"title":"Control Groups","slug":"control-groups"},{"level":2,"title":"In-Memory Computing Thesis Notes","slug":"in-memory-computing-thesis-notes"},{"level":3,"title":"单节点内存计算","slug":"单节点内存计算"},{"level":3,"title":"分布式内存计算","slug":"分布式内存计算"},{"level":3,"title":"混合内存计算","slug":"混合内存计算"},{"level":2,"title":"Hadoop Thesis Notes","slug":"hadoop-thesis-notes"},{"level":3,"title":"Google File System","slug":"google-file-system"},{"level":3,"title":"Map Reduce","slug":"map-reduce"},{"level":3,"title":"Google Big Table","slug":"google-big-table"},{"level":2,"title":"Unikernel","slug":"unikernel"},{"level":3,"title":"Unikernels","slug":"unikernels"},{"level":3,"title":"libOS","slug":"libos"},{"level":3,"title":"OPAM","slug":"opam"},{"level":3,"title":"MirageOS","slug":"mirageos"},{"level":2,"title":"Xen Soft Device","slug":"xen-soft-device"},{"level":3,"title":"Device Access in Xen","slug":"device-access-in-xen"},{"level":2,"title":"Memory Management","slug":"memory-management"},{"level":3,"title":"Direct Segment","slug":"direct-segment"},{"level":3,"title":"Redundant Memory Mappings (RMM)","slug":"redundant-memory-mappings-rmm"}],"relativePath":"computerScience/virtualization/virtBasicNotes.md","lastUpdated":1627194884000}',t={},i=s('

Virtualization Basic Notes

How to read paper

  • abstract, introduction, related works, conclusion
  • reference
  • paper refers it (by Google Scholar)

LXC

Linux Namespaces

LXC 所实现的隔离性主要是来自内核的命名空间

  • PID Namespace: 隔离进程
  • Network Namespace: 隔离网络
  • IPC Namespace: 隔离消息
  • Mount Namespace: 隔离文件系统
  • UTS Namespace: 隔离 hostname
  • Users Namespace
package main\n\nimport (\n    "log"\n    "os"\n    "os/exec"\n    "syscall"\n)\n\nfunc main() {\n    switch os.Args[1] {\n    case "run":\n        run()\n    case "child":\n        child()\n    default:\n        log.Fatal("Invalid command")\n    }\n}\n\nfunc run() {\n    cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...)\n    cmd.Stdin = os.Stdin\n    cmd.Stdout = os.Stdout\n    cmd.Stderr = os.Stderr\n    cmd.SysProcAttr = &syscall.SysProcAttr {\n        CloneFlags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID\n          | syscall.CLONE_NEWNS | syscall.CLONE_NEWUSER,\n        Credential: &syscall.Credential{Uid: 0, Gid: 0},\n        UidMappings: []syscall.SysProcIDMap{\n            {ContainerID: 0, HostID: os.Getuid(), Size: 1},\n        },\n        GidMappings: []syscall.SysProcIDMap{\n            {ContainerID: 0, HostID: os.Getgid(), Size: 1},\n        },\n    }\n\n    must(cmd.Run())\n}\n\nfunc child() {\n    log.Printf("running %v as PID %d\\n", os.Args[2:], os.Getpid())\n\n    // cg()\n\n    cmd := exec.Command(os.Args[2], os.Args[3:]...)\n    cmd.Stdin = os.Stdin\n    cmd.Stdout = os.Stdout\n    cmd.Stderr = os.Stderr\n\n    must(syscall.Sethostname([]byte("mocker")))\n    must(syscall.Chroot("/home/sabertazimi/rootfs"))\n    must(syscall.Chdir("/"))\n    must(syscall.Mount("proc", "proc", "proc", 0, ""))\n    must(cmd.Run())\n    must(syscall.Unmount("proc", 0))\n}\n\nfunc must(err error) {\n    if err != nil {\n        log.Fatal(err)\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

chroot

must(syscall.Chroot("/home/sabertazimi/rootfs"))\nmust(syscall.Chdir("/"))\nmust(syscall.Mount("proc", "proc", "proc", 0, ""))\n
1
2
3

Control Groups

cgroups, resources limitation: CPU, Memory, Disk I/O, Process numbers, Network, Device.

func cg() {\n    cgroups := "/sys/fs/cgroup/"\n    pids := filepath.Join(cgroups, "pids")\n    os.Mkdir(filepath.Join(pids, "sabertazimi"), 0755)\n    must(ioutil.WriteFile(filepath.Join(pids, "sabertazimi/pids.max"),\n      []byte("20"), 0700))\n    must(ioutil.WriteFile(filepath.Join(pids, "sabertazimi/notify_on_release"),\n      []byte("1"), 0700))\n    must(ioutil.WriteFile(filepath.Join(pids, "sabertazimi/cgroup.procs"),\n      []byte(strconv.Itoa(os.Getpid())), 0700))\n}\n
1
2
3
4
5
6
7
8
9
10
11

大数据处理典型问题:

  • 数据一致性问题
  • 数据容错与恢复问题
  • 节点通信问题
  • 能耗问题

In-Memory Computing Thesis Notes

内存计算特点:

  • 硬件: 大内存
  • 软件: 良好编程模型和编程接口
  • 面向数据密集型应用: 数据规模大, 实时性要求高
  • 大多支持并行处理数据

内存计算分类:

  • 单节点
  • 分布式集群
  • 新型混合内存 (Hybrid Memory)

单节点内存计算

节点拥有 1/n 个处理器(单/多核) + 共享内存 (Shared Memory)

内存存储系统

内存数据处理系统

内存压缩技术

提升 I/O 访问效率

分布式内存计算

  • 容错与恢复
  • 同步与一致性
  • 内存分配与管理
  • 网络瓶颈

分布式内存存储系统

  • 内存压缩技术

内存缓存系统

  • 内存替换策略: LRU, LFU
  • 预取技术

分布式内存数据处理系统

  • Spark

混合内存计算

铁电存储器: Ferroelectric Random Access Memory, 简称 FeRAM 相变存储器: Phase Change Memory, 简称 PCM 电阻存储器: Resistive Random Access Memory, 简称 RRAM

Hadoop Thesis Notes

Google File System

基本设计

  • 迅速地侦测、冗余并恢复失效的组件

  • 存储一定数量的大文件

  • 大规模的流式读取

  • 小规模的随机读取

  • 大规模的、顺序的、数据追加方式的写操作

  • 小规模的随机位置写入操作

  • 高性能的稳定网络带宽远比低延迟重要

  • 客户端和 Master 节点的通信只获取元数据,所有的数据操作都是由客户端直接和 Chunk 服务器进行交互的

  • 无论是客户端还是 Chunk 服务器都不需要缓存文件数据

  • 出于可靠性的考虑, 每个块都会复制到多个块服务器上.缺省情况下, 我们使用 3 个存储复制节点.

元数据

Master 节点主要存储三种类型元数据 (保存在内存中, 轮询 Chunk 服务器/周期心跳信息来更新元数据):

  • 文件和 Chunk 的命名空间
  • 文件和 Chunk 的对应关系
  • 每个 Chunk 副本的存放地点

元数据操作日志:

  • 先写日志再响应请求
  • Checkpoint 文件
Chunk 尺寸

较大的 Chunk 尺寸:

  • 降低元数据读取工作和缓存工作成本
  • 多次操作集中在少数 Chunk 上
一致性模型
Chunk 副本修改一致性
  • 对 Chunk 的所有副本的修改操作顺序一致
  • 根据版本号判断 Chunk 是否失效 (未成功进行某次修改), 使用垃圾收集系统回收失效 Chunk

当某个 Chunk 副本失效, 称为不可用; 当所有 Chunk 副本失效, 称为损坏

系统交互

原则: 减小 Master 负担

租约机制

Master 为某个 Chunk 建立租约, 称为主 Chunk: 修改顺序由主 Chunk 进行管理, 从而减小 Master 管理负担:

  • Client 与 Master 通信, 获取主 Chunk 信息
  • Client 将数据传至所有 Chunk 副本
  • Client 将操作发送至主 Chunk
  • 主 Chunk 为所有操作分配连续的序列号, 所有副本按照序列号顺序执行操作, 并在完成后回复主 Chunk
  • 主 Chunk 回复 Client

实际上, 客户端通常会在一次请求中查询多个 Chunk 信息, Master 节点的回应也可能包含了紧跟着这些被请求的 Chunk 后面的 Chunk 的信息. 在实际应用中, 这些额外的信息在没有任何代价的情况下, 避免了客户端和 Master 节点未来可能会发生的几次通讯.

数据流

链式顺序最近推送原则: 推送至距离最近 (IP 地址) 的一个下游

原子追加操作

GFS 并不保证 Chunk 的所有副本在字节级别是完全一致的。它只保证数据作为一个整体原子的被至少写入一次

快照

copy-on-write 技术实现快照

当 Master 节点收到一个快照请求,它首先取消作快照的文件的所有 Chunk 的租约。这个措施保证了后续对这些 Chunk 的写操作都必须与 Master 交互以找到租约持有者

Master 节点通过复制源文件或者目录的元数据的方式,把这条日志记录的变化反映到保存在内存的状态中。新创建的快照文件和源文件指向完全相同的 Chunk 地址。

确保数据在本地复制而不是通过网络复制

Master 节点

Master 节点管理所有的文件系统元数据

元数据包括:

  • 名字空间
  • 访问控制信息
  • 文件和 Chunk 的映射信息
  • 当前 Chunk 的位置信息

Master 节点基本工作:

  • 执行所有的名称空间操作
  • 管理着整个系统里所有 Chunk 的副本: 决定 Chunk 的存储位置, 创建新 Chunk 和它的副本, 协调各种各样的系统活动以保证 Chunk 被完全复制, 在所有的 Chunk 服务器之间的进行负载均衡, 回收不再使用的存储空间
名称空间锁

每一个操作都获取一个目录名的上的读取锁和文件名上的写入锁. 目录名的读取锁足以的防止目录被删除、改名以及被快照. 文件名的写入锁序列化文件创建操作, 确保不会多次创建同名的文件.

Chunk 副本平衡
  • 平衡硬盘使用率
  • 限制同一台 Chunk 服务器上的正在进行的克隆操作的数量
  • 在机架间分布副本
垃圾回收
  • 回收过期(版本号不一致)和损坏的 Chunk.
  • GFS 空间回收采用惰性的策略: 只在文件和 Chunk 级的常规垃圾收集时进行真正的删除操作.
  • 垃圾回收把存储空间的回收操作合并到 Master 节点规律性的后台活动中(例行扫描和与 Chunk 服务器握手).

容错性

  • 快速自启动
  • Chunk 副本
  • Master 服务器副本
  • Chunk Checksum 检验数据完整性

Map Reduce

  • 分割输入数据 (数据分布)
  • 集群调度 (负载均衡)
  • 错误处理 (容错)
  • 集群通信
  1. 用户程序首先调用的 MapReduce 库将输入文件分成 M 个数据片度,每个数据片段的大小一般从 16MB 到 64MB(可以通过可选的参数来控制每个数据片段的大小)。然后用户程序在机群中创建大量 的程序副本。
  2. 这些程序副本中的有一个特殊的程序–master。副本中其它的程序都是 worker 程序,由 master 分配 任务。有 M 个 Map 任务和 R 个 Reduce 任务将被分配,master 将一个 Map 任务或 Reduce 任务分配 给一个空闲的 worker。
  3. 被分配了 map 任务的 worker 程序读取相关的输入数据片段,从输入的数据片段中解析出 key/value pair,然后把 key/value pair 传递给用户自定义的 Map 函数,由 Map 函数生成并输出的中间 key/value pair,并缓存在内存中。
  4. 缓存中的 key/value pair 通过分区函数分成 R 个区域,之后周期性的写入到本地磁盘上。缓存的 key/value pair 在本地磁盘上的存储位置将被回传给 master,由 master 负责把这些存储位置再传送给 Reduce worker。
  5. 当 Reduce worker 程序接收到 master 程序发来的数据存储位置信息后,使用 RPC 从 Map worker 所在 主机的磁盘上读取这些缓存数据。当 Reduce worker 读取了所有的中间数据后,通过对 key 进行排序 后使得具有相同 key 值的数据聚合在一起。由于许多不同的 key 值会映射到相同的 Reduce 任务上, 因此必须进行排序。如果中间数据太大无法在内存中完成排序,那么就要在外部进行排序。
  6. Reduce worker 程序遍历排序后的中间数据,对于每一个唯一的中间 key 值,Reduce worker 程序将这 个 key 值和它相关的中间 value 值的集合传递给用户自定义的 Reduce 函数。Reduce 函数的输出被追 加到所属分区的输出文件。
  7. 当所有的 Map 和 Reduce 任务都完成之后,master 唤醒用户程序。在这个时候,在用户程序里的对 MapReduce 调用才返回。

Google Big Table

BigTable 是一个稀疏的、分布式的、持久化存储的多维度排序, Map: (row: string, column: string, time: int64) -> string.

数据模型

  • 对同一个行关键字的读或者写操作都是原子的
  • BigTable 通过行关键字的字典顺序来组织数据
  • 表中的每个行都可以动态分区, 每个分区叫做一个 "Tablet"
  • Tablet 是数据分布和负载均衡调整的最小单位
列族
  • 列关键字组成的集合叫做 "列族", 列族是访问控制的基本单位
  • 存放在同一列族下的所有数据通常都属于同一个类型
  • 访问控制、磁盘和内存的使用统计都是在列族层面进行的
时间戳
  • 在 BigTable 中, 表的每一个数据项都可以包含同一份数据的不同版本
  • 不同版本的数据通过时间戳来索引

依赖

SSTable
  • BigTable 内部存储数据的文件是 Google SSTable 格式的, SSTable 是一个持久化的、排序的、不可更改的 Map 结构
  • SSTable 使用块索引(通常存储在 SSTable 的最后)来定位数据块
  • 在打开 SSTable 的时候,索引被加载到内存
  • 每次查找都可以通过一次磁盘搜索完成: 首先使用二分查找法在内存中的索引里找到数据块的位置,然后再从硬盘读取相应的数据块
Chubby

BigTable 使用 Chubby 完成以下的几个任务:

  1. 确保在任何给定的时间内最多只有一个活动的 Master 副本
  2. 存储 BigTable 数据的自引导指令的位置
  3. 查找 Tablet 服务器,以及在 Tablet 服务器失效时进行善后
  4. 存储 BigTable 的模式信息(每张表的列族信息)
  5. 存储访问控制列表

组件

BigTable 包括了三个主要的组件: 链接到客户程序中的库、一个 Master 服务器和多个 Tablet 服务器

Master 服务器

Master 服务器主要负责以下工作:

  • 为 Tablet 服务器分配 Tablets
  • 检测新加入的或者过期失效的 Table 服务器
  • 对 Tablet 服务器进行负载均衡
  • 对保存在 GFS 上的文件进行垃圾收集
  • 处理对模式的相关修改操作, 例如建立表和列族
Tablet 服务器
  • 每个 Tablet 服务器都管理一个 Tablet 的集合(通常每个服务器有大约数十个至上千个 Tablet)
  • 每个 Tablet 服务器负责处理它所加载的 Tablet 的读写操作
  • 在 Tablets 过大时,对其进行分割

实现

定位
  • 使用三层 B+ 树结构存储 Tablets 的位置信息 (位于哪个 Tablet 服务器上)
  • MetaTablet 中 的 row key 是通过 table id 和 end row 来计算出的: 因为 row 是有序的, 所以在读取 row/column 型的数据时可以利用到有序性.
服务
  • 当写入表时, 先写日志, 再写入内存中的 memTable, 当其大小达到阈值后, 写入 GFS 中 (以 SSTable 存储)
  • 利用 Log-Structured Merge Tree 来表示 memTable 和 SSTable 之间的关系 (内存数据滚动合并至磁盘)
  • 对于一致性的处理,将 SSTable 不可改变化,然后对 memTable 使用 copy-on-write 技术实现读写并行

Unikernel

小、简单、安全、高效

Fast, Small, Secure Workloads

Containers are Smaller and Faster, but Security is Still an Issue

SELinux: 在一个开启了 SELinux 的内核中, 可能存在 10 万条以上的安全策略, 有时无法正常启动服务 (缺少权限)

Unikernels

Unikernels do at compile time what standard programs do at runtime:

  • Most unikernels use a specialized compiling system that compiles in the low-level functions the developer has selected
  • The code for these low-level functions is compiled directly into the application executable through a library operating system
  • library operating system: a special collection of libraries that provides needed operating system functions in a compilable format

Security

  • There is no command shell to leverage
  • There are no utilities to co-opt
  • There are no unused device drivers or unused libraries to attack
  • There are no password files or authorization information present
  • There are no connections to machines and databases not needed by the application

Debug

Since there is no operating system environment on a unikernel production VM, there are no tools, no debuggers, no shell access with which someone can probe a failure on a deployed program.

Instead, all that can be done is to engineer the executable to log events and data so that the failure might be reconstructed on a development system, which still has access to all the debugging tools needed.

People in our industry have successfully debugged failures of complex software for years without direct access to production systems, and I see no reason why they will fail to do so now.

Limits

  • Single Process (but Multiple Threads)
  • Single User
  • Limited Debugging
  • Impoverished Library Ecosystem

Ecosystem

ClickOS

network function virtualization (NFV) devices

RumpRun

compile most of the programs found on a Linux or Unix-like operating system as unikernels

OSv

It is unique among existing unikernels in that it provides a general-purpose unikernel base that can accept just about any program that can run in a single process (multiple threads are allowed, but multiple processes are not).

As a result, OSv is a "fat" unikernel: the results are measured in megabytes, rather than kilobytes.

Mini-OS

Mini-OS is a tiny OS kernel distributed with the Xen Project Hypervisor sources:

  • used as operating system for stub domains that are used for Dom0 Disaggregation.
  • used as a basis for development of Unikernels, having been instrumental in the formation of multiple examples including ClickOS and Rump kernels.
Xen

Its paravirtualization capabilities allow unikernels to have a very small and efficient footprint interfacing with devices. This is one reason why the Xen Project has been at the forefront of unikernel development.

Xen team has been consciously reworking the hypervisor’s capabilities so it can handle a future state where 2,000 or 3,000 simultaneous unikernel VMs may need to coexist on a single hardware host server. Currently, the hypervisor can handle about 1,000 unikernels simultaneously before scaling becomes nonlinear. The development work continues to improve unikernel support in each release.

Unik
  • UniK is an open source tool written in Go for compiling applications into unikernels and deploying those unikernels.
  • UniK utilizes a simple Docker-like command-line interface, making developing on unikernels as easy as developing on containers.

libOS

  • implement drivers for the virtual hardware devices provided by the hypervisor
  • create the protocol libraries to replace the services of a traditional OS

the kernel is event-driven via an I/O loop that polls Xen devices

OPAM

modern modular, functional and type-safe programming language

opam install\nopam update -u\nopam switch\nopam remote\nopam depext\n
1
2
3
4
5

MirageOS

Exokernel -> Library Operating System -> Unikernel:

  • Unikernels: specialized, sealed, single purpose libOS VMs that run directly on the hypervisor
  • A libOS is structured very differently from a conventional OS: all services, from the scheduler to the device drivers to the network stack, are implemented as libraries linked directly with the application.
  • Coupled with the choice of a modern statically type-safe language for implementation, this affords configuration, performance and security benefits to unikernels.
  • the current release contains clean-slate libraries for TLS, TCP/IP, DNS, Xen network and storage device drivers, HTTP and other common Internet protocols.

Installation

opam init\nopam remote\nocaml -version\nopam switch 4.04.2\neval `opam config env`\nopam list\nopam install mirage\n
1
2
3
4
5
6
7
mirage --help\nmirage configure --xen\nmake depend\nmake\nsudo xl create -c console.xl\nstrip console.xen\nbzip2 -9 console.xen\n
1
2
3
4
5
6
7

Development

opam source repo.name --dev-repo --pin\n
1

Use of OCaml

OCaml is a pragmatic system that strikes a balance between imperative languages, e.g., C, and pure functional languages, e.g., Haskell. It features type inference, algebraic data types, and higher-order functions, but also permits references and mutable data structures while guaranteeing that all such side-effects are always type safe and will never cause memory corruption.

  • (ICFP'10) a full-fledged systems programming language with a flexible programming model that supports functional, imperative and object-oriented programming
  • OCaml has a simple yet high performance runtime making it an ideal platform for experimenting with the unikernel abstraction that interfaces the runtime with Xen.
  • static typing eliminates type information at compile-time while retaining all the benefits of type-safety (remove dynamic typing overheads and introduce more safety at compile time).
  • (SOSP'03/11) the open-source Xen Cloud Platform and critical system components are implemented in OCaml, making integration straightforward.
Concurrency
  • lightweight control-flow threads for managing I/O and timeouts
  • optimized inter-VM communication for parallel computation

Each Mirage instance runs as on a single CPU core. Communication is optimized dynamically: if the VMs are running on the same physical machine, Mirage uses shared memory channels instead of the network stack.

Thread Flow
  • The application’s main thread is launched immediately after boot and the VM shuts down when it returns.
  • Mirage provides an evaluator that uses domain poll to listen for events and wake up lightweight threads. The VM is thus either executing OCaml code or blocked, with no internal preemption or asynchronous interrupts.
  • The main thread repeatedly executes until it completes or throws an exception, and the domain subsequently shuts down with the VM exit code matching the thread return value.

Device Drivers

Data arrives to both the network and storage stacks as a stream of discrete packets. Mirage bridges the gap between packets and streams by using channel iterates that map functions over infinite streams of packets to produce a typed stream.

I/O Stack

Mirage implements a Xen block front VFS which interacts directly with a block device without an intervening filesystem.

Xen Soft Device

Device-level interfaces in operating systems present a very useful cut-point for researchers to experiment with new ideas. By virtualizing these interfaces, developers can create soft devices, which are used in the same way as normal hardware devices, but provide extra functionality in software.

Rather than attempting to present a fully virtualized hardware interface to each OS in a Xen environment, guest OSes are modified to use a simple, narrow and idealized view of hardware. Soft devices take advantage of these narrow interface to capture and transform block requests, network packets, and USB messages.

Device Access in Xen

Physical driver runs in an isolated VM, connected over a shared memory device channel to a guest VM accessing the device. Operating systems wishing to access the device will use a front-end driver, and interact with the back-end driver over a device channel, which is a shared-memory communications primitive (原语).

Memory Management

Direct Segment

In light of the high cost of page-based virtual memory and its significant mismatch to “big-memory” application needs, we propose mapping part of a process’s linear virtual address with a direct segment rather than pages.

Mapping part of a process’s linear virtual address space with a direct segment, while page mapping the rest of the virtual address space:

  • retains a standard linear virtual address space
  • is not overlaid on top of paging
  • coexists with paging of other virtual addresses
  • a one-time fixed-cost solution for any size memory

Large Pages Shortcoming

  • Being a cache, TLBs are reliant on memory-access locality to be effective and it can be a mismatch for future big-memory workloads with poor locality
  • efficient TLB support for multiple page sizes is difficult
  • large page sizes are often few and far apart (not enough for big-memory workload)

Big-memory workload analysis

  • While address translation can be accelerated by TLB hits, misses are costly, taking up to 100s of cycles
  • A sparse memory access pattern can result in more misses with fewer TLB entries

For the majority of their address space, big-memory workloads do not require:

  • swapping

  • fragmentation mitigation

  • fine-grained per-page protection

  • Big-memory workloads pay a cost of page based virtual memory: substantial performance lost to TLB misses

  • Big-memory workloads are long-running programs, receive little benefit from virtual memory optimizations whose primary goal is to allow quick program startup, such as demand paging

  • Big-memory workloads consume almost all memory resources, are sized to match memory capacity

  • Big-memory workloads isolate from other services, have one (or a few) primary process(es)

Hardware Support

direct segments add three registers per core as follows:

  • BASE holds the start address of the contiguous virtual address range mapped through direct segment
  • LIMIT holds the end address of the virtual address range mapped through direct segment
  • OFFSET holds the start address of direct segment’s backing contiguous physical memory minus the value in BASE.

Direct segments are aligned to the base page size, so page offset bits are omitted from these registers (e.g., 12 bits for 4KB pages)

Without real direct segment hardware, we emulate direct segment functionalities using 4KB pages

More specifically, we modify Linux’s page fault handler so that on a page fault within the primary region it calculates the corresponding physical address from the faulting virtual page number.

If VA fault is the 4KB page-aligned virtual address of a faulting page, then our modified page-fault handler first checks if BASE ≤ VA fault < LIMIT. If so, the handler adds a mapping from VA fault to VA fault + OFFSET to the page table.

Software Support

The prototype implementation is simplified by assuming that only one process uses a direct segment at any time (called the primary process)

  • A primary region is a contiguous range of virtual addresses in a process’s address space with uniform read-write access permission
  • fine-grain protection, sparse allocation, swapping, and demand paging are not guaranteed for memory allocated within the primary region
  • managing physical memory: create contiguous physical memory dynamically through periodic memory compaction
  • managing direct segment registers: When the OS dispatches a thread; it loads the BASE, LIMIT, and OFFSET values from the PCB
  • Growing and shrinking direct segment (by updating direct segment registers and page table entries)

Virtual Machines with Direct Segment

In a virtualized environment the memory accesses goes through two levels of address translations:

  1. guest virtual address (gVA) to guest physical address (gPA)
  2. guest physical address (gPA) to system physical address (sPA)

Results

In x86-64, page table entries (PTEs) have a set of reserved bits (41-51 in our test platform) that cause a trap when loaded into the TLB

Redundant Memory Mappings (RMM)

在大内存系统中, 传统的段页式虚存管理技术存在性能问题 (TLBs 的有限性能):

  • 由于 TLBs 不再能覆盖工作集 (working sets: 指活动频繁的页表集), 导致 TLB 缺失 (TLB misses) 急剧上升
  • 在传统内存层次中, 利用 TLBs 完成地址转换后 (虚拟地址 -> 物理地址), 需要从 L1 Cache 开始进行缓存标签匹配, 查看所需内存数据是否已在缓存上. 因此,随意地增大 TLB 的大小以降低 TLB 缺失的方法会影响到所有内存操作的性能 (遍历 TLB 表耗时增大)

The amount of memory accessible from the TLB or The working set of each process is stored in the TLB: TLB Reach = (TLB Size) X (Page Size)

Old Methods

  • Clustered TLBs/Multi-Page Mappings: mapping multiple pages per page table entry
  • Transparent Huge Pages/Super-Paging: 使得每个页表项覆盖更大的内存; 但受限于架构, 页面大小最大为 1GB
  • Direct Segment: 将一个进程所需的虚拟内存直接映射到一个大段中; 但不利用操作系统的动态内存分配

Multi Page mapping

  • pack multiple page table entries(PTEs) into a single TLB entry
  • they pack only a small multiple of translations per entry, which limits their potential to reduce page-walks for large working sets

Transparent Huge pages

  • memory should be size-aligned and contiguous
  • many commodity processors provide limited numbers of large page TLB entries

Direct Segment Method

  • applications explicitly allocate a direct segment during startup
  • OS can reserve a single large contiguous range of physical memory for a segment

New Methods

two key challenges:

  • The address space of a process can be decomposed into many chunks of variable length segments, which can be allocated dynamically
  • address translation must be off the critical path: to support complicated many segment searches, address translation may take much longer compared to traditional fixed-sized page-based TLB lookups.

changes:

  • hardware: range TLB, CR-RT (Control Register - Range Translation)
  • software: process creation, memory allocation, range table management

RMM Benefits

  • transparent to applications
  • maps multiple ranges with no size-alignment restrictions
  • each range contains an unrestricted amount of memory
  • RMM has the potential to map more than 99% of memory for all workloads with 50 or fewer range translations
  • the range TLB achieves extremely high hit rates, eliminating the vast majority of costly page-walks compared to virtual memory systems that use paging alone.

RMM performance depends on the range TLB achieving a high hit ratio with few entries

Range Translation

Range translations are only base-page-aligned and redundant to paging: the page table still maps the entire virtual address space

  • hardware: range TLBs
  • software: range tables
  • OS: change lazy demand page allocation to eager paging, eager paging allocation

Eager paging instantiates pages in physical memory at allocation request time, rather than at first-access time as with demand paging.

Range Table

  • when miss: 及时填补 PTE 以使访存操作正常执行, 将 Range TLB 更新移出访存关键路径 (后台更新)
  • PTE 加入 range bit: 表示当前页是某个 Range 的一部分, 及时更新 Range TLB 和 Range Table
  • 利用 CR-RT 寄存器进行 Range Table 查找: OS handler 可以进行 Range Table 查找, 以简化硬件设计
  • 修改 INVLPG 指令: 使其同时无效化 Range TLB

Contiguous Memory Allocation

  • One motivation for demand paging was to limit unnecessary swapping in multi-programmed workloads, which modern large memories make less common.
  • RMM trades increased memory for better performance, a common tradeoff when memory is cheap and plentiful.
',229);t.render=function(s,e,t,o,l,p){return a(),n("div",null,[i])};export default t;export{e as __pageData}; diff --git a/assets/computerScience_virtualization_virtBasicNotes.md.f20c0b3e.lean.js b/assets/computerScience_virtualization_virtBasicNotes.md.f20c0b3e.lean.js new file mode 100644 index 00000000000..71d55059e12 --- /dev/null +++ b/assets/computerScience_virtualization_virtBasicNotes.md.f20c0b3e.lean.js @@ -0,0 +1 @@ +import{o as a,c as n,a as s}from"./app.6dd2a1b7.js";const e='{"title":"Virtualization Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"How to read paper","slug":"how-to-read-paper"},{"level":2,"title":"LXC","slug":"lxc"},{"level":3,"title":"Linux Namespaces","slug":"linux-namespaces"},{"level":3,"title":"chroot","slug":"chroot"},{"level":3,"title":"Control Groups","slug":"control-groups"},{"level":2,"title":"In-Memory Computing Thesis Notes","slug":"in-memory-computing-thesis-notes"},{"level":3,"title":"单节点内存计算","slug":"单节点内存计算"},{"level":3,"title":"分布式内存计算","slug":"分布式内存计算"},{"level":3,"title":"混合内存计算","slug":"混合内存计算"},{"level":2,"title":"Hadoop Thesis Notes","slug":"hadoop-thesis-notes"},{"level":3,"title":"Google File System","slug":"google-file-system"},{"level":3,"title":"Map Reduce","slug":"map-reduce"},{"level":3,"title":"Google Big Table","slug":"google-big-table"},{"level":2,"title":"Unikernel","slug":"unikernel"},{"level":3,"title":"Unikernels","slug":"unikernels"},{"level":3,"title":"libOS","slug":"libos"},{"level":3,"title":"OPAM","slug":"opam"},{"level":3,"title":"MirageOS","slug":"mirageos"},{"level":2,"title":"Xen Soft Device","slug":"xen-soft-device"},{"level":3,"title":"Device Access in Xen","slug":"device-access-in-xen"},{"level":2,"title":"Memory Management","slug":"memory-management"},{"level":3,"title":"Direct Segment","slug":"direct-segment"},{"level":3,"title":"Redundant Memory Mappings (RMM)","slug":"redundant-memory-mappings-rmm"}],"relativePath":"computerScience/virtualization/virtBasicNotes.md","lastUpdated":1627194884000}',t={},i=s('',229);t.render=function(s,e,t,o,l,p){return a(),n("div",null,[i])};export default t;export{e as __pageData}; diff --git a/assets/ctype.h.b7c42e1e.jpg b/assets/ctype.h.b7c42e1e.jpg new file mode 100644 index 00000000000..5c21e9271dc Binary files /dev/null and b/assets/ctype.h.b7c42e1e.jpg differ diff --git a/assets/foldl.8f7f9807.png b/assets/foldl.8f7f9807.png new file mode 100644 index 00000000000..23af745573c Binary files /dev/null and b/assets/foldl.8f7f9807.png differ diff --git a/assets/index.md.6d96cb4c.js b/assets/index.md.6d96cb4c.js new file mode 100644 index 00000000000..97840da90ec --- /dev/null +++ b/assets/index.md.6d96cb4c.js @@ -0,0 +1 @@ +import{o as a,c as s,a as e}from"./app.6dd2a1b7.js";const n='{"title":"Awesome Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"License","slug":"license"},{"level":2,"title":"Contact","slug":"contact"}],"relativePath":"index.md","lastUpdated":1627216236000}',t={},o=e('

Awesome Notes

                                   _\n  _      _____  ___  ___  _ __ ___   ___   _ __   ___ | |_ ___  ___\n   \\ /\\ / / _ \\/ __|/ _ \\| '_ ` _ \\ / _ \\ | '_ \\ / _ \\| __/ _ \\/ __|\n  \\ V  V /  __/\\__ \\ (_) | | | | | |  __/ | | | | (_) | ||  __/\\__ \\\n   \\_/\\_/ \\___||___/\\___/|_| |_| |_|\\___| |_| |_|\\___/ \\__\\___||___/\n
1
2
3
4
5

AuthorLICENSE

Code LinesContinuous Integration

Personal Learning Notes - Awesome Notes for Myself.

GitHub Version built on VitePress. GitBook Version built on GitBook.IO.

License

MIT License Copyright (c) sabertazimi

Contact

EmailTwitterGitHub

',10);t.render=function(e,n,t,p,r,i){return a(),s("div",null,[o])};export default t;export{n as __pageData}; diff --git a/assets/index.md.6d96cb4c.lean.js b/assets/index.md.6d96cb4c.lean.js new file mode 100644 index 00000000000..50ea78bffa2 --- /dev/null +++ b/assets/index.md.6d96cb4c.lean.js @@ -0,0 +1 @@ +import{o as a,c as s,a as e}from"./app.6dd2a1b7.js";const n='{"title":"Awesome Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"License","slug":"license"},{"level":2,"title":"Contact","slug":"contact"}],"relativePath":"index.md","lastUpdated":1627216236000}',t={},o=e('',10);t.render=function(e,n,t,p,r,i){return a(),s("div",null,[o])};export default t;export{n as __pageData}; diff --git a/assets/language_assembly_assemblyBasicNotes.md.8a26b6c6.js b/assets/language_assembly_assemblyBasicNotes.md.8a26b6c6.js new file mode 100644 index 00000000000..ccc19e3a86e --- /dev/null +++ b/assets/language_assembly_assemblyBasicNotes.md.8a26b6c6.js @@ -0,0 +1 @@ +import{o as a,c as s,a as n}from"./app.6dd2a1b7.js";const e='{"title":"Assembly Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Assembly Execution Model","slug":"assembly-execution-model"},{"level":2,"title":"Tools","slug":"tools"},{"level":3,"title":"GCC","slug":"gcc"},{"level":3,"title":"GDB","slug":"gdb"},{"level":3,"title":"MASM","slug":"masm"},{"level":3,"title":"LINK","slug":"link"},{"level":3,"title":"TD","slug":"td"},{"level":3,"title":"NMAKE","slug":"nmake"},{"level":3,"title":"Objdump","slug":"objdump"},{"level":2,"title":"位运算","slug":"位运算"},{"level":2,"title":"Address","slug":"address"},{"level":3,"title":"mov","slug":"mov"},{"level":3,"title":"address","slug":"address-1"},{"level":2,"title":"常用命令","slug":"常用命令"},{"level":3,"title":"读取指令","slug":"读取指令"},{"level":3,"title":"算术命令","slug":"算术命令"},{"level":3,"title":"转移命令","slug":"转移命令"},{"level":2,"title":"Control Flow Statement","slug":"control-flow-statement"},{"level":3,"title":"If Statement","slug":"if-statement"},{"level":3,"title":"Loop Statement","slug":"loop-statement"},{"level":3,"title":"Switch Statement","slug":"switch-statement"},{"level":2,"title":"Stack Frame","slug":"stack-frame"},{"level":2,"title":"Data Structure","slug":"data-structure"},{"level":3,"title":"Arrays","slug":"arrays"}],"relativePath":"language/assembly/assemblyBasicNotes.md","lastUpdated":1627196319000}',l={},t=n('

Assembly Basic Notes

Assembly Execution Model

Load-ALU-Store 模式 - 读入寄存器,算术逻辑单元运算,回写至内存

Tools

GCC

  • gcc -01/-02/-03 source.c —— 编译优化等级
  • gcc -S source.c —— 编译成汇编代码(文本文件)
  • gcc -o source.c —— 编译成 Object 代码(二进制文件)

GDB

  • si 单指令执行
  • x/i $pc   执行 si 命令时打印汇编代码
  • disas 显示汇编代码
  • bt 查看函数堆栈
  • step1  下一条汇编指令
  • info register
  • x /numsizeformat $pc/rsp/rbp

MASM

masm /Zi/Zd src dist\nmasm /I(path)           // 引用 标准库/宏\nmasm /I..\\include\n
1
2
3
link /DEBUG src\n
1

TD

  • tab 切换分区
  • alt 功能键
  • ctrl 子功能键

Basic

  • F2: break
  • F7: step into
  • F8: step over

Alt

  • Alt+F5 : 临时跳转至 dos 界面
  • Alt+Enter: 全屏
  • Alt+X : 退出

代码区

  • <C-A> Assemble: 临时修改汇编指令
  • <C-C> Caller : 从子程序处跳回至 Caller 处
  • <C-F> Follow : 查看 CALL/JMP/INT 跳转至的子程序处
  • <C-O> Origin : 跳转至 cs:ip 指向指令处

数据区

  • <C-C> Change: 临时修改数据
  • <C-D> Display: 选择显示格式 e.g Byte/Word/Long/Comp/Float/Real/Double/Extended
  • <C-G> Goto: 跳转至指定地址区

NMAKE

  • $<: 源文件名
  • $?: 所有源文件名
  • $@: 全路径的目标文件
  • $*: 除去扩展名的全路径的目标文件
EXE = $(NAME).exe\nOBJS = $(NAME).obj\nSRCS = $(NAME).asm\nSIMPLE_MODE = ;\n\nLINK_FLAG = /subsystem:windows\nML_FLAG = /c/coff/Zi\nMASM_FLAG = /Zi/Zd\n\n$(EXE): $(OBJS)\n    link $(LINK_FLAG) $(OBJS) $(SIMPLE_MODE)\n\n$(OBJS): $(SRCS)\n    masm $(MASM_FLAG) $(SRCS) $(SIMPLE_MODE)\n\nclean:\n    del *.obj\n    del *.tr\n\ntest:\n    td $(EXE)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Objdump

AT&T反汇编工具

位运算

  • ^:无进位
  • p && *p++ : NULL 检查

编码:2^n e.g 一副牌的编码 2 位牌色位+4 位数字位

Address

mov

movX src dest #X:b/w/l/q (1/2/4/8 字节)

address

  • D(Rb, Ri, S) —— MEM[ Reg[Rb] + S * Reg[Ri] + D]
  • leal 命令
    • leal D(Rb, Ri, S) dest - 将地址模式表达式的址存入 dest. 作用为快速给指针赋值p = &x[i]Mem[Reg[x] + i * size]->(x, size, I), 快速计算二次多项式 x + i * k.

常用命令

读取指令

mov 读取

内存数据重复读入寄存器 - 使得汇编代码与上下文无关,减少 BUG 产生可能性

R1 = MEM[SP + 8]\n……\nR1 = MEM[SP + 8]\n……\nR1 = MEM[SP + 8]\n……\n
1
2
3
4
5
6

算术命令

转移命令

  • Flags

    • CF:Carry Flag(unsigned) 进位标志——当有进位时设为 1
    • OF:OverFlow Flag(signed) 溢出标志——当补码溢出时设为 1
    • SF:Sign Flag(signed) 符号标志——当 t<0 时设为 1
    • ZF:Zero Flag 零标志——当 t==0 时设为 1
  • cmpX 命令 cmpl Src2, Src1 根据 Src1 – Src2(subl Src2, Src1)的结果设置标志寄存器的值

  • testX 命令 testl Src2, Src1 根据 Src1 & Src2(andl Src2, Src1)的结果设置标志寄存器的值

    e.g testl %eax, %eax 实现符号函数

  • setX 命令 根据标志寄存器运算值,将值存入 dest setX dest

Control Flow Statement

If Statement

cmovC src, dest(C 表示 Conditional——e/ne 等) 当条件成立时,src->dest src 与 dest 可分别用于存放两种情况的值

Loop Statement

Do While Statement

While Do Statement

For Statement

Switch Statement

Jump Table:

break; → leaveret

  • fallThrough(without break)
  • mixing(连冒号)
  • missing case

Stack Frame

# 准备阶段\n# Caller-Save: %eax %ecx %edx\n# Callee-Save: %ebx %esi %edi\n# 传参顺序: rdi, dsi, rdx, rcx, r8, r9, stack\n\npushl %ebp\nmovl %esp, %ebp\npushl %ebx\n\n# 结束阶段\n\nmovl -4(%ebp), %ebx\nmovl %ebp, %esp\npopl %ebp\nret\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Data Structure

Arrays

int get_sea_digit(int index, int dig) {\n    return sea[index][dig];\n}\n
1
2
3
# 访问二维数组元素\n# %ecx = dig\n# %eax = index\nleal 0(,%ecx,4), %edx        # 4 * dig\nleal (%eax,%eax,4), %eax     # 5 * index\nmovl sea(%edx,%eax,4), %eax  # * (sea + 4 * dig + 20 * index)\n
1
2
3
4
5
6
',61);l.render=function(n,e,l,p,o,c){return a(),s("div",null,[t])};export default l;export{e as __pageData}; diff --git a/assets/language_assembly_assemblyBasicNotes.md.8a26b6c6.lean.js b/assets/language_assembly_assemblyBasicNotes.md.8a26b6c6.lean.js new file mode 100644 index 00000000000..8e672867b59 --- /dev/null +++ b/assets/language_assembly_assemblyBasicNotes.md.8a26b6c6.lean.js @@ -0,0 +1 @@ +import{o as a,c as s,a as n}from"./app.6dd2a1b7.js";const e='{"title":"Assembly Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Assembly Execution Model","slug":"assembly-execution-model"},{"level":2,"title":"Tools","slug":"tools"},{"level":3,"title":"GCC","slug":"gcc"},{"level":3,"title":"GDB","slug":"gdb"},{"level":3,"title":"MASM","slug":"masm"},{"level":3,"title":"LINK","slug":"link"},{"level":3,"title":"TD","slug":"td"},{"level":3,"title":"NMAKE","slug":"nmake"},{"level":3,"title":"Objdump","slug":"objdump"},{"level":2,"title":"位运算","slug":"位运算"},{"level":2,"title":"Address","slug":"address"},{"level":3,"title":"mov","slug":"mov"},{"level":3,"title":"address","slug":"address-1"},{"level":2,"title":"常用命令","slug":"常用命令"},{"level":3,"title":"读取指令","slug":"读取指令"},{"level":3,"title":"算术命令","slug":"算术命令"},{"level":3,"title":"转移命令","slug":"转移命令"},{"level":2,"title":"Control Flow Statement","slug":"control-flow-statement"},{"level":3,"title":"If Statement","slug":"if-statement"},{"level":3,"title":"Loop Statement","slug":"loop-statement"},{"level":3,"title":"Switch Statement","slug":"switch-statement"},{"level":2,"title":"Stack Frame","slug":"stack-frame"},{"level":2,"title":"Data Structure","slug":"data-structure"},{"level":3,"title":"Arrays","slug":"arrays"}],"relativePath":"language/assembly/assemblyBasicNotes.md","lastUpdated":1627196319000}',l={},t=n('',61);l.render=function(n,e,l,p,o,c){return a(),s("div",null,[t])};export default l;export{e as __pageData}; diff --git a/assets/language_c_cBasicNotes.md.4b46ee41.js b/assets/language_c_cBasicNotes.md.4b46ee41.js new file mode 100644 index 00000000000..fbfd60fbdf0 --- /dev/null +++ b/assets/language_c_cBasicNotes.md.4b46ee41.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as e}from"./app.6dd2a1b7.js";const t='{"title":"C Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"编程习惯","slug":"编程习惯"},{"level":3,"title":"Macro(宏)","slug":"macro-宏"},{"level":3,"title":"头文件","slug":"头文件"},{"level":3,"title":"检查","slug":"检查"},{"level":2,"title":"类型转换","slug":"类型转换"},{"level":3,"title":"机器码转换","slug":"机器码转换"},{"level":2,"title":"Pointer Tips and Best Practice","slug":"pointer-tips-and-best-practice"},{"level":3,"title":"Error Prone Pointers","slug":"error-prone-pointers"},{"level":3,"title":"Debugging Malloc","slug":"debugging-malloc"},{"level":3,"title":"利用 void 指针实现 Generic","slug":"利用-void-指针实现-generic"},{"level":2,"title":"Useful Functions","slug":"useful-functions"},{"level":3,"title":"memset","slug":"memset"},{"level":3,"title":"free","slug":"free"},{"level":3,"title":"Strings","slug":"strings"},{"level":3,"title":"I/O","slug":"i-o"},{"level":3,"title":"Exceptions","slug":"exceptions"},{"level":3,"title":"Process","slug":"process"},{"level":3,"title":"Threads","slug":"threads"},{"level":2,"title":"联合体","slug":"联合体"},{"level":2,"title":"Naming Conventions","slug":"naming-conventions"},{"level":3,"title":"常用缩写词","slug":"常用缩写词"},{"level":3,"title":"Header File","slug":"header-file"},{"level":2,"title":"C Standard Library","slug":"c-standard-library"},{"level":3,"title":"Assert","slug":"assert"},{"level":3,"title":"Types","slug":"types"},{"level":3,"title":"Errno","slug":"errno"},{"level":3,"title":"Float and Limits","slug":"float-and-limits"},{"level":3,"title":"Locale","slug":"locale"},{"level":3,"title":"Math","slug":"math"},{"level":3,"title":"SetJMP","slug":"setjmp"},{"level":3,"title":"Signal","slug":"signal"},{"level":3,"title":"Stdarg","slug":"stdarg"},{"level":3,"title":"Stddef","slug":"stddef"},{"level":3,"title":"String","slug":"string"},{"level":2,"title":"GDB Tutorial","slug":"gdb-tutorial"},{"level":3,"title":"Basic Command","slug":"basic-command"},{"level":3,"title":"GDB Set Command","slug":"gdb-set-command"},{"level":3,"title":"GDB Shell","slug":"gdb-shell"},{"level":3,"title":"GDB Assemble","slug":"gdb-assemble"},{"level":3,"title":"GDB Stack Frame","slug":"gdb-stack-frame"},{"level":2,"title":"GCC","slug":"gcc"},{"level":2,"title":"Awesome Tools","slug":"awesome-tools"}],"relativePath":"language/c/cBasicNotes.md","lastUpdated":1627196319000}',l={},o=a("h1",{id:"c-basic-notes"},[a("a",{class:"header-anchor",href:"#c-basic-notes","aria-hidden":"true"},"#"),e(" C Basic Notes")],-1),c=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#c-basic-notes"},"C Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#编程习惯"},"编程习惯")]),a("li",null,[a("a",{href:"#类型转换"},"类型转换")]),a("li",null,[a("a",{href:"#pointer-tips-and-best-practice"},"Pointer Tips and Best Practice")]),a("li",null,[a("a",{href:"#useful-functions"},"Useful Functions")]),a("li",null,[a("a",{href:"#联合体"},"联合体")]),a("li",null,[a("a",{href:"#naming-conventions"},"Naming Conventions")]),a("li",null,[a("a",{href:"#c-standard-library"},"C Standard Library")]),a("li",null,[a("a",{href:"#gdb-tutorial"},"GDB Tutorial")]),a("li",null,[a("a",{href:"#gcc"},"GCC")]),a("li",null,[a("a",{href:"#awesome-tools"},"Awesome Tools")])])])])])],-1),r=a("h2",{id:"编程习惯"},[a("a",{class:"header-anchor",href:"#编程习惯","aria-hidden":"true"},"#"),e(" 编程习惯")],-1),p=a("h3",{id:"macro-宏"},[a("a",{class:"header-anchor",href:"#macro-宏","aria-hidden":"true"},"#"),e(" Macro(宏)")],-1),i=a("h4",{id:"括号"},[a("a",{class:"header-anchor",href:"#括号","aria-hidden":"true"},"#"),e(" 括号")],-1),u=a("p",null,"尽量添加足够的括号,减少宏定义的二义性",-1),k=a("h4",{id:"特殊用法"},[a("a",{class:"header-anchor",href:"#特殊用法","aria-hidden":"true"},"#"),e(" 特殊用法")],-1),d=a("ul",null,[a("li",null,[a("code",null,"#"),e(": 字符串化")]),a("li",null,[a("code",null,"##"),e(": 强制连接符")]),a("li",null,"do { ... } while (0): 防止语法错误")],-1),m=a("h3",{id:"头文件"},[a("a",{class:"header-anchor",href:"#头文件","aria-hidden":"true"},"#"),e(" 头文件")],-1),b=a("h4",{id:"缺少标准库头文件"},[a("a",{class:"header-anchor",href:"#缺少标准库头文件","aria-hidden":"true"},"#"),e(" 缺少标准库头文件")],-1),h=a("h5",{id:"缺少函数原型"},[a("a",{class:"header-anchor",href:"#缺少函数原型","aria-hidden":"true"},"#"),e(" 缺少函数原型")],-1),y=a("p",null,[e("链接成功 - 链接器自动装载库函数,不影响程序执行 "),a("strong",null,"只警告,不报错")],-1),f=a("h5",{id:"覆盖标准库函数原型"},[a("a",{class:"header-anchor",href:"#覆盖标准库函数原型","aria-hidden":"true"},"#"),e(" 覆盖标准库函数原型")],-1),v=a("ul",null,[a("li",null,"定义过多参数原型,调用时传入过多参数,函数正确执行(无视多余参数)"),a("li",null,"定义缺少参数原型,调用时传入不完整参数,函数错误执行,误把 0xc(%ebp),0x10(%ebp),…等更多内存单元当作函数参数")],-1),g=a("h5",{id:"缺少宏定义"},[a("a",{class:"header-anchor",href:"#缺少宏定义","aria-hidden":"true"},"#"),e(" 缺少宏定义")],-1),w=a("p",null,"链接失败 - 宏定义会被识别为函数,但链接器查找不到相应库函数",-1),x=a("h4",{id:"防止重复包括头文件"},[a("a",{class:"header-anchor",href:"#防止重复包括头文件","aria-hidden":"true"},"#"),e(" 防止重复包括头文件")],-1),E=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"ifndef"),e(),a("span",{class:"token expression"},"_FILENAME_H_")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"_FILENAME_H_")]),e("\n\n"),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n"),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n"),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"endif")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),_=a("h4",{id:"头文件不申请内存单元"},[a("a",{class:"header-anchor",href:"#头文件不申请内存单元","aria-hidden":"true"},"#"),e(" 头文件不申请内存单元")],-1),N=a("p",null,"除了全局共享静态变量外, 头文件中的定义不允许申请实际的内存单元",-1),S=a("h3",{id:"检查"},[a("a",{class:"header-anchor",href:"#检查","aria-hidden":"true"},"#"),e(" 检查")],-1),T=a("h4",{id:"边界检查"},[a("a",{class:"header-anchor",href:"#边界检查","aria-hidden":"true"},"#"),e(" 边界检查")],-1),O=a("ul",null,[a("li",null,"空/满栈检查"),a("li",null,"参数合法性检查 e.g elemSize > 0 检查")],-1),L=a("h4",{id:"指针检查"},[a("a",{class:"header-anchor",href:"#指针检查","aria-hidden":"true"},"#"),e(" 指针检查")],-1),A=a("ul",null,[a("li",null,[e("Alloctor 失败,需添加 NULL 检查: "),a("ul",null,[a("li",null,"assert"),a("li",null,"exit")])])],-1),C=a("h2",{id:"类型转换"},[a("a",{class:"header-anchor",href:"#类型转换","aria-hidden":"true"},"#"),e(" 类型转换")],-1),D=a("h3",{id:"机器码转换"},[a("a",{class:"header-anchor",href:"#机器码转换","aria-hidden":"true"},"#"),e(" 机器码转换")],-1),R=a("ul",null,[a("li",null,"有符号类型转换: 进行符号扩展"),a("li",null,"无符号类型转换: 进行零扩展")],-1),I=a("h2",{id:"pointer-tips-and-best-practice"},[a("a",{class:"header-anchor",href:"#pointer-tips-and-best-practice","aria-hidden":"true"},"#"),e(" Pointer Tips and Best Practice")],-1),P=a("h3",{id:"error-prone-pointers"},[a("a",{class:"header-anchor",href:"#error-prone-pointers","aria-hidden":"true"},"#"),e(" Error Prone Pointers")],-1),M=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"37"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"float"),e(" f "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"float"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("i"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"float"),e(" f "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"7.0"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"short"),e(" s "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"short"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("f"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),B=a("ul",null,[a("li",null,"悬挂指针"),a("li",null,"未初始化"),a("li",null,[e("改写未知区域 "),a("ul",null,[a("li",null,"下标越界"),a("li",null,[e("内存上溢 e.g "),a("code",null,"gets(string);")])])]),a("li",null,"指针相关运算符优先级与结合性"),a("li",null,"返回局部变量的地址"),a("li",null,"重复释放内存空间"),a("li",null,"内存泄漏 e.g 未释放空间/未释放部分深度空间(多维数组)"),a("li",null,"不能引用 void 指针指向的内存单元")],-1),U=a("h3",{id:"debugging-malloc"},[a("a",{class:"header-anchor",href:"#debugging-malloc","aria-hidden":"true"},"#"),e(" Debugging Malloc")],-1),F=a("h4",{id:"处理-void-指针"},[a("a",{class:"header-anchor",href:"#处理-void-指针","aria-hidden":"true"},"#"),e(" 处理 void 指针")],-1),G=a("p",null,"Tips: 中途运用强制类型转换,使得 void 指针可以执行指针加减运算",-1),H=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("target "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("void_pointer "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),z=a("h3",{id:"利用-void-指针实现-generic"},[a("a",{class:"header-anchor",href:"#利用-void-指针实现-generic","aria-hidden":"true"},"#"),e(" 利用 void 指针实现 Generic")],-1),X=a("h4",{id:"通用型-swap-函数"},[a("a",{class:"header-anchor",href:"#通用型-swap-函数","aria-hidden":"true"},"#"),e(" 通用型 Swap 函数")],-1),j=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"swap"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("vp1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("vp2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" size"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"char"),e(" buffer"),a("span",{class:"token punctuation"},"["),e("size"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("buffer"),a("span",{class:"token punctuation"},","),e(" vp1"),a("span",{class:"token punctuation"},","),e(" size"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("vp1"),a("span",{class:"token punctuation"},","),e(" vp2"),a("span",{class:"token punctuation"},","),e(" size"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("vp2"),a("span",{class:"token punctuation"},","),e(" buffer"),a("span",{class:"token punctuation"},","),e(" size"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),q=a("h4",{id:"通用型-search-function"},[a("a",{class:"header-anchor",href:"#通用型-search-function","aria-hidden":"true"},"#"),e(" 通用型 Search Function")],-1),K=a("h5",{id:"实现"},[a("a",{class:"header-anchor",href:"#实现","aria-hidden":"true"},"#"),e(" 实现")],-1),V=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token function"},"lsearch"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("key"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("base"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" n"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" elemSize"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),e("cmp_fn"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("i "),a("span",{class:"token operator"},"<"),e(" n"),a("span",{class:"token punctuation"},";"),e("i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e(" elemAddr "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("base "),a("span",{class:"token operator"},"+"),e(" i "),a("span",{class:"token operator"},"*"),e(" elemSize"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"cmp_fn"),a("span",{class:"token punctuation"},"("),e("key"),a("span",{class:"token punctuation"},","),e(" elemAddr"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(" elemAddr"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Y=a("h5",{id:"int-实例"},[a("a",{class:"header-anchor",href:"#int-实例","aria-hidden":"true"},"#"),e(" int 实例")],-1),W=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token function"},"IntCmp"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elem1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elem2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),e("ip1 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("elem1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),e("ip2 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("elem2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token operator"},"*"),e("ip1 "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token operator"},"*"),e("ip2"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Q=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(" array"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),e("\n size "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),e("\n target "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"// 应进行强制类型转换"),e("\n"),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),e(" found "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token function"},"lsearch"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"&"),e("target"),a("span",{class:"token punctuation"},","),e(" array"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(" IntCmp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("found "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Not Found"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Found"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),J=a("h5",{id:"string-实例"},[a("a",{class:"header-anchor",href:"#string-实例","aria-hidden":"true"},"#"),e(" string 实例")],-1),$=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token function"},"StrCmp"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("vp1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("vp2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 必须进行强制类型转换"),e("\n "),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("s1 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("vp1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("s2 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("vp2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token function"},"strcmp"),a("span",{class:"token punctuation"},"("),e("s1"),a("span",{class:"token punctuation"},","),e(" s2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Z=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("notes"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token string"},'"Ab"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"F#"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"B"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"Gb"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"D"'),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token operator"},"*"),e("target "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Eb"'),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),e(" found "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"lsearch"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"&"),e("target"),a("span",{class:"token punctuation"},","),e(" notes"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(" StrCmp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),nn=a("h4",{id:"泛型数据结构"},[a("a",{class:"header-anchor",href:"#泛型数据结构","aria-hidden":"true"},"#"),e(" 泛型数据结构")],-1),sn=a("h5",{id:"通用型栈"},[a("a",{class:"header-anchor",href:"#通用型栈","aria-hidden":"true"},"#"),e(" 通用型栈")],-1),an=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"typedef"),e(),a("span",{class:"token keyword"},"struct"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elements"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" elemSize"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" logLen"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" allocLen"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e(" stack"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackNew"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackDispose"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackPush"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elemAddr"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackPop"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elemAddr"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),en=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackNew"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" elemSize"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 参数合法性检查"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("elemSize "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"perror"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"ElemSize <= 0"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n s"),a("span",{class:"token operator"},"->"),e("elemSize "),a("span",{class:"token operator"},"="),e(" elemSize"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("allocLen "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token function"},"malloc"),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("allocLen "),a("span",{class:"token operator"},"*"),e(" elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// NULL检查"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"perror"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"No Mem"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackPush"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elemAddr"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 满栈检查"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"=="),e(" s"),a("span",{class:"token operator"},"->"),e("allocLen"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n s"),a("span",{class:"token operator"},"->"),e("allocLen "),a("span",{class:"token operator"},"*="),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token function"},"malloc"),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("elements"),a("span",{class:"token punctuation"},","),e(" s"),a("span",{class:"token operator"},"->"),e("allocLen "),a("span",{class:"token operator"},"*"),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("target "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"+"),e(" s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"*"),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("target"),a("span",{class:"token punctuation"},","),e(" elemAddr"),a("span",{class:"token punctuation"},","),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("logLen"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackPop"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elemAddr"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 空栈检查"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"perror"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Empty Stack"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n s"),a("span",{class:"token operator"},"->"),e("logLen"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("source "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"+"),e(" s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"*"),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("elemAddr"),a("span",{class:"token punctuation"},","),e(" source"),a("span",{class:"token punctuation"},","),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br")])],-1),tn=a("h4",{id:"tools"},[a("a",{class:"header-anchor",href:"#tools","aria-hidden":"true"},"#"),e(" Tools")],-1),ln=a("p",null,[e("Valgrind - "),a("a",{href:"https://github.com/svn2github/valgrind",target:"_blank",rel:"noopener noreferrer"},"GitHub Repo")],-1),on=a("h2",{id:"useful-functions"},[a("a",{class:"header-anchor",href:"#useful-functions","aria-hidden":"true"},"#"),e(" Useful Functions")],-1),cn=a("h3",{id:"memset"},[a("a",{class:"header-anchor",href:"#memset","aria-hidden":"true"},"#"),e(" memset")],-1),rn=a("h3",{id:"free"},[a("a",{class:"header-anchor",href:"#free","aria-hidden":"true"},"#"),e(" free")],-1),pn=a("p",null,[a("strong",null,"free 函数会回退 4/8 字节,取出 heap 块的长度/信息,根据此信息进行 heap 块的释放.")],-1),un=a("h3",{id:"strings"},[a("a",{class:"header-anchor",href:"#strings","aria-hidden":"true"},"#"),e(" Strings")],-1),kn=a("h4",{id:"strdup"},[a("a",{class:"header-anchor",href:"#strdup","aria-hidden":"true"},"#"),e(" strdup")],-1),dn=a("p",null,[e("string duplicate - "),a("code",null,"char *strdup(string)"),e(" 封装 allocator 细节")],-1),mn=a("h4",{id:"strchr-and-strstr"},[a("a",{class:"header-anchor",href:"#strchr-and-strstr","aria-hidden":"true"},"#"),e(" strchr and strstr")],-1),bn=a("p",null,"返回字符/串在字符串中出现的位置(地址)",-1),hn=a("h4",{id:"strtok"},[a("a",{class:"header-anchor",href:"#strtok","aria-hidden":"true"},"#"),e(" strtok")],-1),yn=a("h4",{id:"strcasecmp"},[a("a",{class:"header-anchor",href:"#strcasecmp","aria-hidden":"true"},"#"),e(" strcasecmp")],-1),fn=a("h4",{id:"getopt"},[a("a",{class:"header-anchor",href:"#getopt","aria-hidden":"true"},"#"),e(" getopt")],-1),vn=a("p",null,"解析命令行参数, 轻松地提取以 - 或 / 开头的参数",-1),gn=a("h3",{id:"i-o"},[a("a",{class:"header-anchor",href:"#i-o","aria-hidden":"true"},"#"),e(" I/O")],-1),wn=a("h4",{id:"string-scanf"},[a("a",{class:"header-anchor",href:"#string-scanf","aria-hidden":"true"},"#"),e(" String Scanf")],-1),xn=a("p",null,"可以用作简易匹配读取函数",-1),En=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 提取除 http:// 外的字符串"),e("\n"),a("span",{class:"token function"},"sscanf"),a("span",{class:"token punctuation"},"("),e("buf"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"http://%s"'),a("span",{class:"token punctuation"},","),e(" url_part"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),_n=a("h3",{id:"exceptions"},[a("a",{class:"header-anchor",href:"#exceptions","aria-hidden":"true"},"#"),e(" Exceptions")],-1),Nn=a("p",null,"perror(string) - 用来将上一个函数发生错误的原因输出到标准设备(stderr)",-1),Sn=a("h3",{id:"process"},[a("a",{class:"header-anchor",href:"#process","aria-hidden":"true"},"#"),e(" Process")],-1),Tn=a("h4",{id:"fork-and-execve"},[a("a",{class:"header-anchor",href:"#fork-and-execve","aria-hidden":"true"},"#"),e(" Fork and Execve")],-1),On=a("ul",null,[a("li",null,"fork(): 创建当前进程的拷贝"),a("li",null,[e("execve(): 用另一程序的代码代替当前进程的代码 "),a("ul",null,[a("li",null,[a("code",null,"int execve(char *filename, char *argv[], char *env_p[])")])])])],-1),Ln=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"fork_exec"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("path"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("argv"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token class-name"},"pid_t"),e(" pid "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),e(),a("span",{class:"token operator"},"!="),e(" pid"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Parent: created a child %d\\n"'),a("span",{class:"token punctuation"},","),e(" pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Child: exec-ing new program now\\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"execv"),a("span",{class:"token punctuation"},"("),e("path"),a("span",{class:"token punctuation"},","),e(" argv"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"This line printed by parent only!\\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),An=a("h4",{id:"other"},[a("a",{class:"header-anchor",href:"#other","aria-hidden":"true"},"#"),e(" Other")],-1),Cn=a("ul",null,[a("li",null,"getpid()"),a("li",null,"wait(int *child_status)/waitpid(pid)"),a("li",null,"exit()")],-1),Dn=a("h3",{id:"threads"},[a("a",{class:"header-anchor",href:"#threads","aria-hidden":"true"},"#"),e(" Threads")],-1),Rn=a("h4",{id:"pthread"},[a("a",{class:"header-anchor",href:"#pthread","aria-hidden":"true"},"#"),e(" PThread")],-1),In=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"typedef"),e(),a("span",{class:"token keyword"},"unsigned"),e(),a("span",{class:"token keyword"},"long"),e(),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token class-name"},"pthread_t"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"/**\n * create thread\n * @param {指向线程标识符的指针} pthread_t *__thread\n * @param {设置线程属性} __const pthread_attr_t *__attr\n * @param {线程运行函数的起始地址} void *(*__start_routine) (void *)\n * @param {运行函数的参数} void *__arg\n */"),e("\n"),a("span",{class:"token keyword"},"extern"),e(),a("span",{class:"token keyword"},"int"),e(" pthread_create "),a("span",{class:"token function"},"__P"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token class-name"},"pthread_t"),e(),a("span",{class:"token operator"},"*"),e("__thread"),a("span",{class:"token punctuation"},","),e("\n __const "),a("span",{class:"token class-name"},"pthread_attr_t"),e(),a("span",{class:"token operator"},"*"),e("__attr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),e("__start_routine"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(", "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("__arg"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n  \n"),a("span",{class:"token comment"},"/**\n * 等待线程\n * @param {被等待的线程标识符} pthread_t __th\n * @param {一个用户定义的指针,它可以用来存储被等待线程的返回值} void **__thread_return\n */"),e("\n"),a("span",{class:"token keyword"},"extern"),e(),a("span",{class:"token keyword"},"int"),e(" pthread_join "),a("span",{class:"token function"},"__P"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"pthread_t"),e(" __th"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),e("__thread_return"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"/**\n * 退出线程\n * @param {函数的返回代码} (void *__retval)) __attribute__ ((__noreturn__)\n */"),e("\n"),a("span",{class:"token keyword"},"extern"),e(),a("span",{class:"token keyword"},"void"),e(" pthread_exit "),a("span",{class:"token function"},"__P"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("__retval"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"__attribute__"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),e("__noreturn__"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"// 一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码 ESRCH"),e("\n\n"),a("span",{class:"token comment"},"// 以下为互斥锁相关函数"),e("\n\npthread_mutex_init\npthread_mutexattr_init\n\n"),a("span",{class:"token comment"},"/**\n * 设置属性 pshared\n * PTHREAD_PROCESS_PRIVATE\n * PTHREAD_PROCESS_SHARED\n */"),e("\npthread_mutexattr_setpshared\n\n"),a("span",{class:"token comment"},"/**\n * 设置互斥锁类型\n * PTHREAD_MUTEX_NORMAL\n * PTHREAD_MUTEX_ERRORCHECK\n * PTHREAD_MUTEX_RECURSIVE\n * PTHREAD_MUTEX_DEFAULT\n */"),e("\npthread_mutexattr_settype\n\npthread_mutex_lock\npthread_mutex_unlock\npthread_delay_np\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br")])],-1),Pn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[e("InitThreadPackage"),a("span",{class:"token punctuation"},";"),e("\nThreadNew"),a("span",{class:"token punctuation"},";"),e("\nThreadSleep"),a("span",{class:"token punctuation"},";"),e("\nRunAllThreads"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"SemaphoreNew"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"SemaphoreWait"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"SemaphoreSignal"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Mn=a("h4",{id:"semaphore"},[a("a",{class:"header-anchor",href:"#semaphore","aria-hidden":"true"},"#"),e(" Semaphore")],-1),Bn=a("ul",null,[a("li",null,"哲学家就餐问题"),a("li",null,"将 Semaphore 变量的值在允许范围内(不至于使得线程锁失效)使得其取最大值,减少线程阻塞"),a("li",null,"EmptyBuf 8, FullBuf 0"),a("li",null,[e("双向通信,互相唤醒 - "),a("code",null,"Writer:sw(empty),ss(full);"),e(),a("code",null,"Reader:sw(full),ss(empty);")])],-1),Un=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"SellTickets"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" agent"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),e("ticketsNum"),a("span",{class:"token punctuation"},","),e(" Semaphore lock"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"while"),e(),a("span",{class:"token punctuation"},"("),e("true"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 当 lock == 0 时,当前进程阻塞, 等待 lock > 0"),e("\n "),a("span",{class:"token comment"},"// 当 lock > 0 时, 当前进程继续进行, 并且 lock--"),e("\n "),a("span",{class:"token function"},"SemaphoreWait"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),e("ticketsNum "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"break"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 票已售磬"),e("\n\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),e("ticketsNum"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 售出一张票"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Sell One Ticket.\\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// lock++ 使得 lock > 0"),e("\n "),a("span",{class:"token comment"},"// 若有其他进程调用了SemaphoreWait, 且因之前 lock == 0 而被阻塞, 则此时其他进程可继续进行"),e("\n "),a("span",{class:"token function"},"SemaphoreSignal"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n "),a("span",{class:"token comment"},"// break to here"),e("\n "),a("span",{class:"token comment"},"// 作用同循环内的 Signal 函数"),e("\n "),a("span",{class:"token function"},"SemaphoreSignal"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),Fn=a("h2",{id:"联合体"},[a("a",{class:"header-anchor",href:"#联合体","aria-hidden":"true"},"#"),e(" 联合体")],-1),Gn=a("ul",null,[a("li",null,"机器码 e.g 理解 IEEE 754 标准"),a("li",null,"区分大/小端模式")],-1),Hn=a("h2",{id:"naming-conventions"},[a("a",{class:"header-anchor",href:"#naming-conventions","aria-hidden":"true"},"#"),e(" Naming Conventions")],-1),zn=a("h3",{id:"常用缩写词"},[a("a",{class:"header-anchor",href:"#常用缩写词","aria-hidden":"true"},"#"),e(" 常用缩写词")],-1),Xn=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"left"}},"原词"),a("th",{style:{"text-align":"left"}},"缩写")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"left"}},"addition"),a("td",{style:{"text-align":"left"}},"add")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"answer"),a("td",{style:{"text-align":"left"}},"ans")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"array"),a("td",{style:{"text-align":"left"}},"arr")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"average"),a("td",{style:{"text-align":"left"}},"avg")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"buffer"),a("td",{style:{"text-align":"left"}},"buf 或 buff")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"capture"),a("td",{style:{"text-align":"left"}},"cap 或 capt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"check"),a("td",{style:{"text-align":"left"}},"chk")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"count"),a("td",{style:{"text-align":"left"}},"cnt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"column"),a("td",{style:{"text-align":"left"}},"col")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"control"),a("td",{style:{"text-align":"left"}},"ctrl")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"decode"),a("td",{style:{"text-align":"left"}},"dec")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"define"),a("td",{style:{"text-align":"left"}},"def")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"delete"),a("td",{style:{"text-align":"left"}},"del")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"destination"),a("td",{style:{"text-align":"left"}},"dst 或 dest")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"display"),a("td",{style:{"text-align":"left"}},"disp")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"division"),a("td",{style:{"text-align":"left"}},"div")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"encode"),a("td",{style:{"text-align":"left"}},"enc")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"environment"),a("td",{style:{"text-align":"left"}},"env")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"error"),a("td",{style:{"text-align":"left"}},"err")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"float"),a("td",{style:{"text-align":"left"}},"flt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"frequency"),a("td",{style:{"text-align":"left"}},"freq")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"header"),a("td",{style:{"text-align":"left"}},"hdr")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"index"),a("td",{style:{"text-align":"left"}},"idx")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"image"),a("td",{style:{"text-align":"left"}},"img")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"increment"),a("td",{style:{"text-align":"left"}},"inc")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"initialize"),a("td",{style:{"text-align":"left"}},"init")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"iteration"),a("td",{style:{"text-align":"left"}},"itr")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"length"),a("td",{style:{"text-align":"left"}},"len")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"memory"),a("td",{style:{"text-align":"left"}},"mem")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"middle"),a("td",{style:{"text-align":"left"}},"mid")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"make"),a("td",{style:{"text-align":"left"}},"mk")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"message"),a("td",{style:{"text-align":"left"}},"msg")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"multiplication"),a("td",{style:{"text-align":"left"}},"mul")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"number"),a("td",{style:{"text-align":"left"}},"num")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"operand"),a("td",{style:{"text-align":"left"}},"opnd")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"optimization"),a("td",{style:{"text-align":"left"}},"opt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"operator"),a("td",{style:{"text-align":"left"}},"optr")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"packet"),a("td",{style:{"text-align":"left"}},"pkt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"position"),a("td",{style:{"text-align":"left"}},"pos")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"previous"),a("td",{style:{"text-align":"left"}},"pre/prev")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"payload"),a("td",{style:{"text-align":"left"}},"type")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"pointer"),a("td",{style:{"text-align":"left"}},"ptr/pt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"return"),a("td",{style:{"text-align":"left"}},"code")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"record"),a("td",{style:{"text-align":"left"}},"rcd/rc")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"receive"),a("td",{style:{"text-align":"left"}},"recv")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"result"),a("td",{style:{"text-align":"left"}},"res")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"return"),a("td",{style:{"text-align":"left"}},"ret")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"source"),a("td",{style:{"text-align":"left"}},"src")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"stack"),a("td",{style:{"text-align":"left"}},"stk")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"string"),a("td",{style:{"text-align":"left"}},"str")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"subtraction"),a("td",{style:{"text-align":"left"}},"sub")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"table"),a("td",{style:{"text-align":"left"}},"tab")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"temporary"),a("td",{style:{"text-align":"left"}},"tmp 或 temp")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"total"),a("td",{style:{"text-align":"left"}},"tot")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"time"),a("td",{style:{"text-align":"left"}},"stamp")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"value"),a("td",{style:{"text-align":"left"}},"val")])])],-1),jn=a("h3",{id:"header-file"},[a("a",{class:"header-anchor",href:"#header-file","aria-hidden":"true"},"#"),e(" Header File")],-1),qn=a("p",null,"防止其他文件重复#include 本文件",-1),Kn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"ifndef"),e(),a("span",{class:"token expression"},"MONGOOSE_HEADER_INCLUDED")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(" "),a("span",{class:"token macro-name"},"MONGOOSE_HEADER_INCLUDED")]),e("\n\n"),a("span",{class:"token comment"},"/*.................................\n * do something here\n *.................................\n */"),e("\n\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"endif"),e(),a("span",{class:"token comment"},"/* MONGOOSE_HEADER_INCLUDED */")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),Vn=a("h2",{id:"c-standard-library"},[a("a",{class:"header-anchor",href:"#c-standard-library","aria-hidden":"true"},"#"),e(" C Standard Library")],-1),Yn=a("h3",{id:"assert"},[a("a",{class:"header-anchor",href:"#assert","aria-hidden":"true"},"#"),e(" Assert")],-1),Wn=a("ul",null,[a("li",null,"关闭断言")],-1),Qn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"NDEBUG")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),e(),a("span",{class:"token string"},"")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Jn=a("ul",null,[a("li",null,"开启断言")],-1),$n=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"undef"),e(),a("span",{class:"token expression"},"NDEBUG")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),e(),a("span",{class:"token string"},"")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Zn=a("h3",{id:"types"},[a("a",{class:"header-anchor",href:"#types","aria-hidden":"true"},"#"),e(" Types")],-1),ns=a("ul",null,[a("li",null,"可检测字符")],-1),ss=a("p",null,"getc、fgetc、getchar 函数可返回值(EOF 值/unsigned char 类型)",-1),as=a("ul",null,[a("li",null,"不可检测字符")],-1),es=a("p",null,[e("非 EOF 值/非 unsigned char 类型("),a("em",null,"会引发严重错误"),e(")")],-1),ts=a("p",null,[a("img",{src:"/awesome-notes/assets/ctype.h.b7c42e1e.jpg",alt:"CType Functions"})],-1),ls=a("h3",{id:"errno"},[a("a",{class:"header-anchor",href:"#errno","aria-hidden":"true"},"#"),e(" Errno")],-1),os=a("ul",null,[a("li",null,"errno 的值在程序启动时为零,但是不会被任何库函数设为零")],-1),cs=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[e("errno "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\ny "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"sqrt"),a("span",{class:"token punctuation"},"("),e("x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("errno "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"invalid x : %e\\n"'),a("span",{class:"token punctuation"},","),e(" x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),rs=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"ifndef"),e(),a("span",{class:"token expression"},"_I386_ERRNO_H")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"_I386_ERRNO_H")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPERM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"1"),e()]),a("span",{class:"token comment"},"/* Operation not permitted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOENT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"2"),e()]),a("span",{class:"token comment"},"/* No such file or directory */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESRCH"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"3"),e()]),a("span",{class:"token comment"},"/* No such process */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EINTR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"4"),e()]),a("span",{class:"token comment"},"/* Interrupted system call */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EIO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"5"),e()]),a("span",{class:"token comment"},"/* I/O error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENXIO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"6"),e()]),a("span",{class:"token comment"},"/* No such device or address */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"E2BIG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"7"),e()]),a("span",{class:"token comment"},"/* Arg list too long */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOEXEC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"8"),e()]),a("span",{class:"token comment"},"/* Exec format error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADF"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"9"),e()]),a("span",{class:"token comment"},"/* Bad file number */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECHILD"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"10"),e()]),a("span",{class:"token comment"},"/* No child processes */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EAGAIN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"11"),e()]),a("span",{class:"token comment"},"/* Try again */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOMEM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"12"),e()]),a("span",{class:"token comment"},"/* Out of memory */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EACCES"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"13"),e()]),a("span",{class:"token comment"},"/* Permission denied */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EFAULT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"14"),e()]),a("span",{class:"token comment"},"/* Bad address */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTBLK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"15"),e()]),a("span",{class:"token comment"},"/* Block device required */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBUSY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"16"),e()]),a("span",{class:"token comment"},"/* Device or resource busy */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EEXIST"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"17"),e()]),a("span",{class:"token comment"},"/* File exists */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EXDEV"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"18"),e()]),a("span",{class:"token comment"},"/* Cross-device link */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENODEV"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"19"),e()]),a("span",{class:"token comment"},"/* No such device */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTDIR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"20"),e()]),a("span",{class:"token comment"},"/* Not a directory */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EISDIR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"21"),e()]),a("span",{class:"token comment"},"/* Is a directory */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EINVAL"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"22"),e()]),a("span",{class:"token comment"},"/* Invalid argument */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENFILE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"23"),e()]),a("span",{class:"token comment"},"/* File table overflow */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMFILE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"24"),e()]),a("span",{class:"token comment"},"/* Too many open files */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTTY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"25"),e()]),a("span",{class:"token comment"},"/* Not a typewriter */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ETXTBSY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"26"),e()]),a("span",{class:"token comment"},"/* Text file busy */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EFBIG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"27"),e()]),a("span",{class:"token comment"},"/* File too large */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOSPC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"28"),e()]),a("span",{class:"token comment"},"/* No space left on device */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESPIPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"29"),e()]),a("span",{class:"token comment"},"/* Illegal seek */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EROFS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"30"),e()]),a("span",{class:"token comment"},"/* Read-only file system */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMLINK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"31"),e()]),a("span",{class:"token comment"},"/* Too many links */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPIPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"32"),e()]),a("span",{class:"token comment"},"/* Broken pipe */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDOM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"33"),e()]),a("span",{class:"token comment"},"/* Math argument out of domain of func */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ERANGE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"34"),e()]),a("span",{class:"token comment"},"/* Math result not representable */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDEADLK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"35"),e()]),a("span",{class:"token comment"},"/* Resource deadlock would occur */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENAMETOOLONG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"36"),e()]),a("span",{class:"token comment"},"/* File name too long */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOLCK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"37"),e()]),a("span",{class:"token comment"},"/* No record locks available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOSYS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"38"),e()]),a("span",{class:"token comment"},"/* Function not implemented */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTEMPTY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"39"),e()]),a("span",{class:"token comment"},"/* Directory not empty */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELOOP"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"40"),e()]),a("span",{class:"token comment"},"/* Too many symbolic links encountered */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EWOULDBLOCK"),e(),a("span",{class:"token expression"},"EAGAIN "),a("span",{class:"token comment"},"/* Operation would block */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOMSG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"42"),e()]),a("span",{class:"token comment"},"/* No message of desired type */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EIDRM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"43"),e()]),a("span",{class:"token comment"},"/* Identifier removed */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECHRNG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"44"),e()]),a("span",{class:"token comment"},"/* Channel number out of range */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EL2NSYNC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"45"),e()]),a("span",{class:"token comment"},"/* Level 2 not synchronized */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EL3HLT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"46"),e()]),a("span",{class:"token comment"},"/* Level 3 halted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EL3RST"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"47"),e()]),a("span",{class:"token comment"},"/* Level 3 reset */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELNRNG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"48"),e()]),a("span",{class:"token comment"},"/* Link number out of range */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EUNATCH"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"49"),e()]),a("span",{class:"token comment"},"/* Protocol driver not attached */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOCSI"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"50"),e()]),a("span",{class:"token comment"},"/* No CSI structure available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EL2HLT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"51"),e()]),a("span",{class:"token comment"},"/* Level 2 halted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"52"),e()]),a("span",{class:"token comment"},"/* Invalid exchange */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"53"),e()]),a("span",{class:"token comment"},"/* Invalid request descriptor */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EXFULL"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"54"),e()]),a("span",{class:"token comment"},"/* Exchange full */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOANO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"55"),e()]),a("span",{class:"token comment"},"/* No anode */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADRQC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"56"),e()]),a("span",{class:"token comment"},"/* Invalid request code */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADSLT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"57"),e()]),a("span",{class:"token comment"},"/* Invalid slot */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDEADLOCK"),e(),a("span",{class:"token expression"},"EDEADLK")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBFONT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"59"),e()]),a("span",{class:"token comment"},"/* Bad font file format */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOSTR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"60"),e()]),a("span",{class:"token comment"},"/* Device not a stream */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENODATA"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"61"),e()]),a("span",{class:"token comment"},"/* No data available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ETIME"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"62"),e()]),a("span",{class:"token comment"},"/* Timer expired */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOSR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"63"),e()]),a("span",{class:"token comment"},"/* Out of streams resources */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENONET"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"64"),e()]),a("span",{class:"token comment"},"/* Machine is not on the network */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOPKG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"65"),e()]),a("span",{class:"token comment"},"/* Package not installed */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EREMOTE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"66"),e()]),a("span",{class:"token comment"},"/* Object is remote */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOLINK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"67"),e()]),a("span",{class:"token comment"},"/* Link has been severed */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EADV"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"68"),e()]),a("span",{class:"token comment"},"/* Advertise error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESRMNT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"69"),e()]),a("span",{class:"token comment"},"/* Srmount error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECOMM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"70"),e()]),a("span",{class:"token comment"},"/* Communication error on send */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPROTO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"71"),e()]),a("span",{class:"token comment"},"/* Protocol error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMULTIHOP"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"72"),e()]),a("span",{class:"token comment"},"/* Multi hop attempted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDOTDOT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"73"),e()]),a("span",{class:"token comment"},"/* RFS specific error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADMSG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"74"),e()]),a("span",{class:"token comment"},"/* Not a data message */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EOVERFLOW"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"75"),e()]),a("span",{class:"token comment"},"/* Value too large for defined data type */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTUNIQ"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"76"),e()]),a("span",{class:"token comment"},"/* Name not unique on network */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADFD"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"77"),e()]),a("span",{class:"token comment"},"/* File descriptor in bad state */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EREMCHG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"78"),e()]),a("span",{class:"token comment"},"/* Remote address changed */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBACC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"79"),e()]),a("span",{class:"token comment"},"/* Can not access a needed shared library */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBBAD"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"80"),e()]),a("span",{class:"token comment"},"/* Accessing a corrupted shared library */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBSCN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"81"),e()]),a("span",{class:"token comment"},"/* .lib section in a.out corrupted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBMAX"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"82"),e()]),a("span",{class:"token comment"},"/* Attempting to link in too many shared libraries */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBEXEC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"83"),e()]),a("span",{class:"token comment"},"/* Cannot exec a shared library directly */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EILSEQ"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"84"),e()]),a("span",{class:"token comment"},"/* Illegal byte sequence */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ERESTART"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"85"),e()]),a("span",{class:"token comment"},"/* Interrupted system call should be restarted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESTRPIPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"86"),e()]),a("span",{class:"token comment"},"/* Streams pipe error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EUSERS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"87"),e()]),a("span",{class:"token comment"},"/* Too many users */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTSOCK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"88"),e()]),a("span",{class:"token comment"},"/* Socket operation on non-socket */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDESTADDRREQ"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"89"),e()]),a("span",{class:"token comment"},"/* Destination address required */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMSGSIZE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"90"),e()]),a("span",{class:"token comment"},"/* Message too long */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPROTOTYPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"91"),e()]),a("span",{class:"token comment"},"/* Protocol wrong type for socket */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOPROTOOPT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"92"),e()]),a("span",{class:"token comment"},"/* Protocol not available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPROTONOSUPPORT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"93"),e()]),a("span",{class:"token comment"},"/* Protocol not supported */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESOCKTNOSUPPORT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"94"),e()]),a("span",{class:"token comment"},"/* Socket type not supported */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EOPNOTSUPP"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"95"),e()]),a("span",{class:"token comment"},"/* Operation not supported on transport endpoint */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPFNOSUPPORT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"96"),e()]),a("span",{class:"token comment"},"/* Protocol family not supported */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EAFNOSUPPORT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"97"),e()]),a("span",{class:"token comment"},"/* Address family not supported by protocol */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EADDRINUSE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"98"),e()]),a("span",{class:"token comment"},"/* Address already in use */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EADDRNOTAVAIL"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"99"),e()]),a("span",{class:"token comment"},"/* Cannot assign requested address */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENETDOWN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"100"),e()]),a("span",{class:"token comment"},"/* Network is down */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENETUNREACH"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"101"),e()]),a("span",{class:"token comment"},"/* Network is unreachable */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENETRESET"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"102"),e()]),a("span",{class:"token comment"},"/* Network dropped connection because of reset */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECONNABORTED"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"103"),e()]),a("span",{class:"token comment"},"/* Software caused connection abort */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECONNRESET"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"104"),e()]),a("span",{class:"token comment"},"/* Connection reset by peer */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOBUFS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"105"),e()]),a("span",{class:"token comment"},"/* No buffer space available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EISCONN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"106"),e()]),a("span",{class:"token comment"},"/* Transport endpoint is already connected */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTCONN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"107"),e()]),a("span",{class:"token comment"},"/* Transport endpoint is not connected */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESHUTDOWN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"108"),e()]),a("span",{class:"token comment"},"/* Cannot send after transport endpoint shutdown */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ETOOMANYREFS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"109"),e()]),a("span",{class:"token comment"},"/* Too many references: cannot splice */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ETIMEDOUT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"110"),e()]),a("span",{class:"token comment"},"/* Connection timed out */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECONNREFUSED"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"111"),e()]),a("span",{class:"token comment"},"/* Connection refused */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EHOSTDOWN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"112"),e()]),a("span",{class:"token comment"},"/* Host is down */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EHOSTUNREACH"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"113"),e()]),a("span",{class:"token comment"},"/* No route to host */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EALREADY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"114"),e()]),a("span",{class:"token comment"},"/* Operation already in progress */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EINPROGRESS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"115"),e()]),a("span",{class:"token comment"},"/* Operation now in progress */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESTALE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"116"),e()]),a("span",{class:"token comment"},"/* Stale NFS file handle */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EUCLEAN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"117"),e()]),a("span",{class:"token comment"},"/* Structure needs cleaning */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTNAM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"118"),e()]),a("span",{class:"token comment"},"/* Not a XENIX named type file */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENAVAIL"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"119"),e()]),a("span",{class:"token comment"},"/* No XENIX semaphores available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EISNAM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"120"),e()]),a("span",{class:"token comment"},"/* Is a named type file */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EREMOTEIO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"121"),e()]),a("span",{class:"token comment"},"/* Remote I/O error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDQUOT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"122"),e()]),a("span",{class:"token comment"},"/* Quota exceeded */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOMEDIUM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"123"),e()]),a("span",{class:"token comment"},"/* No medium found */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMEDIUMTYPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"124"),e()]),a("span",{class:"token comment"},"/* Wrong medium type */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"endif")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br"),a("span",{class:"line-number"},"83"),a("br"),a("span",{class:"line-number"},"84"),a("br"),a("span",{class:"line-number"},"85"),a("br"),a("span",{class:"line-number"},"86"),a("br"),a("span",{class:"line-number"},"87"),a("br"),a("span",{class:"line-number"},"88"),a("br"),a("span",{class:"line-number"},"89"),a("br"),a("span",{class:"line-number"},"90"),a("br"),a("span",{class:"line-number"},"91"),a("br"),a("span",{class:"line-number"},"92"),a("br"),a("span",{class:"line-number"},"93"),a("br"),a("span",{class:"line-number"},"94"),a("br"),a("span",{class:"line-number"},"95"),a("br"),a("span",{class:"line-number"},"96"),a("br"),a("span",{class:"line-number"},"97"),a("br"),a("span",{class:"line-number"},"98"),a("br"),a("span",{class:"line-number"},"99"),a("br"),a("span",{class:"line-number"},"100"),a("br"),a("span",{class:"line-number"},"101"),a("br"),a("span",{class:"line-number"},"102"),a("br"),a("span",{class:"line-number"},"103"),a("br"),a("span",{class:"line-number"},"104"),a("br"),a("span",{class:"line-number"},"105"),a("br"),a("span",{class:"line-number"},"106"),a("br"),a("span",{class:"line-number"},"107"),a("br"),a("span",{class:"line-number"},"108"),a("br"),a("span",{class:"line-number"},"109"),a("br"),a("span",{class:"line-number"},"110"),a("br"),a("span",{class:"line-number"},"111"),a("br"),a("span",{class:"line-number"},"112"),a("br"),a("span",{class:"line-number"},"113"),a("br"),a("span",{class:"line-number"},"114"),a("br"),a("span",{class:"line-number"},"115"),a("br"),a("span",{class:"line-number"},"116"),a("br"),a("span",{class:"line-number"},"117"),a("br"),a("span",{class:"line-number"},"118"),a("br"),a("span",{class:"line-number"},"119"),a("br"),a("span",{class:"line-number"},"120"),a("br"),a("span",{class:"line-number"},"121"),a("br"),a("span",{class:"line-number"},"122"),a("br"),a("span",{class:"line-number"},"123"),a("br"),a("span",{class:"line-number"},"124"),a("br"),a("span",{class:"line-number"},"125"),a("br"),a("span",{class:"line-number"},"126"),a("br"),a("span",{class:"line-number"},"127"),a("br")])],-1),ps=a("h3",{id:"float-and-limits"},[a("a",{class:"header-anchor",href:"#float-and-limits","aria-hidden":"true"},"#"),e(" Float and Limits")],-1),is=a("ul",null,[a("li",null,[e("宏定义:"),a("code",null,"CHAR/UCHAR/SCHAR/SHRT/USHRT/INT/UINT/LONG/ULONG/FLT/DBL/LDBL"),e("有关的"),a("code",null,"MIN/MAX/EPSILON")])],-1),us=a("h3",{id:"locale"},[a("a",{class:"header-anchor",href:"#locale","aria-hidden":"true"},"#"),e(" Locale")],-1),ks=a("ul",null,[a("li",null,"实现时间/单位/货币等一系列的国际化"),a("li",null,"常用函数")],-1),ds=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[e("_CRTIMP "),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e(" __cdecl "),a("span",{class:"token function"},"setlocale"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"const"),e(),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n_CRTIMP "),a("span",{class:"token keyword"},"struct"),e(),a("span",{class:"token class-name"},"lconv"),e(),a("span",{class:"token operator"},"*"),e(" __cdecl "),a("span",{class:"token function"},"localeconv"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ms=a("ul",null,[a("li",null,"int 值")],-1),bs=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_ALL"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"0")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_COLLATE"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"1")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_CTYPE"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"2")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_MONETARY"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"3")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_NUMERIC"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"4")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_TIME"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"5")])]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),hs=a("h3",{id:"math"},[a("a",{class:"header-anchor",href:"#math","aria-hidden":"true"},"#"),e(" Math")],-1),ys=a("p",null,"数学函数库(包括后缀 f(float)/l(long double))",-1),fs=a("h3",{id:"setjmp"},[a("a",{class:"header-anchor",href:"#setjmp","aria-hidden":"true"},"#"),e(" SetJMP")],-1),vs=a("ul",null,[a("li",null,"常用函数")],-1),gs=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token function"},"setjmp"),a("span",{class:"token punctuation"},"("),e("jmp_buf env"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"longjmp"),a("span",{class:"token punctuation"},"("),e("jmp_buf env"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" val"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ws=a("ul",null,[a("li",null,[e("使用:用于 if/else、loop、switch 语句 "),a("ol",null,[a("li",null,"直接调用 setjmp 函数时,返回值为 0;"),a("li",null,"调用 longjmp 函数时,若 val 值不为 0,则跳转至上次 setjmp 返回值为 0 处,继续向后执行语句")])]),a("li",null,[e("功能 "),a("ol",null,[a("li",null,"实现非本地(局部)跳转(跨越多层函数调用栈进行跳转)"),a("li",null,"实现类 Java 异常机制(异常抛出及捕获)")])])],-1),xs=a("h3",{id:"signal"},[a("a",{class:"header-anchor",href:"#signal","aria-hidden":"true"},"#"),e(" Signal")],-1),Es=a("p",null,"信号处理程序中所有数据应为 volatile 类型",-1),_s=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[e("_CRTIMP "),a("span",{class:"token keyword"},"int"),e(" __cdecl "),a("span",{class:"token function"},"raise"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n_CRTIMP "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token punctuation"},"("),e("__cdecl "),a("span",{class:"token operator"},"*"),e(" __cdecl "),a("span",{class:"token function"},"signal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token punctuation"},"("),e("__cdecl "),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ns=a("h3",{id:"stdarg"},[a("a",{class:"header-anchor",href:"#stdarg","aria-hidden":"true"},"#"),e(" Stdarg")],-1),Ss=a("p",null,"用于编写可变参数函数",-1),Ts=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"printargs"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" arg1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},"/* 输出所有int类型的参数,直到-1结束 */"),e("\n"),a("span",{class:"token punctuation"},"{"),e("\n va_list ap"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" i"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"va_start"),a("span",{class:"token punctuation"},"("),e("ap"),a("span",{class:"token punctuation"},","),e(" arg1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"="),e(" arg1"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"va_arg"),a("span",{class:"token punctuation"},"("),e("ap"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"%d "'),a("span",{class:"token punctuation"},","),e(" i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"va_end"),a("span",{class:"token punctuation"},"("),e("ap"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"putchar"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'\\n'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Os=a("h3",{id:"stddef"},[a("a",{class:"header-anchor",href:"#stddef","aria-hidden":"true"},"#"),e(" Stddef")],-1),Ls=a("ul",null,[a("li",null,[e("宏 "),a("ul",null,[a("li",null,"NULL Null 指针常量"),a("li",null,[a("code",null,"offsetof(type, member-designator)"),e(" 获得字段在结构体中的偏移量")])])]),a("li",null,[e("类型 "),a("ul",null,[a("li",null,[a("code",null,"ptrdiff_t"),e(" 带符号的整数类型, 用来表示指针相减的结果类型")]),a("li",null,[a("code",null,"wchar_t"),e(" 宽字符类型")]),a("li",null,[a("code",null,"size_t"),e(" 无符号整数类型, 用来表示 sizeof 操作符的结果类型")])])])],-1),As=a("h3",{id:"string"},[a("a",{class:"header-anchor",href:"#string","aria-hidden":"true"},"#"),e(" String")],-1),Cs=a("ul",null,[a("li",null,"men 系函数:操作任意字符序列"),a("li",null,"strn 系函数:操作非空字符序列"),a("li",null,"str 系函数:操作字符串序列('\\0')")],-1),Ds=a("h2",{id:"gdb-tutorial"},[a("a",{class:"header-anchor",href:"#gdb-tutorial","aria-hidden":"true"},"#"),e(" GDB Tutorial")],-1),Rs=a("h3",{id:"basic-command"},[a("a",{class:"header-anchor",href:"#basic-command","aria-hidden":"true"},"#"),e(" Basic Command")],-1),Is=a("ul",null,[a("li",null,"r(run)"),a("li",null,"l(list)"),a("li",null,[e("b(break) "),a("ul",null,[a("li",null,"b line_num"),a("li",null,"b filename:line_num")])]),a("li",null,"display/format address")],-1),Ps=a("p",null,"e.g display/i $pc",-1),Ms=a("ul",null,[a("li",null,[a("p",null,"t 按二进制格式显示变量")]),a("li",null,[a("p",null,"d 按十进制格式显示变量")]),a("li",null,[a("p",null,"o 按八进制格式显示变量")]),a("li",null,[a("p",null,"u 按十六进制格式显示无符号整型")]),a("li",null,[a("p",null,"x 按十六进制格式显示变量")]),a("li",null,[a("p",null,"a 按十六进制格式显示变量")]),a("li",null,[a("p",null,"f 按浮点数格式显示变量")]),a("li",null,[a("p",null,"c 按字符格式显示变量")]),a("li",null,[a("p",null,"s 按字符串格式显示变量")]),a("li",null,[a("p",null,"disas 显示汇编代码")]),a("li",null,[a("p",null,"x /num-size-format $pc/rsp/rbp")])],-1),Bs=a("p",null,"e.g size:w(2 字节) format:x/d/s(十六进制/十进制/字符串) 2wx",-1),Us=a("ul",null,[a("li",null,[e("examine - 查看内存 n、f、u 是可选的参数 "),a("ul",null,[a("li",null,"n 是一个正整数,表示显示内存的长度,也就是说从当前地址向后显示几个地址的内容。"),a("li",null,"f 表示显示的格式,参见上面。如果地址所指的是字符串,那么格式可以是 s,如果地十是指令地址,那么格式可以是 i。"),a("li",null,"u 表示从当前地址往后请求的字节数,如果不指定的话,GDB 默认是 4 个 bytes. u 参数可以用下面的字符来代替,b 表示单字节,h 表示双字节,w 表示四字节,g 表示八字节. 当我们指定了字节长度后,GDB 会从指内存定的内存地址开始,读写指定字节,并把其当作一个值取出来. 表示一个内存地址.")])])],-1),Fs=a("blockquote",null,[a("p",null,"命令:x/3uh 0x54320 表示,从内存地址 0x54320 读取内容,h 表示以双字节为一个单位,3 表示三个单位,u 表示按十六进制显示。")],-1),Gs=a("ul",null,[a("li",null,[e("s step into "),a("ul",null,[a("li",null,"step1 下一条汇编指令")])]),a("li",null,"n next line"),a("li",null,"p print"),a("li",null,"q quit"),a("li",null,"up last stack"),a("li",null,"bt(back trace) function stack 显示堆栈回溯信息"),a("li",null,"info breakpoints/register")],-1),Hs=a("h3",{id:"gdb-set-command"},[a("a",{class:"header-anchor",href:"#gdb-set-command","aria-hidden":"true"},"#"),e(" GDB Set Command")],-1),zs=a("ul",null,[a("li",null,"set disassembly"),a("li",null,"set variable")],-1),Xs=a("h3",{id:"gdb-shell"},[a("a",{class:"header-anchor",href:"#gdb-shell","aria-hidden":"true"},"#"),e(" GDB Shell")],-1),js=a("p",null,"shell command",-1),qs=a("h3",{id:"gdb-assemble"},[a("a",{class:"header-anchor",href:"#gdb-assemble","aria-hidden":"true"},"#"),e(" GDB Assemble")],-1),Ks=a("h4",{id:"gdb-core-dump"},[a("a",{class:"header-anchor",href:"#gdb-core-dump","aria-hidden":"true"},"#"),e(" GDB Core Dump")],-1),Vs=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin class-name"},"ulimit"),e(" -c unlimited\ngdb -c core_file_path target_exe_path\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ys=a("h4",{id:"gdb-disasm"},[a("a",{class:"header-anchor",href:"#gdb-disasm","aria-hidden":"true"},"#"),e(" GDB Disasm")],-1),Ws=a("ul",null,[a("li",null,"CS Segment")],-1),Qs=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" disass\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" x/i\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" x/5i "),a("span",{class:"token variable"},"$pc"),e("\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" ni/si\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Js=a("ul",null,[a("li",null,"Registers")],-1),$s=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" i r\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" i r a\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" i r ds\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Zs=a("ul",null,[a("li",null,"DS Segment"),a("li",null,"SS Segment")],-1),na=a("h3",{id:"gdb-stack-frame"},[a("a",{class:"header-anchor",href:"#gdb-stack-frame","aria-hidden":"true"},"#"),e(" GDB Stack Frame")],-1),sa=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" bt\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" frame n\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" info locals\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),aa=a("h2",{id:"gcc"},[a("a",{class:"header-anchor",href:"#gcc","aria-hidden":"true"},"#"),e(" GCC")],-1),ea=a("ul",null,[a("li",null,"-E: cpp(c preprocessor) 预处理 => .i"),a("li",null,"-S: cll 编译 => .s"),a("li",null,"-c: as(assemble) 汇编 => .o"),a("li",null,"-time Time the execution of each subprocess"),a("li",null,[a("code",null,"-std="),e(" Assume that the input sources are for "),a("code",null,"")]),a("li",null,[a("code",null,"-B "),e(" Add "),a("code",null,""),e(" to the compiler's search paths")]),a("li",null,"-v Display the programs invoked by the compiler"),a("li",null,[a("code",null,"-o "),e(" Place the output into "),a("code",null,"")]),a("li",null,"-shared Create a shared library"),a("li",null,[a("code",null,"-Wall")]),a("li",null,[a("code",null,"-v --help")])],-1),ta=a("h2",{id:"awesome-tools"},[a("a",{class:"header-anchor",href:"#awesome-tools","aria-hidden":"true"},"#"),e(" Awesome Tools")],-1),la=a("ul",null,[a("li",null,"用于创建代码文档资料的 NDoc"),a("li",null,"用于生成解决方案的 NAnt"),a("li",null,"用于生成代码的 CodeSmith"),a("li",null,"用于监视代码的 FxCop"),a("li",null,"用于编译少量代码的 Snippet Compiler"),a("li",null,[e("两种不同的转换器工具:"),a("a",{href:"http://ASP.NET",target:"_blank",rel:"noopener noreferrer"},"ASP.NET"),e(" 版本转换器和 Visual Studio .NET 项目转换器")]),a("li",null,"用于生成正则表达式的 Regulator"),a("li",null,"用于分析程序集的 .NET Reflector"),a("li",null,"用于单元测试的 NUnit")],-1);l.render=function(a,e,t,l,oa,ca){return n(),s("div",null,[o,c,r,p,i,u,k,d,m,b,h,y,f,v,g,w,x,E,_,N,S,T,O,L,A,C,D,R,I,P,M,B,U,F,G,H,z,X,j,q,K,V,Y,W,Q,J,$,Z,nn,sn,an,en,tn,ln,on,cn,rn,pn,un,kn,dn,mn,bn,hn,yn,fn,vn,gn,wn,xn,En,_n,Nn,Sn,Tn,On,Ln,An,Cn,Dn,Rn,In,Pn,Mn,Bn,Un,Fn,Gn,Hn,zn,Xn,jn,qn,Kn,Vn,Yn,Wn,Qn,Jn,$n,Zn,ns,ss,as,es,ts,ls,os,cs,rs,ps,is,us,ks,ds,ms,bs,hs,ys,fs,vs,gs,ws,xs,Es,_s,Ns,Ss,Ts,Os,Ls,As,Cs,Ds,Rs,Is,Ps,Ms,Bs,Us,Fs,Gs,Hs,zs,Xs,js,qs,Ks,Vs,Ys,Ws,Qs,Js,$s,Zs,na,sa,aa,ea,ta,la])};export default l;export{t as __pageData}; diff --git a/assets/language_c_cBasicNotes.md.4b46ee41.lean.js b/assets/language_c_cBasicNotes.md.4b46ee41.lean.js new file mode 100644 index 00000000000..fbfd60fbdf0 --- /dev/null +++ b/assets/language_c_cBasicNotes.md.4b46ee41.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as e}from"./app.6dd2a1b7.js";const t='{"title":"C Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"编程习惯","slug":"编程习惯"},{"level":3,"title":"Macro(宏)","slug":"macro-宏"},{"level":3,"title":"头文件","slug":"头文件"},{"level":3,"title":"检查","slug":"检查"},{"level":2,"title":"类型转换","slug":"类型转换"},{"level":3,"title":"机器码转换","slug":"机器码转换"},{"level":2,"title":"Pointer Tips and Best Practice","slug":"pointer-tips-and-best-practice"},{"level":3,"title":"Error Prone Pointers","slug":"error-prone-pointers"},{"level":3,"title":"Debugging Malloc","slug":"debugging-malloc"},{"level":3,"title":"利用 void 指针实现 Generic","slug":"利用-void-指针实现-generic"},{"level":2,"title":"Useful Functions","slug":"useful-functions"},{"level":3,"title":"memset","slug":"memset"},{"level":3,"title":"free","slug":"free"},{"level":3,"title":"Strings","slug":"strings"},{"level":3,"title":"I/O","slug":"i-o"},{"level":3,"title":"Exceptions","slug":"exceptions"},{"level":3,"title":"Process","slug":"process"},{"level":3,"title":"Threads","slug":"threads"},{"level":2,"title":"联合体","slug":"联合体"},{"level":2,"title":"Naming Conventions","slug":"naming-conventions"},{"level":3,"title":"常用缩写词","slug":"常用缩写词"},{"level":3,"title":"Header File","slug":"header-file"},{"level":2,"title":"C Standard Library","slug":"c-standard-library"},{"level":3,"title":"Assert","slug":"assert"},{"level":3,"title":"Types","slug":"types"},{"level":3,"title":"Errno","slug":"errno"},{"level":3,"title":"Float and Limits","slug":"float-and-limits"},{"level":3,"title":"Locale","slug":"locale"},{"level":3,"title":"Math","slug":"math"},{"level":3,"title":"SetJMP","slug":"setjmp"},{"level":3,"title":"Signal","slug":"signal"},{"level":3,"title":"Stdarg","slug":"stdarg"},{"level":3,"title":"Stddef","slug":"stddef"},{"level":3,"title":"String","slug":"string"},{"level":2,"title":"GDB Tutorial","slug":"gdb-tutorial"},{"level":3,"title":"Basic Command","slug":"basic-command"},{"level":3,"title":"GDB Set Command","slug":"gdb-set-command"},{"level":3,"title":"GDB Shell","slug":"gdb-shell"},{"level":3,"title":"GDB Assemble","slug":"gdb-assemble"},{"level":3,"title":"GDB Stack Frame","slug":"gdb-stack-frame"},{"level":2,"title":"GCC","slug":"gcc"},{"level":2,"title":"Awesome Tools","slug":"awesome-tools"}],"relativePath":"language/c/cBasicNotes.md","lastUpdated":1627196319000}',l={},o=a("h1",{id:"c-basic-notes"},[a("a",{class:"header-anchor",href:"#c-basic-notes","aria-hidden":"true"},"#"),e(" C Basic Notes")],-1),c=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#c-basic-notes"},"C Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#编程习惯"},"编程习惯")]),a("li",null,[a("a",{href:"#类型转换"},"类型转换")]),a("li",null,[a("a",{href:"#pointer-tips-and-best-practice"},"Pointer Tips and Best Practice")]),a("li",null,[a("a",{href:"#useful-functions"},"Useful Functions")]),a("li",null,[a("a",{href:"#联合体"},"联合体")]),a("li",null,[a("a",{href:"#naming-conventions"},"Naming Conventions")]),a("li",null,[a("a",{href:"#c-standard-library"},"C Standard Library")]),a("li",null,[a("a",{href:"#gdb-tutorial"},"GDB Tutorial")]),a("li",null,[a("a",{href:"#gcc"},"GCC")]),a("li",null,[a("a",{href:"#awesome-tools"},"Awesome Tools")])])])])])],-1),r=a("h2",{id:"编程习惯"},[a("a",{class:"header-anchor",href:"#编程习惯","aria-hidden":"true"},"#"),e(" 编程习惯")],-1),p=a("h3",{id:"macro-宏"},[a("a",{class:"header-anchor",href:"#macro-宏","aria-hidden":"true"},"#"),e(" Macro(宏)")],-1),i=a("h4",{id:"括号"},[a("a",{class:"header-anchor",href:"#括号","aria-hidden":"true"},"#"),e(" 括号")],-1),u=a("p",null,"尽量添加足够的括号,减少宏定义的二义性",-1),k=a("h4",{id:"特殊用法"},[a("a",{class:"header-anchor",href:"#特殊用法","aria-hidden":"true"},"#"),e(" 特殊用法")],-1),d=a("ul",null,[a("li",null,[a("code",null,"#"),e(": 字符串化")]),a("li",null,[a("code",null,"##"),e(": 强制连接符")]),a("li",null,"do { ... } while (0): 防止语法错误")],-1),m=a("h3",{id:"头文件"},[a("a",{class:"header-anchor",href:"#头文件","aria-hidden":"true"},"#"),e(" 头文件")],-1),b=a("h4",{id:"缺少标准库头文件"},[a("a",{class:"header-anchor",href:"#缺少标准库头文件","aria-hidden":"true"},"#"),e(" 缺少标准库头文件")],-1),h=a("h5",{id:"缺少函数原型"},[a("a",{class:"header-anchor",href:"#缺少函数原型","aria-hidden":"true"},"#"),e(" 缺少函数原型")],-1),y=a("p",null,[e("链接成功 - 链接器自动装载库函数,不影响程序执行 "),a("strong",null,"只警告,不报错")],-1),f=a("h5",{id:"覆盖标准库函数原型"},[a("a",{class:"header-anchor",href:"#覆盖标准库函数原型","aria-hidden":"true"},"#"),e(" 覆盖标准库函数原型")],-1),v=a("ul",null,[a("li",null,"定义过多参数原型,调用时传入过多参数,函数正确执行(无视多余参数)"),a("li",null,"定义缺少参数原型,调用时传入不完整参数,函数错误执行,误把 0xc(%ebp),0x10(%ebp),…等更多内存单元当作函数参数")],-1),g=a("h5",{id:"缺少宏定义"},[a("a",{class:"header-anchor",href:"#缺少宏定义","aria-hidden":"true"},"#"),e(" 缺少宏定义")],-1),w=a("p",null,"链接失败 - 宏定义会被识别为函数,但链接器查找不到相应库函数",-1),x=a("h4",{id:"防止重复包括头文件"},[a("a",{class:"header-anchor",href:"#防止重复包括头文件","aria-hidden":"true"},"#"),e(" 防止重复包括头文件")],-1),E=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"ifndef"),e(),a("span",{class:"token expression"},"_FILENAME_H_")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"_FILENAME_H_")]),e("\n\n"),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n"),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n"),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"endif")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),_=a("h4",{id:"头文件不申请内存单元"},[a("a",{class:"header-anchor",href:"#头文件不申请内存单元","aria-hidden":"true"},"#"),e(" 头文件不申请内存单元")],-1),N=a("p",null,"除了全局共享静态变量外, 头文件中的定义不允许申请实际的内存单元",-1),S=a("h3",{id:"检查"},[a("a",{class:"header-anchor",href:"#检查","aria-hidden":"true"},"#"),e(" 检查")],-1),T=a("h4",{id:"边界检查"},[a("a",{class:"header-anchor",href:"#边界检查","aria-hidden":"true"},"#"),e(" 边界检查")],-1),O=a("ul",null,[a("li",null,"空/满栈检查"),a("li",null,"参数合法性检查 e.g elemSize > 0 检查")],-1),L=a("h4",{id:"指针检查"},[a("a",{class:"header-anchor",href:"#指针检查","aria-hidden":"true"},"#"),e(" 指针检查")],-1),A=a("ul",null,[a("li",null,[e("Alloctor 失败,需添加 NULL 检查: "),a("ul",null,[a("li",null,"assert"),a("li",null,"exit")])])],-1),C=a("h2",{id:"类型转换"},[a("a",{class:"header-anchor",href:"#类型转换","aria-hidden":"true"},"#"),e(" 类型转换")],-1),D=a("h3",{id:"机器码转换"},[a("a",{class:"header-anchor",href:"#机器码转换","aria-hidden":"true"},"#"),e(" 机器码转换")],-1),R=a("ul",null,[a("li",null,"有符号类型转换: 进行符号扩展"),a("li",null,"无符号类型转换: 进行零扩展")],-1),I=a("h2",{id:"pointer-tips-and-best-practice"},[a("a",{class:"header-anchor",href:"#pointer-tips-and-best-practice","aria-hidden":"true"},"#"),e(" Pointer Tips and Best Practice")],-1),P=a("h3",{id:"error-prone-pointers"},[a("a",{class:"header-anchor",href:"#error-prone-pointers","aria-hidden":"true"},"#"),e(" Error Prone Pointers")],-1),M=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"37"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"float"),e(" f "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"float"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("i"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"float"),e(" f "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"7.0"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"short"),e(" s "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"short"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("f"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),B=a("ul",null,[a("li",null,"悬挂指针"),a("li",null,"未初始化"),a("li",null,[e("改写未知区域 "),a("ul",null,[a("li",null,"下标越界"),a("li",null,[e("内存上溢 e.g "),a("code",null,"gets(string);")])])]),a("li",null,"指针相关运算符优先级与结合性"),a("li",null,"返回局部变量的地址"),a("li",null,"重复释放内存空间"),a("li",null,"内存泄漏 e.g 未释放空间/未释放部分深度空间(多维数组)"),a("li",null,"不能引用 void 指针指向的内存单元")],-1),U=a("h3",{id:"debugging-malloc"},[a("a",{class:"header-anchor",href:"#debugging-malloc","aria-hidden":"true"},"#"),e(" Debugging Malloc")],-1),F=a("h4",{id:"处理-void-指针"},[a("a",{class:"header-anchor",href:"#处理-void-指针","aria-hidden":"true"},"#"),e(" 处理 void 指针")],-1),G=a("p",null,"Tips: 中途运用强制类型转换,使得 void 指针可以执行指针加减运算",-1),H=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("target "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("void_pointer "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),z=a("h3",{id:"利用-void-指针实现-generic"},[a("a",{class:"header-anchor",href:"#利用-void-指针实现-generic","aria-hidden":"true"},"#"),e(" 利用 void 指针实现 Generic")],-1),X=a("h4",{id:"通用型-swap-函数"},[a("a",{class:"header-anchor",href:"#通用型-swap-函数","aria-hidden":"true"},"#"),e(" 通用型 Swap 函数")],-1),j=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"swap"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("vp1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("vp2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" size"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"char"),e(" buffer"),a("span",{class:"token punctuation"},"["),e("size"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("buffer"),a("span",{class:"token punctuation"},","),e(" vp1"),a("span",{class:"token punctuation"},","),e(" size"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("vp1"),a("span",{class:"token punctuation"},","),e(" vp2"),a("span",{class:"token punctuation"},","),e(" size"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("vp2"),a("span",{class:"token punctuation"},","),e(" buffer"),a("span",{class:"token punctuation"},","),e(" size"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),q=a("h4",{id:"通用型-search-function"},[a("a",{class:"header-anchor",href:"#通用型-search-function","aria-hidden":"true"},"#"),e(" 通用型 Search Function")],-1),K=a("h5",{id:"实现"},[a("a",{class:"header-anchor",href:"#实现","aria-hidden":"true"},"#"),e(" 实现")],-1),V=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token function"},"lsearch"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("key"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("base"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" n"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" elemSize"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),e("cmp_fn"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("i "),a("span",{class:"token operator"},"<"),e(" n"),a("span",{class:"token punctuation"},";"),e("i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e(" elemAddr "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("base "),a("span",{class:"token operator"},"+"),e(" i "),a("span",{class:"token operator"},"*"),e(" elemSize"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"cmp_fn"),a("span",{class:"token punctuation"},"("),e("key"),a("span",{class:"token punctuation"},","),e(" elemAddr"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(" elemAddr"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Y=a("h5",{id:"int-实例"},[a("a",{class:"header-anchor",href:"#int-实例","aria-hidden":"true"},"#"),e(" int 实例")],-1),W=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token function"},"IntCmp"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elem1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elem2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),e("ip1 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("elem1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),e("ip2 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("elem2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token operator"},"*"),e("ip1 "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token operator"},"*"),e("ip2"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Q=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(" array"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),e("\n size "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),e("\n target "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"// 应进行强制类型转换"),e("\n"),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),e(" found "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token function"},"lsearch"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"&"),e("target"),a("span",{class:"token punctuation"},","),e(" array"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(" IntCmp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("found "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Not Found"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Found"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),J=a("h5",{id:"string-实例"},[a("a",{class:"header-anchor",href:"#string-实例","aria-hidden":"true"},"#"),e(" string 实例")],-1),$=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token function"},"StrCmp"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("vp1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("vp2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 必须进行强制类型转换"),e("\n "),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("s1 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("vp1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("s2 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("vp2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token function"},"strcmp"),a("span",{class:"token punctuation"},"("),e("s1"),a("span",{class:"token punctuation"},","),e(" s2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Z=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("notes"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token string"},'"Ab"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"F#"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"B"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"Gb"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"D"'),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token operator"},"*"),e("target "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Eb"'),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),e(" found "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"lsearch"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"&"),e("target"),a("span",{class:"token punctuation"},","),e(" notes"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(" StrCmp"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),nn=a("h4",{id:"泛型数据结构"},[a("a",{class:"header-anchor",href:"#泛型数据结构","aria-hidden":"true"},"#"),e(" 泛型数据结构")],-1),sn=a("h5",{id:"通用型栈"},[a("a",{class:"header-anchor",href:"#通用型栈","aria-hidden":"true"},"#"),e(" 通用型栈")],-1),an=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"typedef"),e(),a("span",{class:"token keyword"},"struct"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elements"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" elemSize"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" logLen"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" allocLen"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e(" stack"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackNew"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackDispose"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackPush"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elemAddr"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackPop"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elemAddr"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),en=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackNew"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" elemSize"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 参数合法性检查"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("elemSize "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"perror"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"ElemSize <= 0"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n s"),a("span",{class:"token operator"},"->"),e("elemSize "),a("span",{class:"token operator"},"="),e(" elemSize"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("allocLen "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token function"},"malloc"),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("allocLen "),a("span",{class:"token operator"},"*"),e(" elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// NULL检查"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"perror"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"No Mem"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"exit"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackPush"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elemAddr"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 满栈检查"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"=="),e(" s"),a("span",{class:"token operator"},"->"),e("allocLen"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n s"),a("span",{class:"token operator"},"->"),e("allocLen "),a("span",{class:"token operator"},"*="),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token function"},"malloc"),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("elements"),a("span",{class:"token punctuation"},","),e(" s"),a("span",{class:"token operator"},"->"),e("allocLen "),a("span",{class:"token operator"},"*"),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("target "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"+"),e(" s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"*"),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("target"),a("span",{class:"token punctuation"},","),e(" elemAddr"),a("span",{class:"token punctuation"},","),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n s"),a("span",{class:"token operator"},"->"),e("logLen"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"StackPop"),a("span",{class:"token punctuation"},"("),e("stack "),a("span",{class:"token operator"},"*"),e("s"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("elemAddr"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 空栈检查"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"perror"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Empty Stack"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n s"),a("span",{class:"token operator"},"->"),e("logLen"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("source "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("s"),a("span",{class:"token operator"},"->"),e("elements "),a("span",{class:"token operator"},"+"),e(" s"),a("span",{class:"token operator"},"->"),e("logLen "),a("span",{class:"token operator"},"*"),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"memcpy"),a("span",{class:"token punctuation"},"("),e("elemAddr"),a("span",{class:"token punctuation"},","),e(" source"),a("span",{class:"token punctuation"},","),e(" s"),a("span",{class:"token operator"},"->"),e("elemSize"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br")])],-1),tn=a("h4",{id:"tools"},[a("a",{class:"header-anchor",href:"#tools","aria-hidden":"true"},"#"),e(" Tools")],-1),ln=a("p",null,[e("Valgrind - "),a("a",{href:"https://github.com/svn2github/valgrind",target:"_blank",rel:"noopener noreferrer"},"GitHub Repo")],-1),on=a("h2",{id:"useful-functions"},[a("a",{class:"header-anchor",href:"#useful-functions","aria-hidden":"true"},"#"),e(" Useful Functions")],-1),cn=a("h3",{id:"memset"},[a("a",{class:"header-anchor",href:"#memset","aria-hidden":"true"},"#"),e(" memset")],-1),rn=a("h3",{id:"free"},[a("a",{class:"header-anchor",href:"#free","aria-hidden":"true"},"#"),e(" free")],-1),pn=a("p",null,[a("strong",null,"free 函数会回退 4/8 字节,取出 heap 块的长度/信息,根据此信息进行 heap 块的释放.")],-1),un=a("h3",{id:"strings"},[a("a",{class:"header-anchor",href:"#strings","aria-hidden":"true"},"#"),e(" Strings")],-1),kn=a("h4",{id:"strdup"},[a("a",{class:"header-anchor",href:"#strdup","aria-hidden":"true"},"#"),e(" strdup")],-1),dn=a("p",null,[e("string duplicate - "),a("code",null,"char *strdup(string)"),e(" 封装 allocator 细节")],-1),mn=a("h4",{id:"strchr-and-strstr"},[a("a",{class:"header-anchor",href:"#strchr-and-strstr","aria-hidden":"true"},"#"),e(" strchr and strstr")],-1),bn=a("p",null,"返回字符/串在字符串中出现的位置(地址)",-1),hn=a("h4",{id:"strtok"},[a("a",{class:"header-anchor",href:"#strtok","aria-hidden":"true"},"#"),e(" strtok")],-1),yn=a("h4",{id:"strcasecmp"},[a("a",{class:"header-anchor",href:"#strcasecmp","aria-hidden":"true"},"#"),e(" strcasecmp")],-1),fn=a("h4",{id:"getopt"},[a("a",{class:"header-anchor",href:"#getopt","aria-hidden":"true"},"#"),e(" getopt")],-1),vn=a("p",null,"解析命令行参数, 轻松地提取以 - 或 / 开头的参数",-1),gn=a("h3",{id:"i-o"},[a("a",{class:"header-anchor",href:"#i-o","aria-hidden":"true"},"#"),e(" I/O")],-1),wn=a("h4",{id:"string-scanf"},[a("a",{class:"header-anchor",href:"#string-scanf","aria-hidden":"true"},"#"),e(" String Scanf")],-1),xn=a("p",null,"可以用作简易匹配读取函数",-1),En=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 提取除 http:// 外的字符串"),e("\n"),a("span",{class:"token function"},"sscanf"),a("span",{class:"token punctuation"},"("),e("buf"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"http://%s"'),a("span",{class:"token punctuation"},","),e(" url_part"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),_n=a("h3",{id:"exceptions"},[a("a",{class:"header-anchor",href:"#exceptions","aria-hidden":"true"},"#"),e(" Exceptions")],-1),Nn=a("p",null,"perror(string) - 用来将上一个函数发生错误的原因输出到标准设备(stderr)",-1),Sn=a("h3",{id:"process"},[a("a",{class:"header-anchor",href:"#process","aria-hidden":"true"},"#"),e(" Process")],-1),Tn=a("h4",{id:"fork-and-execve"},[a("a",{class:"header-anchor",href:"#fork-and-execve","aria-hidden":"true"},"#"),e(" Fork and Execve")],-1),On=a("ul",null,[a("li",null,"fork(): 创建当前进程的拷贝"),a("li",null,[e("execve(): 用另一程序的代码代替当前进程的代码 "),a("ul",null,[a("li",null,[a("code",null,"int execve(char *filename, char *argv[], char *env_p[])")])])])],-1),Ln=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"fork_exec"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("path"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e("argv"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token class-name"},"pid_t"),e(" pid "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"fork"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),e(),a("span",{class:"token operator"},"!="),e(" pid"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Parent: created a child %d\\n"'),a("span",{class:"token punctuation"},","),e(" pid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Child: exec-ing new program now\\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"execv"),a("span",{class:"token punctuation"},"("),e("path"),a("span",{class:"token punctuation"},","),e(" argv"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"This line printed by parent only!\\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),An=a("h4",{id:"other"},[a("a",{class:"header-anchor",href:"#other","aria-hidden":"true"},"#"),e(" Other")],-1),Cn=a("ul",null,[a("li",null,"getpid()"),a("li",null,"wait(int *child_status)/waitpid(pid)"),a("li",null,"exit()")],-1),Dn=a("h3",{id:"threads"},[a("a",{class:"header-anchor",href:"#threads","aria-hidden":"true"},"#"),e(" Threads")],-1),Rn=a("h4",{id:"pthread"},[a("a",{class:"header-anchor",href:"#pthread","aria-hidden":"true"},"#"),e(" PThread")],-1),In=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"typedef"),e(),a("span",{class:"token keyword"},"unsigned"),e(),a("span",{class:"token keyword"},"long"),e(),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token class-name"},"pthread_t"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"/**\n * create thread\n * @param {指向线程标识符的指针} pthread_t *__thread\n * @param {设置线程属性} __const pthread_attr_t *__attr\n * @param {线程运行函数的起始地址} void *(*__start_routine) (void *)\n * @param {运行函数的参数} void *__arg\n */"),e("\n"),a("span",{class:"token keyword"},"extern"),e(),a("span",{class:"token keyword"},"int"),e(" pthread_create "),a("span",{class:"token function"},"__P"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token class-name"},"pthread_t"),e(),a("span",{class:"token operator"},"*"),e("__thread"),a("span",{class:"token punctuation"},","),e("\n __const "),a("span",{class:"token class-name"},"pthread_attr_t"),e(),a("span",{class:"token operator"},"*"),e("__attr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),e("__start_routine"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(", "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("__arg"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n  \n"),a("span",{class:"token comment"},"/**\n * 等待线程\n * @param {被等待的线程标识符} pthread_t __th\n * @param {一个用户定义的指针,它可以用来存储被等待线程的返回值} void **__thread_return\n */"),e("\n"),a("span",{class:"token keyword"},"extern"),e(),a("span",{class:"token keyword"},"int"),e(" pthread_join "),a("span",{class:"token function"},"__P"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"pthread_t"),e(" __th"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),e("__thread_return"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"/**\n * 退出线程\n * @param {函数的返回代码} (void *__retval)) __attribute__ ((__noreturn__)\n */"),e("\n"),a("span",{class:"token keyword"},"extern"),e(),a("span",{class:"token keyword"},"void"),e(" pthread_exit "),a("span",{class:"token function"},"__P"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),e("__retval"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"__attribute__"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),e("__noreturn__"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"// 一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码 ESRCH"),e("\n\n"),a("span",{class:"token comment"},"// 以下为互斥锁相关函数"),e("\n\npthread_mutex_init\npthread_mutexattr_init\n\n"),a("span",{class:"token comment"},"/**\n * 设置属性 pshared\n * PTHREAD_PROCESS_PRIVATE\n * PTHREAD_PROCESS_SHARED\n */"),e("\npthread_mutexattr_setpshared\n\n"),a("span",{class:"token comment"},"/**\n * 设置互斥锁类型\n * PTHREAD_MUTEX_NORMAL\n * PTHREAD_MUTEX_ERRORCHECK\n * PTHREAD_MUTEX_RECURSIVE\n * PTHREAD_MUTEX_DEFAULT\n */"),e("\npthread_mutexattr_settype\n\npthread_mutex_lock\npthread_mutex_unlock\npthread_delay_np\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br")])],-1),Pn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[e("InitThreadPackage"),a("span",{class:"token punctuation"},";"),e("\nThreadNew"),a("span",{class:"token punctuation"},";"),e("\nThreadSleep"),a("span",{class:"token punctuation"},";"),e("\nRunAllThreads"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"SemaphoreNew"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"SemaphoreWait"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"SemaphoreSignal"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Mn=a("h4",{id:"semaphore"},[a("a",{class:"header-anchor",href:"#semaphore","aria-hidden":"true"},"#"),e(" Semaphore")],-1),Bn=a("ul",null,[a("li",null,"哲学家就餐问题"),a("li",null,"将 Semaphore 变量的值在允许范围内(不至于使得线程锁失效)使得其取最大值,减少线程阻塞"),a("li",null,"EmptyBuf 8, FullBuf 0"),a("li",null,[e("双向通信,互相唤醒 - "),a("code",null,"Writer:sw(empty),ss(full);"),e(),a("code",null,"Reader:sw(full),ss(empty);")])],-1),Un=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"SellTickets"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" agent"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token operator"},"*"),e("ticketsNum"),a("span",{class:"token punctuation"},","),e(" Semaphore lock"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"while"),e(),a("span",{class:"token punctuation"},"("),e("true"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"// 当 lock == 0 时,当前进程阻塞, 等待 lock > 0"),e("\n "),a("span",{class:"token comment"},"// 当 lock > 0 时, 当前进程继续进行, 并且 lock--"),e("\n "),a("span",{class:"token function"},"SemaphoreWait"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),e("ticketsNum "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"break"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 票已售磬"),e("\n\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),e("ticketsNum"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 售出一张票"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Sell One Ticket.\\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// lock++ 使得 lock > 0"),e("\n "),a("span",{class:"token comment"},"// 若有其他进程调用了SemaphoreWait, 且因之前 lock == 0 而被阻塞, 则此时其他进程可继续进行"),e("\n "),a("span",{class:"token function"},"SemaphoreSignal"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n\n "),a("span",{class:"token comment"},"// break to here"),e("\n "),a("span",{class:"token comment"},"// 作用同循环内的 Signal 函数"),e("\n "),a("span",{class:"token function"},"SemaphoreSignal"),a("span",{class:"token punctuation"},"("),e("lock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),Fn=a("h2",{id:"联合体"},[a("a",{class:"header-anchor",href:"#联合体","aria-hidden":"true"},"#"),e(" 联合体")],-1),Gn=a("ul",null,[a("li",null,"机器码 e.g 理解 IEEE 754 标准"),a("li",null,"区分大/小端模式")],-1),Hn=a("h2",{id:"naming-conventions"},[a("a",{class:"header-anchor",href:"#naming-conventions","aria-hidden":"true"},"#"),e(" Naming Conventions")],-1),zn=a("h3",{id:"常用缩写词"},[a("a",{class:"header-anchor",href:"#常用缩写词","aria-hidden":"true"},"#"),e(" 常用缩写词")],-1),Xn=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"left"}},"原词"),a("th",{style:{"text-align":"left"}},"缩写")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"left"}},"addition"),a("td",{style:{"text-align":"left"}},"add")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"answer"),a("td",{style:{"text-align":"left"}},"ans")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"array"),a("td",{style:{"text-align":"left"}},"arr")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"average"),a("td",{style:{"text-align":"left"}},"avg")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"buffer"),a("td",{style:{"text-align":"left"}},"buf 或 buff")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"capture"),a("td",{style:{"text-align":"left"}},"cap 或 capt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"check"),a("td",{style:{"text-align":"left"}},"chk")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"count"),a("td",{style:{"text-align":"left"}},"cnt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"column"),a("td",{style:{"text-align":"left"}},"col")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"control"),a("td",{style:{"text-align":"left"}},"ctrl")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"decode"),a("td",{style:{"text-align":"left"}},"dec")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"define"),a("td",{style:{"text-align":"left"}},"def")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"delete"),a("td",{style:{"text-align":"left"}},"del")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"destination"),a("td",{style:{"text-align":"left"}},"dst 或 dest")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"display"),a("td",{style:{"text-align":"left"}},"disp")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"division"),a("td",{style:{"text-align":"left"}},"div")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"encode"),a("td",{style:{"text-align":"left"}},"enc")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"environment"),a("td",{style:{"text-align":"left"}},"env")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"error"),a("td",{style:{"text-align":"left"}},"err")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"float"),a("td",{style:{"text-align":"left"}},"flt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"frequency"),a("td",{style:{"text-align":"left"}},"freq")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"header"),a("td",{style:{"text-align":"left"}},"hdr")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"index"),a("td",{style:{"text-align":"left"}},"idx")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"image"),a("td",{style:{"text-align":"left"}},"img")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"increment"),a("td",{style:{"text-align":"left"}},"inc")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"initialize"),a("td",{style:{"text-align":"left"}},"init")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"iteration"),a("td",{style:{"text-align":"left"}},"itr")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"length"),a("td",{style:{"text-align":"left"}},"len")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"memory"),a("td",{style:{"text-align":"left"}},"mem")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"middle"),a("td",{style:{"text-align":"left"}},"mid")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"make"),a("td",{style:{"text-align":"left"}},"mk")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"message"),a("td",{style:{"text-align":"left"}},"msg")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"multiplication"),a("td",{style:{"text-align":"left"}},"mul")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"number"),a("td",{style:{"text-align":"left"}},"num")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"operand"),a("td",{style:{"text-align":"left"}},"opnd")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"optimization"),a("td",{style:{"text-align":"left"}},"opt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"operator"),a("td",{style:{"text-align":"left"}},"optr")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"packet"),a("td",{style:{"text-align":"left"}},"pkt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"position"),a("td",{style:{"text-align":"left"}},"pos")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"previous"),a("td",{style:{"text-align":"left"}},"pre/prev")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"payload"),a("td",{style:{"text-align":"left"}},"type")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"pointer"),a("td",{style:{"text-align":"left"}},"ptr/pt")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"return"),a("td",{style:{"text-align":"left"}},"code")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"record"),a("td",{style:{"text-align":"left"}},"rcd/rc")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"receive"),a("td",{style:{"text-align":"left"}},"recv")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"result"),a("td",{style:{"text-align":"left"}},"res")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"return"),a("td",{style:{"text-align":"left"}},"ret")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"source"),a("td",{style:{"text-align":"left"}},"src")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"stack"),a("td",{style:{"text-align":"left"}},"stk")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"string"),a("td",{style:{"text-align":"left"}},"str")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"subtraction"),a("td",{style:{"text-align":"left"}},"sub")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"table"),a("td",{style:{"text-align":"left"}},"tab")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"temporary"),a("td",{style:{"text-align":"left"}},"tmp 或 temp")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"total"),a("td",{style:{"text-align":"left"}},"tot")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"time"),a("td",{style:{"text-align":"left"}},"stamp")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"value"),a("td",{style:{"text-align":"left"}},"val")])])],-1),jn=a("h3",{id:"header-file"},[a("a",{class:"header-anchor",href:"#header-file","aria-hidden":"true"},"#"),e(" Header File")],-1),qn=a("p",null,"防止其他文件重复#include 本文件",-1),Kn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"ifndef"),e(),a("span",{class:"token expression"},"MONGOOSE_HEADER_INCLUDED")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(" "),a("span",{class:"token macro-name"},"MONGOOSE_HEADER_INCLUDED")]),e("\n\n"),a("span",{class:"token comment"},"/*.................................\n * do something here\n *.................................\n */"),e("\n\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"endif"),e(),a("span",{class:"token comment"},"/* MONGOOSE_HEADER_INCLUDED */")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),Vn=a("h2",{id:"c-standard-library"},[a("a",{class:"header-anchor",href:"#c-standard-library","aria-hidden":"true"},"#"),e(" C Standard Library")],-1),Yn=a("h3",{id:"assert"},[a("a",{class:"header-anchor",href:"#assert","aria-hidden":"true"},"#"),e(" Assert")],-1),Wn=a("ul",null,[a("li",null,"关闭断言")],-1),Qn=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"NDEBUG")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),e(),a("span",{class:"token string"},"")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Jn=a("ul",null,[a("li",null,"开启断言")],-1),$n=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"undef"),e(),a("span",{class:"token expression"},"NDEBUG")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"include"),e(),a("span",{class:"token string"},"")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Zn=a("h3",{id:"types"},[a("a",{class:"header-anchor",href:"#types","aria-hidden":"true"},"#"),e(" Types")],-1),ns=a("ul",null,[a("li",null,"可检测字符")],-1),ss=a("p",null,"getc、fgetc、getchar 函数可返回值(EOF 值/unsigned char 类型)",-1),as=a("ul",null,[a("li",null,"不可检测字符")],-1),es=a("p",null,[e("非 EOF 值/非 unsigned char 类型("),a("em",null,"会引发严重错误"),e(")")],-1),ts=a("p",null,[a("img",{src:"/awesome-notes/assets/ctype.h.b7c42e1e.jpg",alt:"CType Functions"})],-1),ls=a("h3",{id:"errno"},[a("a",{class:"header-anchor",href:"#errno","aria-hidden":"true"},"#"),e(" Errno")],-1),os=a("ul",null,[a("li",null,"errno 的值在程序启动时为零,但是不会被任何库函数设为零")],-1),cs=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[e("errno "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\ny "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"sqrt"),a("span",{class:"token punctuation"},"("),e("x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("errno "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"invalid x : %e\\n"'),a("span",{class:"token punctuation"},","),e(" x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),rs=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"ifndef"),e(),a("span",{class:"token expression"},"_I386_ERRNO_H")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"_I386_ERRNO_H")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPERM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"1"),e()]),a("span",{class:"token comment"},"/* Operation not permitted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOENT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"2"),e()]),a("span",{class:"token comment"},"/* No such file or directory */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESRCH"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"3"),e()]),a("span",{class:"token comment"},"/* No such process */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EINTR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"4"),e()]),a("span",{class:"token comment"},"/* Interrupted system call */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EIO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"5"),e()]),a("span",{class:"token comment"},"/* I/O error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENXIO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"6"),e()]),a("span",{class:"token comment"},"/* No such device or address */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"E2BIG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"7"),e()]),a("span",{class:"token comment"},"/* Arg list too long */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOEXEC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"8"),e()]),a("span",{class:"token comment"},"/* Exec format error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADF"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"9"),e()]),a("span",{class:"token comment"},"/* Bad file number */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECHILD"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"10"),e()]),a("span",{class:"token comment"},"/* No child processes */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EAGAIN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"11"),e()]),a("span",{class:"token comment"},"/* Try again */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOMEM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"12"),e()]),a("span",{class:"token comment"},"/* Out of memory */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EACCES"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"13"),e()]),a("span",{class:"token comment"},"/* Permission denied */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EFAULT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"14"),e()]),a("span",{class:"token comment"},"/* Bad address */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTBLK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"15"),e()]),a("span",{class:"token comment"},"/* Block device required */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBUSY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"16"),e()]),a("span",{class:"token comment"},"/* Device or resource busy */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EEXIST"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"17"),e()]),a("span",{class:"token comment"},"/* File exists */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EXDEV"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"18"),e()]),a("span",{class:"token comment"},"/* Cross-device link */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENODEV"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"19"),e()]),a("span",{class:"token comment"},"/* No such device */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTDIR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"20"),e()]),a("span",{class:"token comment"},"/* Not a directory */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EISDIR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"21"),e()]),a("span",{class:"token comment"},"/* Is a directory */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EINVAL"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"22"),e()]),a("span",{class:"token comment"},"/* Invalid argument */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENFILE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"23"),e()]),a("span",{class:"token comment"},"/* File table overflow */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMFILE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"24"),e()]),a("span",{class:"token comment"},"/* Too many open files */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTTY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"25"),e()]),a("span",{class:"token comment"},"/* Not a typewriter */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ETXTBSY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"26"),e()]),a("span",{class:"token comment"},"/* Text file busy */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EFBIG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"27"),e()]),a("span",{class:"token comment"},"/* File too large */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOSPC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"28"),e()]),a("span",{class:"token comment"},"/* No space left on device */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESPIPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"29"),e()]),a("span",{class:"token comment"},"/* Illegal seek */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EROFS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"30"),e()]),a("span",{class:"token comment"},"/* Read-only file system */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMLINK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"31"),e()]),a("span",{class:"token comment"},"/* Too many links */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPIPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"32"),e()]),a("span",{class:"token comment"},"/* Broken pipe */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDOM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"33"),e()]),a("span",{class:"token comment"},"/* Math argument out of domain of func */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ERANGE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"34"),e()]),a("span",{class:"token comment"},"/* Math result not representable */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDEADLK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"35"),e()]),a("span",{class:"token comment"},"/* Resource deadlock would occur */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENAMETOOLONG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"36"),e()]),a("span",{class:"token comment"},"/* File name too long */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOLCK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"37"),e()]),a("span",{class:"token comment"},"/* No record locks available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOSYS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"38"),e()]),a("span",{class:"token comment"},"/* Function not implemented */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTEMPTY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"39"),e()]),a("span",{class:"token comment"},"/* Directory not empty */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELOOP"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"40"),e()]),a("span",{class:"token comment"},"/* Too many symbolic links encountered */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EWOULDBLOCK"),e(),a("span",{class:"token expression"},"EAGAIN "),a("span",{class:"token comment"},"/* Operation would block */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOMSG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"42"),e()]),a("span",{class:"token comment"},"/* No message of desired type */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EIDRM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"43"),e()]),a("span",{class:"token comment"},"/* Identifier removed */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECHRNG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"44"),e()]),a("span",{class:"token comment"},"/* Channel number out of range */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EL2NSYNC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"45"),e()]),a("span",{class:"token comment"},"/* Level 2 not synchronized */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EL3HLT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"46"),e()]),a("span",{class:"token comment"},"/* Level 3 halted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EL3RST"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"47"),e()]),a("span",{class:"token comment"},"/* Level 3 reset */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELNRNG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"48"),e()]),a("span",{class:"token comment"},"/* Link number out of range */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EUNATCH"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"49"),e()]),a("span",{class:"token comment"},"/* Protocol driver not attached */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOCSI"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"50"),e()]),a("span",{class:"token comment"},"/* No CSI structure available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EL2HLT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"51"),e()]),a("span",{class:"token comment"},"/* Level 2 halted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"52"),e()]),a("span",{class:"token comment"},"/* Invalid exchange */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"53"),e()]),a("span",{class:"token comment"},"/* Invalid request descriptor */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EXFULL"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"54"),e()]),a("span",{class:"token comment"},"/* Exchange full */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOANO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"55"),e()]),a("span",{class:"token comment"},"/* No anode */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADRQC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"56"),e()]),a("span",{class:"token comment"},"/* Invalid request code */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADSLT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"57"),e()]),a("span",{class:"token comment"},"/* Invalid slot */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDEADLOCK"),e(),a("span",{class:"token expression"},"EDEADLK")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBFONT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"59"),e()]),a("span",{class:"token comment"},"/* Bad font file format */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOSTR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"60"),e()]),a("span",{class:"token comment"},"/* Device not a stream */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENODATA"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"61"),e()]),a("span",{class:"token comment"},"/* No data available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ETIME"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"62"),e()]),a("span",{class:"token comment"},"/* Timer expired */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOSR"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"63"),e()]),a("span",{class:"token comment"},"/* Out of streams resources */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENONET"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"64"),e()]),a("span",{class:"token comment"},"/* Machine is not on the network */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOPKG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"65"),e()]),a("span",{class:"token comment"},"/* Package not installed */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EREMOTE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"66"),e()]),a("span",{class:"token comment"},"/* Object is remote */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOLINK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"67"),e()]),a("span",{class:"token comment"},"/* Link has been severed */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EADV"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"68"),e()]),a("span",{class:"token comment"},"/* Advertise error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESRMNT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"69"),e()]),a("span",{class:"token comment"},"/* Srmount error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECOMM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"70"),e()]),a("span",{class:"token comment"},"/* Communication error on send */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPROTO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"71"),e()]),a("span",{class:"token comment"},"/* Protocol error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMULTIHOP"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"72"),e()]),a("span",{class:"token comment"},"/* Multi hop attempted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDOTDOT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"73"),e()]),a("span",{class:"token comment"},"/* RFS specific error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADMSG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"74"),e()]),a("span",{class:"token comment"},"/* Not a data message */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EOVERFLOW"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"75"),e()]),a("span",{class:"token comment"},"/* Value too large for defined data type */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTUNIQ"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"76"),e()]),a("span",{class:"token comment"},"/* Name not unique on network */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EBADFD"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"77"),e()]),a("span",{class:"token comment"},"/* File descriptor in bad state */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EREMCHG"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"78"),e()]),a("span",{class:"token comment"},"/* Remote address changed */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBACC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"79"),e()]),a("span",{class:"token comment"},"/* Can not access a needed shared library */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBBAD"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"80"),e()]),a("span",{class:"token comment"},"/* Accessing a corrupted shared library */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBSCN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"81"),e()]),a("span",{class:"token comment"},"/* .lib section in a.out corrupted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBMAX"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"82"),e()]),a("span",{class:"token comment"},"/* Attempting to link in too many shared libraries */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ELIBEXEC"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"83"),e()]),a("span",{class:"token comment"},"/* Cannot exec a shared library directly */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EILSEQ"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"84"),e()]),a("span",{class:"token comment"},"/* Illegal byte sequence */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ERESTART"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"85"),e()]),a("span",{class:"token comment"},"/* Interrupted system call should be restarted */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESTRPIPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"86"),e()]),a("span",{class:"token comment"},"/* Streams pipe error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EUSERS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"87"),e()]),a("span",{class:"token comment"},"/* Too many users */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTSOCK"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"88"),e()]),a("span",{class:"token comment"},"/* Socket operation on non-socket */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDESTADDRREQ"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"89"),e()]),a("span",{class:"token comment"},"/* Destination address required */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMSGSIZE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"90"),e()]),a("span",{class:"token comment"},"/* Message too long */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPROTOTYPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"91"),e()]),a("span",{class:"token comment"},"/* Protocol wrong type for socket */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOPROTOOPT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"92"),e()]),a("span",{class:"token comment"},"/* Protocol not available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPROTONOSUPPORT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"93"),e()]),a("span",{class:"token comment"},"/* Protocol not supported */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESOCKTNOSUPPORT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"94"),e()]),a("span",{class:"token comment"},"/* Socket type not supported */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EOPNOTSUPP"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"95"),e()]),a("span",{class:"token comment"},"/* Operation not supported on transport endpoint */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EPFNOSUPPORT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"96"),e()]),a("span",{class:"token comment"},"/* Protocol family not supported */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EAFNOSUPPORT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"97"),e()]),a("span",{class:"token comment"},"/* Address family not supported by protocol */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EADDRINUSE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"98"),e()]),a("span",{class:"token comment"},"/* Address already in use */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EADDRNOTAVAIL"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"99"),e()]),a("span",{class:"token comment"},"/* Cannot assign requested address */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENETDOWN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"100"),e()]),a("span",{class:"token comment"},"/* Network is down */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENETUNREACH"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"101"),e()]),a("span",{class:"token comment"},"/* Network is unreachable */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENETRESET"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"102"),e()]),a("span",{class:"token comment"},"/* Network dropped connection because of reset */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECONNABORTED"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"103"),e()]),a("span",{class:"token comment"},"/* Software caused connection abort */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECONNRESET"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"104"),e()]),a("span",{class:"token comment"},"/* Connection reset by peer */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOBUFS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"105"),e()]),a("span",{class:"token comment"},"/* No buffer space available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EISCONN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"106"),e()]),a("span",{class:"token comment"},"/* Transport endpoint is already connected */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTCONN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"107"),e()]),a("span",{class:"token comment"},"/* Transport endpoint is not connected */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESHUTDOWN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"108"),e()]),a("span",{class:"token comment"},"/* Cannot send after transport endpoint shutdown */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ETOOMANYREFS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"109"),e()]),a("span",{class:"token comment"},"/* Too many references: cannot splice */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ETIMEDOUT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"110"),e()]),a("span",{class:"token comment"},"/* Connection timed out */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ECONNREFUSED"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"111"),e()]),a("span",{class:"token comment"},"/* Connection refused */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EHOSTDOWN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"112"),e()]),a("span",{class:"token comment"},"/* Host is down */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EHOSTUNREACH"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"113"),e()]),a("span",{class:"token comment"},"/* No route to host */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EALREADY"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"114"),e()]),a("span",{class:"token comment"},"/* Operation already in progress */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EINPROGRESS"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"115"),e()]),a("span",{class:"token comment"},"/* Operation now in progress */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ESTALE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"116"),e()]),a("span",{class:"token comment"},"/* Stale NFS file handle */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EUCLEAN"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"117"),e()]),a("span",{class:"token comment"},"/* Structure needs cleaning */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOTNAM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"118"),e()]),a("span",{class:"token comment"},"/* Not a XENIX named type file */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENAVAIL"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"119"),e()]),a("span",{class:"token comment"},"/* No XENIX semaphores available */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EISNAM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"120"),e()]),a("span",{class:"token comment"},"/* Is a named type file */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EREMOTEIO"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"121"),e()]),a("span",{class:"token comment"},"/* Remote I/O error */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EDQUOT"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"122"),e()]),a("span",{class:"token comment"},"/* Quota exceeded */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"ENOMEDIUM"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"123"),e()]),a("span",{class:"token comment"},"/* No medium found */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"EMEDIUMTYPE"),e(),a("span",{class:"token expression"},[a("span",{class:"token number"},"124"),e()]),a("span",{class:"token comment"},"/* Wrong medium type */")]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"endif")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br"),a("span",{class:"line-number"},"83"),a("br"),a("span",{class:"line-number"},"84"),a("br"),a("span",{class:"line-number"},"85"),a("br"),a("span",{class:"line-number"},"86"),a("br"),a("span",{class:"line-number"},"87"),a("br"),a("span",{class:"line-number"},"88"),a("br"),a("span",{class:"line-number"},"89"),a("br"),a("span",{class:"line-number"},"90"),a("br"),a("span",{class:"line-number"},"91"),a("br"),a("span",{class:"line-number"},"92"),a("br"),a("span",{class:"line-number"},"93"),a("br"),a("span",{class:"line-number"},"94"),a("br"),a("span",{class:"line-number"},"95"),a("br"),a("span",{class:"line-number"},"96"),a("br"),a("span",{class:"line-number"},"97"),a("br"),a("span",{class:"line-number"},"98"),a("br"),a("span",{class:"line-number"},"99"),a("br"),a("span",{class:"line-number"},"100"),a("br"),a("span",{class:"line-number"},"101"),a("br"),a("span",{class:"line-number"},"102"),a("br"),a("span",{class:"line-number"},"103"),a("br"),a("span",{class:"line-number"},"104"),a("br"),a("span",{class:"line-number"},"105"),a("br"),a("span",{class:"line-number"},"106"),a("br"),a("span",{class:"line-number"},"107"),a("br"),a("span",{class:"line-number"},"108"),a("br"),a("span",{class:"line-number"},"109"),a("br"),a("span",{class:"line-number"},"110"),a("br"),a("span",{class:"line-number"},"111"),a("br"),a("span",{class:"line-number"},"112"),a("br"),a("span",{class:"line-number"},"113"),a("br"),a("span",{class:"line-number"},"114"),a("br"),a("span",{class:"line-number"},"115"),a("br"),a("span",{class:"line-number"},"116"),a("br"),a("span",{class:"line-number"},"117"),a("br"),a("span",{class:"line-number"},"118"),a("br"),a("span",{class:"line-number"},"119"),a("br"),a("span",{class:"line-number"},"120"),a("br"),a("span",{class:"line-number"},"121"),a("br"),a("span",{class:"line-number"},"122"),a("br"),a("span",{class:"line-number"},"123"),a("br"),a("span",{class:"line-number"},"124"),a("br"),a("span",{class:"line-number"},"125"),a("br"),a("span",{class:"line-number"},"126"),a("br"),a("span",{class:"line-number"},"127"),a("br")])],-1),ps=a("h3",{id:"float-and-limits"},[a("a",{class:"header-anchor",href:"#float-and-limits","aria-hidden":"true"},"#"),e(" Float and Limits")],-1),is=a("ul",null,[a("li",null,[e("宏定义:"),a("code",null,"CHAR/UCHAR/SCHAR/SHRT/USHRT/INT/UINT/LONG/ULONG/FLT/DBL/LDBL"),e("有关的"),a("code",null,"MIN/MAX/EPSILON")])],-1),us=a("h3",{id:"locale"},[a("a",{class:"header-anchor",href:"#locale","aria-hidden":"true"},"#"),e(" Locale")],-1),ks=a("ul",null,[a("li",null,"实现时间/单位/货币等一系列的国际化"),a("li",null,"常用函数")],-1),ds=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[e("_CRTIMP "),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),e(" __cdecl "),a("span",{class:"token function"},"setlocale"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"const"),e(),a("span",{class:"token keyword"},"char"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n_CRTIMP "),a("span",{class:"token keyword"},"struct"),e(),a("span",{class:"token class-name"},"lconv"),e(),a("span",{class:"token operator"},"*"),e(" __cdecl "),a("span",{class:"token function"},"localeconv"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ms=a("ul",null,[a("li",null,"int 值")],-1),bs=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_ALL"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"0")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_COLLATE"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"1")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_CTYPE"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"2")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_MONETARY"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"3")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_NUMERIC"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"4")])]),e("\n"),a("span",{class:"token macro property"},[a("span",{class:"token directive-hash"},"#"),a("span",{class:"token directive keyword"},"define"),e(),a("span",{class:"token macro-name"},"LC_TIME"),e(" "),a("span",{class:"token expression"},[a("span",{class:"token number"},"5")])]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),hs=a("h3",{id:"math"},[a("a",{class:"header-anchor",href:"#math","aria-hidden":"true"},"#"),e(" Math")],-1),ys=a("p",null,"数学函数库(包括后缀 f(float)/l(long double))",-1),fs=a("h3",{id:"setjmp"},[a("a",{class:"header-anchor",href:"#setjmp","aria-hidden":"true"},"#"),e(" SetJMP")],-1),vs=a("ul",null,[a("li",null,"常用函数")],-1),gs=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"int"),e(),a("span",{class:"token function"},"setjmp"),a("span",{class:"token punctuation"},"("),e("jmp_buf env"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"longjmp"),a("span",{class:"token punctuation"},"("),e("jmp_buf env"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),e(" val"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ws=a("ul",null,[a("li",null,[e("使用:用于 if/else、loop、switch 语句 "),a("ol",null,[a("li",null,"直接调用 setjmp 函数时,返回值为 0;"),a("li",null,"调用 longjmp 函数时,若 val 值不为 0,则跳转至上次 setjmp 返回值为 0 处,继续向后执行语句")])]),a("li",null,[e("功能 "),a("ol",null,[a("li",null,"实现非本地(局部)跳转(跨越多层函数调用栈进行跳转)"),a("li",null,"实现类 Java 异常机制(异常抛出及捕获)")])])],-1),xs=a("h3",{id:"signal"},[a("a",{class:"header-anchor",href:"#signal","aria-hidden":"true"},"#"),e(" Signal")],-1),Es=a("p",null,"信号处理程序中所有数据应为 volatile 类型",-1),_s=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[e("_CRTIMP "),a("span",{class:"token keyword"},"int"),e(" __cdecl "),a("span",{class:"token function"},"raise"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n_CRTIMP "),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token punctuation"},"("),e("__cdecl "),a("span",{class:"token operator"},"*"),e(" __cdecl "),a("span",{class:"token function"},"signal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token punctuation"},"("),e("__cdecl "),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ns=a("h3",{id:"stdarg"},[a("a",{class:"header-anchor",href:"#stdarg","aria-hidden":"true"},"#"),e(" Stdarg")],-1),Ss=a("p",null,"用于编写可变参数函数",-1),Ts=a("div",{class:"language-c line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"printargs"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" arg1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},"/* 输出所有int类型的参数,直到-1结束 */"),e("\n"),a("span",{class:"token punctuation"},"{"),e("\n va_list ap"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" i"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"va_start"),a("span",{class:"token punctuation"},"("),e("ap"),a("span",{class:"token punctuation"},","),e(" arg1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"="),e(" arg1"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"va_arg"),a("span",{class:"token punctuation"},"("),e("ap"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"int"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"printf"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"%d "'),a("span",{class:"token punctuation"},","),e(" i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"va_end"),a("span",{class:"token punctuation"},"("),e("ap"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"putchar"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'\\n'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Os=a("h3",{id:"stddef"},[a("a",{class:"header-anchor",href:"#stddef","aria-hidden":"true"},"#"),e(" Stddef")],-1),Ls=a("ul",null,[a("li",null,[e("宏 "),a("ul",null,[a("li",null,"NULL Null 指针常量"),a("li",null,[a("code",null,"offsetof(type, member-designator)"),e(" 获得字段在结构体中的偏移量")])])]),a("li",null,[e("类型 "),a("ul",null,[a("li",null,[a("code",null,"ptrdiff_t"),e(" 带符号的整数类型, 用来表示指针相减的结果类型")]),a("li",null,[a("code",null,"wchar_t"),e(" 宽字符类型")]),a("li",null,[a("code",null,"size_t"),e(" 无符号整数类型, 用来表示 sizeof 操作符的结果类型")])])])],-1),As=a("h3",{id:"string"},[a("a",{class:"header-anchor",href:"#string","aria-hidden":"true"},"#"),e(" String")],-1),Cs=a("ul",null,[a("li",null,"men 系函数:操作任意字符序列"),a("li",null,"strn 系函数:操作非空字符序列"),a("li",null,"str 系函数:操作字符串序列('\\0')")],-1),Ds=a("h2",{id:"gdb-tutorial"},[a("a",{class:"header-anchor",href:"#gdb-tutorial","aria-hidden":"true"},"#"),e(" GDB Tutorial")],-1),Rs=a("h3",{id:"basic-command"},[a("a",{class:"header-anchor",href:"#basic-command","aria-hidden":"true"},"#"),e(" Basic Command")],-1),Is=a("ul",null,[a("li",null,"r(run)"),a("li",null,"l(list)"),a("li",null,[e("b(break) "),a("ul",null,[a("li",null,"b line_num"),a("li",null,"b filename:line_num")])]),a("li",null,"display/format address")],-1),Ps=a("p",null,"e.g display/i $pc",-1),Ms=a("ul",null,[a("li",null,[a("p",null,"t 按二进制格式显示变量")]),a("li",null,[a("p",null,"d 按十进制格式显示变量")]),a("li",null,[a("p",null,"o 按八进制格式显示变量")]),a("li",null,[a("p",null,"u 按十六进制格式显示无符号整型")]),a("li",null,[a("p",null,"x 按十六进制格式显示变量")]),a("li",null,[a("p",null,"a 按十六进制格式显示变量")]),a("li",null,[a("p",null,"f 按浮点数格式显示变量")]),a("li",null,[a("p",null,"c 按字符格式显示变量")]),a("li",null,[a("p",null,"s 按字符串格式显示变量")]),a("li",null,[a("p",null,"disas 显示汇编代码")]),a("li",null,[a("p",null,"x /num-size-format $pc/rsp/rbp")])],-1),Bs=a("p",null,"e.g size:w(2 字节) format:x/d/s(十六进制/十进制/字符串) 2wx",-1),Us=a("ul",null,[a("li",null,[e("examine - 查看内存 n、f、u 是可选的参数 "),a("ul",null,[a("li",null,"n 是一个正整数,表示显示内存的长度,也就是说从当前地址向后显示几个地址的内容。"),a("li",null,"f 表示显示的格式,参见上面。如果地址所指的是字符串,那么格式可以是 s,如果地十是指令地址,那么格式可以是 i。"),a("li",null,"u 表示从当前地址往后请求的字节数,如果不指定的话,GDB 默认是 4 个 bytes. u 参数可以用下面的字符来代替,b 表示单字节,h 表示双字节,w 表示四字节,g 表示八字节. 当我们指定了字节长度后,GDB 会从指内存定的内存地址开始,读写指定字节,并把其当作一个值取出来. 表示一个内存地址.")])])],-1),Fs=a("blockquote",null,[a("p",null,"命令:x/3uh 0x54320 表示,从内存地址 0x54320 读取内容,h 表示以双字节为一个单位,3 表示三个单位,u 表示按十六进制显示。")],-1),Gs=a("ul",null,[a("li",null,[e("s step into "),a("ul",null,[a("li",null,"step1 下一条汇编指令")])]),a("li",null,"n next line"),a("li",null,"p print"),a("li",null,"q quit"),a("li",null,"up last stack"),a("li",null,"bt(back trace) function stack 显示堆栈回溯信息"),a("li",null,"info breakpoints/register")],-1),Hs=a("h3",{id:"gdb-set-command"},[a("a",{class:"header-anchor",href:"#gdb-set-command","aria-hidden":"true"},"#"),e(" GDB Set Command")],-1),zs=a("ul",null,[a("li",null,"set disassembly"),a("li",null,"set variable")],-1),Xs=a("h3",{id:"gdb-shell"},[a("a",{class:"header-anchor",href:"#gdb-shell","aria-hidden":"true"},"#"),e(" GDB Shell")],-1),js=a("p",null,"shell command",-1),qs=a("h3",{id:"gdb-assemble"},[a("a",{class:"header-anchor",href:"#gdb-assemble","aria-hidden":"true"},"#"),e(" GDB Assemble")],-1),Ks=a("h4",{id:"gdb-core-dump"},[a("a",{class:"header-anchor",href:"#gdb-core-dump","aria-hidden":"true"},"#"),e(" GDB Core Dump")],-1),Vs=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin class-name"},"ulimit"),e(" -c unlimited\ngdb -c core_file_path target_exe_path\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ys=a("h4",{id:"gdb-disasm"},[a("a",{class:"header-anchor",href:"#gdb-disasm","aria-hidden":"true"},"#"),e(" GDB Disasm")],-1),Ws=a("ul",null,[a("li",null,"CS Segment")],-1),Qs=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" disass\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" x/i\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" x/5i "),a("span",{class:"token variable"},"$pc"),e("\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" ni/si\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Js=a("ul",null,[a("li",null,"Registers")],-1),$s=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" i r\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" i r a\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" i r ds\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Zs=a("ul",null,[a("li",null,"DS Segment"),a("li",null,"SS Segment")],-1),na=a("h3",{id:"gdb-stack-frame"},[a("a",{class:"header-anchor",href:"#gdb-stack-frame","aria-hidden":"true"},"#"),e(" GDB Stack Frame")],-1),sa=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" bt\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" frame n\n"),a("span",{class:"token punctuation"},"("),e("gdb"),a("span",{class:"token punctuation"},")"),e(" info locals\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),aa=a("h2",{id:"gcc"},[a("a",{class:"header-anchor",href:"#gcc","aria-hidden":"true"},"#"),e(" GCC")],-1),ea=a("ul",null,[a("li",null,"-E: cpp(c preprocessor) 预处理 => .i"),a("li",null,"-S: cll 编译 => .s"),a("li",null,"-c: as(assemble) 汇编 => .o"),a("li",null,"-time Time the execution of each subprocess"),a("li",null,[a("code",null,"-std="),e(" Assume that the input sources are for "),a("code",null,"")]),a("li",null,[a("code",null,"-B "),e(" Add "),a("code",null,""),e(" to the compiler's search paths")]),a("li",null,"-v Display the programs invoked by the compiler"),a("li",null,[a("code",null,"-o "),e(" Place the output into "),a("code",null,"")]),a("li",null,"-shared Create a shared library"),a("li",null,[a("code",null,"-Wall")]),a("li",null,[a("code",null,"-v --help")])],-1),ta=a("h2",{id:"awesome-tools"},[a("a",{class:"header-anchor",href:"#awesome-tools","aria-hidden":"true"},"#"),e(" Awesome Tools")],-1),la=a("ul",null,[a("li",null,"用于创建代码文档资料的 NDoc"),a("li",null,"用于生成解决方案的 NAnt"),a("li",null,"用于生成代码的 CodeSmith"),a("li",null,"用于监视代码的 FxCop"),a("li",null,"用于编译少量代码的 Snippet Compiler"),a("li",null,[e("两种不同的转换器工具:"),a("a",{href:"http://ASP.NET",target:"_blank",rel:"noopener noreferrer"},"ASP.NET"),e(" 版本转换器和 Visual Studio .NET 项目转换器")]),a("li",null,"用于生成正则表达式的 Regulator"),a("li",null,"用于分析程序集的 .NET Reflector"),a("li",null,"用于单元测试的 NUnit")],-1);l.render=function(a,e,t,l,oa,ca){return n(),s("div",null,[o,c,r,p,i,u,k,d,m,b,h,y,f,v,g,w,x,E,_,N,S,T,O,L,A,C,D,R,I,P,M,B,U,F,G,H,z,X,j,q,K,V,Y,W,Q,J,$,Z,nn,sn,an,en,tn,ln,on,cn,rn,pn,un,kn,dn,mn,bn,hn,yn,fn,vn,gn,wn,xn,En,_n,Nn,Sn,Tn,On,Ln,An,Cn,Dn,Rn,In,Pn,Mn,Bn,Un,Fn,Gn,Hn,zn,Xn,jn,qn,Kn,Vn,Yn,Wn,Qn,Jn,$n,Zn,ns,ss,as,es,ts,ls,os,cs,rs,ps,is,us,ks,ds,ms,bs,hs,ys,fs,vs,gs,ws,xs,Es,_s,Ns,Ss,Ts,Os,Ls,As,Cs,Ds,Rs,Is,Ps,Ms,Bs,Us,Fs,Gs,Hs,zs,Xs,js,qs,Ks,Vs,Ys,Ws,Qs,Js,$s,Zs,na,sa,aa,ea,ta,la])};export default l;export{t as __pageData}; diff --git a/assets/language_cpp_cppBasicNotes.md.212edeb8.js b/assets/language_cpp_cppBasicNotes.md.212edeb8.js new file mode 100644 index 00000000000..0a0220881f1 --- /dev/null +++ b/assets/language_cpp_cppBasicNotes.md.212edeb8.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"C++ Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Workflow","slug":"basic-workflow"},{"level":3,"title":"初始化与回收","slug":"初始化与回收"},{"level":2,"title":"变量","slug":"变量"},{"level":3,"title":"指针","slug":"指针"},{"level":3,"title":"reference (引用类型)","slug":"reference-引用类型"},{"level":3,"title":"volatile 类型","slug":"volatile-类型"},{"level":2,"title":"运算符","slug":"运算符"},{"level":3,"title":"左/右值运算符(运算结果为左/右值)","slug":"左-右值运算符-运算结果为左-右值"},{"level":3,"title":"sizeof","slug":"sizeof"},{"level":2,"title":"Expression (表达式)","slug":"expression-表达式"},{"level":3,"title":"左/右值表达式","slug":"左-右值表达式"},{"level":2,"title":"联合(union)","slug":"联合-union"},{"level":3,"title":"位段","slug":"位段"},{"level":2,"title":"函数","slug":"函数"},{"level":3,"title":"Default Params","slug":"default-params"},{"level":3,"title":"内联函数(inline)","slug":"内联函数-inline"},{"level":2,"title":"类","slug":"类"},{"level":3,"title":"访问控制权限","slug":"访问控制权限"},{"level":3,"title":"构造函数","slug":"构造函数"},{"level":3,"title":"析构函数","slug":"析构函数"},{"level":3,"title":"构造与析构(重点)(P148)","slug":"构造与析构-重点-p148"},{"level":3,"title":"New and Delete","slug":"new-and-delete"},{"level":3,"title":"This Pointer","slug":"this-pointer"},{"level":3,"title":"成员指针 与 指针成员","slug":"成员指针-与-指针成员"},{"level":3,"title":"函数成员","slug":"函数成员"},{"level":3,"title":"静态成员","slug":"静态成员"},{"level":3,"title":"友元","slug":"友元"},{"level":3,"title":"单继承","slug":"单继承"},{"level":3,"title":"虚函数","slug":"虚函数"},{"level":2,"title":"作用域(Scope)","slug":"作用域-scope"},{"level":3,"title":"面向过程的作用域","slug":"面向过程的作用域"},{"level":3,"title":"面向对象的作用域","slug":"面向对象的作用域"},{"level":2,"title":"运算符重载","slug":"运算符重载"},{"level":3,"title":"重载原则","slug":"重载原则"},{"level":3,"title":"自增/减运算符","slug":"自增-减运算符"},{"level":3,"title":"成员运算符","slug":"成员运算符"},{"level":3,"title":"赋值运算符","slug":"赋值运算符"},{"level":3,"title":"强制类型转换","slug":"强制类型转换"},{"level":2,"title":"模板","slug":"模板"},{"level":3,"title":"模板调用形式","slug":"模板调用形式"},{"level":2,"title":"异常","slug":"异常"},{"level":3,"title":"异常对象的析构","slug":"异常对象的析构"},{"level":2,"title":"多态","slug":"多态"},{"level":2,"title":"STL","slug":"stl"},{"level":3,"title":"String","slug":"string"},{"level":3,"title":"Input Output","slug":"input-output"},{"level":3,"title":"Container","slug":"container"},{"level":3,"title":"Heap Algorithm","slug":"heap-algorithm"},{"level":3,"title":"Sorting Algorithms","slug":"sorting-algorithms"},{"level":3,"title":"Partitioning Algorithms","slug":"partitioning-algorithms"},{"level":3,"title":"Permutation Algorithms","slug":"permutation-algorithms"},{"level":3,"title":"Numeric Algorithms","slug":"numeric-algorithms"},{"level":3,"title":"Query Algorithms","slug":"query-algorithms"},{"level":3,"title":"Search Algorithms","slug":"search-algorithms"},{"level":3,"title":"Set Algorithms","slug":"set-algorithms"},{"level":3,"title":"Mover Algorithms","slug":"mover-algorithms"},{"level":3,"title":"Value Algorithms","slug":"value-algorithms"},{"level":3,"title":"Functional Algorithms","slug":"functional-algorithms"},{"level":2,"title":"Awesome Tips and Best Practice","slug":"awesome-tips-and-best-practice"},{"level":3,"title":"静态成员 BP","slug":"静态成员-bp"},{"level":3,"title":"友元函数","slug":"友元函数"},{"level":3,"title":"单继承 BP","slug":"单继承-bp"},{"level":3,"title":"虚函数 BP","slug":"虚函数-bp"},{"level":3,"title":"Error BP","slug":"error-bp"},{"level":3,"title":"Inherits Output","slug":"inherits-output"}],"relativePath":"language/cpp/cppBasicNotes.md","lastUpdated":1627196319000}',e={},t=a('

C++ Basic Notes

Basic Workflow

初始化与回收

  • Treat global program as a object: 开工函数与收工函数
  • Normal Object: 构造函数与析构函数

变量

const 变量 代替 #define 宏定义

指针

  • 只读指针可赋值为 普通对象地址
  • 普通指针不可赋值为 只读对象地址
int *p = const int;         // x\nint *p = int;               // o\nconst int *p = const int;   // o\nconst int *p = int;         // o\n
1
2
3
4

reference (引用类型)

性质

引用类型(&)实质上是一种语法糖,编译器将引用变量最终全部编译为指针变量

作为函数参数

向函数传递引用类型实参时,若:

  • 实参与形参类型不一致(必须完全一致)
  • 实参为(右)值表达式

则会生成一个内部匿名变量,用于函数调用. 此时,对参数进行的操作将无法改变实参原有值,使得 call by reference 失效.

特性

  • 普通全局引用变量必须在定义时初始化初始化(左值表达式)
int &x = 1;   // warning: 引用匿名变量\nint &k = j++; // warning: 引用匿名变量\n
1
2
  • 引用变量不分配存储单元: 不能引用 引用变量, 不能作为指针所指对象, 不能作为数组元素
int & &x;       // error\nint & *p;       // error\nint & s[4];     // error\n
1
2
3
  • 引用变量的值按字节(指针/地址)编码: 不能引用位段(无法按字节编码)
int &w = a.j; // error\n
1
  • 引用变量不能引用引用变量, 只能引用另一个引用变量所引用的变量/地址
int x = 10;\nint &j = x;\nint &&m = j;  // error\nint &m = j;   // pass: m = j => x\n
1
2
3
4
  • 引用变量可以引用 register 变量, 编译器会将其自动转为 auto 变量(为其分配地址)
register int i = 0,\nregister int &j = i;\n
1
2
  • 引用变量在一定程度上具有指针性质
int x = 1;\nconst int &p = x;   // pass\n\nx = 7;  // pass\np = 7;  // error\n
1
2
3
4
5

volatile 类型

  • volatile 表示可被其他线程/进程改变的变量
  • volatile 变量常作为全局变量, 用于同步各进程
  • const 表示不可被本线程/进程修改的变量
volatile int x;\n\nx = 3;\n\nif (4 == x) {\n    cout << "X changed by other routines.";\n}\n
1
2
3
4
5
6
7

运算符

左/右值运算符(运算结果为左/右值)

  • 左值运算符: 前置++/--, =/+=/*=/&=/op=
  • 右值运算符: 强制类型转换(不可对其运算结果进行取地址), +/*/binary

sizeof

sizeof 数值\nsizeof(数值)\nsizeof(类型)\n\nsizeof(printf("abcd")); // 无输出\n
1
2
3
4
5

Expression (表达式)

左/右值表达式

左值表达式一定可作(右)值表达式, (右)值表达式不可作左值表达式:

  • 左值表达式 : 变量, 赋值表达式, 前缀自操作表达式, 返回值为引用类型的函数调用
bar;\ny = 6;\ni -= 10;\n++i;\n\nint &f(void);\nf() = j;\n
1
2
3
4
5
6
7
  • (右)值表达式: 常量, 强制类型转换表达式, 后缀自操作表达式, 算数表达式
20;\n(type)x;\ni++;\ny + 2;\n
1
2
3
4

联合(union)

匿名联合具有以下性质:

  • 没有对象的全局匿名联合必须 static
  • 只可定义 public 成员
  • 数据成员与联合本身作用域相同
  • 数据成员共享存储空间

位段

class/struct/union 都可定义位段成员, 但类型必须为 char/short/int/enum, 不可为 long/float/array/class

函数

Default Params

  • 不能在 函数原型声明函数定义同时 定义参数的默认值,会产生默认值冲突
  • 所有缺省参数必须位于参数列表右边
  • 不能用前一个参数初始化后一个参数
  • 当同时有 int g(void) int g(int x = 1) 时, 不能调用 g() (具有二义性)
int bar(int x , int y = 5, int z = m(u,v));\nint foo(int x, int y = x++);    // error\n
1
2

内联函数(inline)

以下情况会造成内联失败:

  • 内联函数中使用 分支/循环/开关/函数调用
  • 内联函数定义出现在调用后面(先调用后定义)
  • 其他函数访问了内联函数入口地址
  • 内联函数定义为(纯)虚函数

内联最终结果:

  • 内联成功后, 原函数会被编译器清除
  • 不管内联是否成功, 内联函数作用域局限于当前源文件
  • 全局 extern main 函数不能定义为内联函数(否则会使得主函数作用域变小, 操作系统无法访问主函数)
  • 在类体内实现的任何函数自动变为内联函数

访问控制权限

public > protected > private + friend > private

构造函数

调用形式

构造函数不可被显式调用(类前缀), 必须隐式调用(省略类前缀)

构造函数体

  • 构造函数体前: 初始化只读成员、引用成员、对象成员、其他数据成员, 初始化顺序以定义顺序为准, 无关构造函数体前出现顺序
  • 构造函数体内: 再次赋值其他数据成员(不可再次只读成员、引用成员、对象成员)

默认无参构造函数

  • 当定义了含参构造函数后, 编译器将不会再为对象自动添加无参构造函数
  • 默认无参构造函数不会初始化只读/引用成员, 且只会调用对象成员的无参构造函数(若对象成员没有无参构造函数,则编译器报错)
  • 对于局部对象, 将随机初始化普通数据成员; 对于全局对象, 将普通数据成员初始化为 0. 由于默认无参构造函数的存在, 当对象只含有普通数据成员(无只读/引用/指针成员, 且对象成员有无参构造函数), 可以不显式定义构造函数
class Foo {\n    const int b;\n    int c, &d, e, f;\n    String g, h;\n\npublic:\n    // 初始化顺序: b, c, d, e, f, g, h\n    Foo(int bar): d(c), c(bar), g(bar), b(bar), e(bar) {\n        c += bar;\n        f = bar;\n    }\n};\n
1
2
3
4
5
6
7
8
9
10
11
12
B z(7, 8); => B z(7, 8);    ///< 2 参\nB z = (7, 8); => B z(8);    ///< 1 参\n
1
2

构造顺序

  • 同一派生树上所有虚基类(自左向右, 自下向上)(递归)
  • (继承顺序)直接基类(递归)
  • (定义顺序)所有成员
  • 构造函数体

深拷贝构造函数

  • 形式为 Foo:Foo(Foo &obj) 的构造函数,可使得对象作为实参传递时自动进行深拷贝复制
ARRAY::ARRAY(ARRAY &r) {\n    p = new int[size = r.size];\n\n    for (int i = 0;i < size; i++) {\n        p[i] = r.p[i];\n    }\n}\n
1
2
3
4
5
6
7

Move Constructor

class A {\n  A(const A&& a) {\n    // move constructor\n    // set a == null_ptr\n  }\n}\n
1
2
3
4
5
6

析构函数

  • 析构函数即可显式调用,又可隐式调用
  • 析构函数与全局 main 函数 没有重载函数
  • 作用域结束时会自动调用析构函数
  • 调用 exit/abort 时, 需手动调用析构函数释放资源
String x("global");\n\nint main(void) {\n    short error = 0;\n    String y("local");\n\n    // set error flag\n    switch (error) {\n        case 0:\n            return;\n        case 1:\n            y.~String();\n            exit(1);\n        default:\n            x.~String();\n            y.~String();\n            abort();\n    }\n\n    return 0;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  • 设置 析构标志 防止重复析构同一对象
String::~String() {\n    // check flag\n    if (s == NULL) {\n        return;\n    }\n\n    cout<<"Deconstruct:"<<s;\n\n    free(s);\n\n    // set flag\n    s = NULL;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13

构造与析构(重点)(P148)

  • 按定义顺序自左下至右上地构造所有虚基类
  • 按定义顺序构造直接基类
  • 按定义顺序构造数据成员(对象/const/引用/普通成员)
  • 执行类构造函数体
  • 递归执行以上过程

派生树

  • 一个对象/对象成员一颗单独的派生树
  • 单独的派生树中, 合并同名虚基类, 不合并同名基类, 并 { name(type), ...} 标示数据成员

New and Delete

Stack and Heap

  • string str("sabertazimi") 创建在栈上, 自动析构
  • new/malloc 返回堆指针, delete/free 的对象是堆指针/(&引用变量), 完全由程序员管理创建与回收
int x = 5;\nint *p = &x;\nint &q = x;\n\ndelete p;   // address of x\ndelete &q;  // address of x\n
1
2
3
4
5
6

指针成员

  • 普通指针/不含指针成员的对象变量分配/回收内存可混用 malloc/new/free/delete/delete []
  • 创建/回收含有指针成员的类时,只能用 new/delete/delete [](分配对象内存+调用构造/析构函数), 不能用 malloc/free(只作用于对象本身,不调用构造/析构函数,即不为指针成员分配/回收内存), 否则会造成指针成员未分配内存/内存泄露
  • new 对象数组实质: malloc 对象 + 调用对象无参构造函数

This Pointer

普通函数成员比静态函数成员多一个隐含参数 this 指针, 其会随着函数类型的不同而改变类型

  • this 指针指向对象起始地址处(对象首成员地址)
  • 一般函数: class_type *const this;
  • const 函数: const class_type *const this;, 可以修改 this 所指对象的非只读静态数据成员
  • volatile 函数: volatile class_type *const this;
  • 当 this 指针类型不同时, 亦会产生重载函数

成员指针 与 指针成员

成员指针 - 指向成员的指针

  • 成员指针不是地址, 而是偏移量
  • 考虑到越界问题,成员指针不可移动
  • 成员指针不可进行类型转换, 不可将其转换为其他类型, 也不可将其他类型转换为它
  • 作用: 成员别名 (a.*pf)() => a.size()
  • 优先级: . > * > .*(结合性: 自左向右)
int A::*pi = &A::i;         ///< 数据成员指针\nint (A::*pf)(void) = &A::f;     ///< 函数成员指针\n\nlong x = a.*pi;     ///< x = a.*pi = a.*(&A::i) = a.A::i = a.i;\nx = (a.*pf)();      ///< x = (a.*pf)() = (a.*(&A::f))() = (a.A::f)() = a.f()\n\npi++, pf+=1;        ///< Error: 成员指针不可移动\nx = (long)pi;       ///< Error: pi 不能转换为 long int\n
1
2
3
4
5
6
7
8

定义含指针成员的类

  • 深拷贝构造函数: T(const T &)
  • 深拷贝赋值运算函数: virtual T& operator=(const T &)
  • 虚析构函数: virtual ~T()
  • 定义T &p = *new T()后, 使用delete &p释放内存
  • 定义T *p = new T()后, 使用delete p释放内存

函数成员

Const and Volatile Member

  • 修饰函数成员隐含参数 * this 对象指针,表示不可修改/挥发对象
  • 被修饰函数成员不能修改对象自身(即对象的普通数据成员), 可以修改对象的非只读静态数据成员
  • 构造函数/析构函数不可被 const/volatile 修饰: 在构造/析构函数中, 对象必须可以被修改(No const), 且处于稳定状态(No volatile)

Mutable Member

  • mutable 不可修饰引用成员
  • mutable 成员不可用 const/volatile/static 修饰
  • const 函数中可以修改 mutable 成员

静态成员

静态数据成员

static member 特性
  • 静态数据成员脱离对象实例存在 Person::totalNum
  • 静态数据成员存储单元不属于任何对象实例
定义
  • 不能在构造函数/析构函数创建/释放静态数据成员的存储单元
  • 在类体内声明静态数据成员, 在类体外定义并初始化静态数据成员 List * List::head = NULL;(包括私有静态数据成员)
  • 局部类/union 类不可定义静态数据成员, 局部类/union 类可以定义静态函数成员

静态函数成员

  • 静态函数成员不含隐含参数(this 对象指针)
  • 构造/析构/virtual/const/volatile 函数(均有 this 对象指针)不能定义为静态函数成员
  • 体内实现为 inline 函数, 体外实现不能有 static 关键字
引用形式
  • 抽象类型 List::sort()
  • 对象实例 list.sort()
Static Function Member
  • 只能直接修改当前对象静态数据成员
  • 可以通过静态函数成员的函数参数, 传入对象实例指针, 从而间接修改非静态数据成员

静态成员指针

除了具有访问权限外, 静态成员指针就是普通指针(可以进行指针加减运算)

int *d = &CROWD::number;\nint (*f)() = &CROWD::getnumber;\n
1
2

static 关键字

  • 位于 class 体内: 表示该成员/方法共享存储单元(不限定作用域)
  • 位于 class 体外: 表示该变量/函数具有静态存储周期(限定作用域为当前文件)

友元

  • 在重载函数中, (函数签名不一致的) 未声明的函数只能访问类的共有成员
  • friend 声明不能与存储类型关键字共用(static/virtual)
  • 定义友元函数(使用 friend 修饰函数声明)时: 友元函数不是此处的函数成员, 可随意指定 返回类型与访问控制权限

成员友元函数

可在 3 处实现函数体:

  • 函数定义处(本类): 内联函数成员
  • 友元声明处(它类): 内联友元函数(作用域局限于当前程序文件)
  • 类体外(不使用 friend 关键字)

单继承

派生控制权限

  • 降低/维持权限
  • class 缺省为 private 派生控制, struct 缺省为 public 派生控制
  • 只可恢复至基类原权限, 不可随意修改基类成员权限

父类与子类

  • 子类对象地址可以赋值给父类指针, 子类指针可以直接赋值给父类指针
  • 父类对象地址不可以赋值给子类指针, 父类指针即使强制类型转换后也不可以赋值给子类指针

虚函数

virtual function 特性

  • 无限传递性: 派生类中原型相同的函数自动成为虚函数
  • 具有隐式参数(this 对象指针): 不能为静态函数成员(静态函数无 this 参数)/非函数成员(本类友元函数), 可以为它类成员友元函数
  • 构造函数不能为虚函数, 析构函数可以为虚函数
  • virtual 与 friend/static 关键字不可共用
  • union 既不能定义基类也不能定义派生类, 故不能在 union 中定义虚函数

功能

实现运行时多态. 最好将普通函数成员全部定义为 虚函数

纯虚函数 与 抽象类

  • 纯虚函数具有虚函数的所有特性
  • 具有纯虚函数的类自动成为抽象类
struct A {\n    virtual void f1() = 0;\n    virtual void f2() = 0;\n};\n
1
2
3
4

易错点

  • virtual 关键字只能在 struct/class 体内使用, 在体外实现相应函数成员时, 应去掉 virtual 关键字
  • 当在函数成员中调用虚函数时, 会根据 this 所指真实对象动态调用虚函数(如在基类函数成员中调用虚函数, 可能实际调用的是子类的虚函数)
  • 使用基类指针/基类引用指向派生类时, 只可按基类访问控制权限调用基类拥有的方法
  • 不可以new 抽象类(堆实例)/构造抽象类的栈实例, 函数参数/返回值不可直接定义为抽象类(调用时无法构造栈实例), 但可以定义为抽象类的引用/指针
Parent *p = new Child(5);\np->~Child();    // Error: method not defined\np->~Parent();   // Success: invoke Child::~Child()\n
1
2
3

作用域(Scope)

  • 标识符的作用域越小,访问的优先级别越高
  • 单目运算符 :: 可访问全局符号 e.g ::process++ // 自加全局变量 process

面向过程的作用域

  • 作用于表达式内
  • 作用于函数内
  • 作用于程序文件内
  • 作用于整个程序

面向对象的作用域

  • 作用于函数成员
  • 作用于类/派生类
  • 作用于基类
  • 作用于虚基类

命名空间(namespace)

  • 指定一个完全的命名空间时, 不会将任何标识符加入当前作用域. 可重新定义同名局部符号
namespace A {\n    int a = 0;\n    namespace B {\n        int a = 0;\n    }\n    namespace C {\n\n    }\n    namespace D {\n\n    }\n    using namespace B;\n    using namespace C;\n}\n\nusing namespace A;\nint a = 5;      ///< Right: 全局变量 a 与 A中的a 同名\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • 指定一个命名空间的具体成员时, 会将成员符号加入当前作用域
namespace A {\n    float a = 0,\n          b = 0;\n    float d(float y) {\n        return y;\n    }\n}\n\nnamespace B {\n    void g(void) {\n        cout<<"B\\n">>;\n    }\n}\n\nint main(void) {\n    using A::a;\n    using A::d;\n    using B::g;\n\n    long a = 1;     ///< Error: a 已被加入当前 main 函数作用域(A::a), 不可重复定义\n    a = d(2.1);     ///< Right: A::a = A::d(2.1);\n\n    return 0;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  • 可以为嵌套命名空间定义别名
namespace A {\n    namespace B {\n        namespace C {\n            int k = 4;\n        }\n    }\n}\n\nnamespace ABCD = A::B::C;\n\nusing ABCD::k;      ///< refer to A::B::C::k\n
1
2
3
4
5
6
7
8
9
10
11

运算符重载

运算符重载方式
sizeof . .* :: ?:不可重载
= -> () [](必须有 this 指针)普通函数成员
new delete(不可有 this 指针)静态函数成员 普通函数
其他运算符普通函数成员 普通函数

重载原则

  • 若运算符为左值运算符, 则重载返回类型为非 const 引用类型
  • 若运算符为非左值运算符, 则重载返回类型为普通类型(栈内存)或 const 类型
  • 重载为普通函数成员, this 占据第一个参数位置
  • 重载不改变运算符的优先级与结合性

自增/减运算符

A &A::operator++();     ///< 前置++\nA A::operator++(int);   ///< 后置++, int 参数只起区分作用\n
1
2

成员运算符

只能重载为普通函数成员, 只有一个参数(+ this), 返回类型必须为指针/引用类型

struct A {\n    int a;\n};\n\nclass B {\n    A x;\npublic:\n    A *operator->() {\n        return &x;\n    }\n};\n\nint main(void) {\n    B b;\n    int i = b->a;               // i = b.x.a\n    i = (*b.operator->()).a;    // i = b.x.a\n    i = b.operator->()->a;      // i = b.x.a\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

赋值运算符

  • 所有对象都有默认的 = 重载: 浅拷贝赋值运算

强制类型转换

  • 当定义只有一个参数的构造函数时, 进行运算时会自动发生类型转换(利用右值调用此构造函数, 生成中间变量赋给左值)
Complex m;\nm + 2 => m + 2.0 => m + Complex(2.0);\n
1
2

模板

模板调用形式

  • 函数标签相同的覆盖函数
  • 通过函数模板自动生成可匹配参数的实例函数
  • 通过强制类型转换,调用已经手动生成的可匹配参数的实例函数
  • 调用失败

异常

异常对象的析构

catch (const A *a) 形式

多态

  • 编译时多态(重载): 静态绑定(同名不同参的)重载函数 - 一个方法, 多种参数 (f(void) => f@, f(int) => f@i, f(int, int) => f@ii)
  • 运行时多态(多态): 动态绑定(upcasting)虚函数 - 一个接口, 多个方法(父子表现不同, 兄弟表现不同)
Parent c = new Child();\nc.speak("Hello World!") // => "Child: Hello World!"\n
1
2

STL

  • 工作方式: copy in, copy out

String

#include <iostream>\n#include <malloc.h>\n#include <string.h>\n\nusing namespace std;\n\n/**\n *\n * Demos for operator overload:\n *  comparison: < == >\n *  getter: []\n *  typecast: (const char *)\n *  contact: +\n *  assign: = +=\n */\n\n/**\n * Notes:\n *  普通函数成员 与 析构函数 全部定义为虚函数\n *  不改变对象实例的函数 全部定义为 const 函数\n *  被改变参数/返回值 全部定义为 引用类型\n *  深拷贝(赋值)函数参数/返回值 全部定义为 引用类型\n */\n\nclass String {\n    char *s;\npublic:\n    virtual int operator>(const String &c) const;\n    virtual int operator==(const String &c) const;\n    virtual int operator<(const String &c) const;\n    virtual char &operator[](int x);\n    virtual operator const char *() const;\n    virtual String operator+(const String &c) const;\n    virtual String &operator=(const String &c);\n    virtual String &operator+=(const String &c);\n    String(const char *c);\n    String(const String &c);\n    virtual ~String();\n};\n\nint String::operator>(const String &c) const {\n    return strcmp(s, c.s) > 0;\n}\n\nint String::operator==(const String &c) const {\n    return strcmp(s, c.s) == 0;\n}\n\nint String::operator<(const String &c) const {\n    return strcmp(s, c.s) < 0;\n}\n\nchar &String::operator[](int x) {\n    return s[x];\n}\n\nString::operator const char *() const {\n    return s;\n}\n\nString String::operator+(const String &c) const {\n    char *t = new char[strlen(s)+strlen(c.s)+1];\n    String r(strcat(strcpy(t,s), c.s));\n    delete []t;\n    return r;\n}\n\nString &String::operator=(const String &c) {\n    delete []s;\n    strcpy(s=new char[strlen(c.s)+1], c.s);\n    return *this;\n}\n\nString &String::operator+=(const String &c) {\n    return *this = *this+s;\n}\n\nString::String(const char *c) {\n    strcpy(s=new char[strlen(c)+1], c);\n}\n\nString::String(const String &c) {\n    strcpy(s=new char[strlen(c.s)+1], c.s);\n}\n\nString::~String(void) {\n    if (s) {\n        delete []s;\n        s = 0;\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

Input Output

iomanip STL API

output format

#include <iostream>\n#include <iomanip>\n\nint main() {\n    std::cout << "default fill: " << std::setw(10) << 42 << '\\n'\n              << "setfill('*'): " << std::setfill('*')\n                                  << std::setw(10) << 42 << '\\n';\n\n    std::cout << rd.name << " 0x"\n              << std::setfill('0') << std::setw(16) << std::hex\n              << get_register_value(m_pid, rd.r) << std::endl;\n}\n\n// Output:\n// default fill:         42\n// setfill('*'): ********42\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Container

  • 序列容器: vector string deque list
  • 关联容器: set multiset map multimap
  • 连续内存容器: vector string deque
  • 链表容器: list slit hash

unordered_map container

  • count/find
  • []
if (m_breakpoints.count(possible_breakpoint_location)) {\n    auto& bp = m_breakpoints[possible_breakpoint_location];\n}\n\nauto alias = m_aliases.find(input);\nreturn (alias != m_aliases.end() && alias->second == command);\n
1
2
3
4
5
6

Container Choosing

  • Vector vs Array: use Array when size is fixed and need elaborate container-like behavior
  • Vector vs Deque: use Deque when container is large
  • Vector vs List (no random access): use List when many middle insertion/deletion/splicing
  • Vector vs Set/Map: use Set/Map when container is large

List Size

list.empty() 优于 list.size() == 0

List Manipulation

void container::insert(InputIterator begin, InputIterator end);\nvoid container::insert(iterator position, InputIterator begin, InputIterator end);\n\niterator container::erase(iterator begin, iterator end);    // 序列容器\nvoid container::erase(iterator begin, iterator end);        // 关联容器\n\nvoid container::assign(InputIterator begin, InputIterator end);\n
1
2
3
4
5
6
7
v1.assign(v2.begin() + v2.size() / 2, v2.end());\nv1.insert(v1.end(), v2.begin() + v2.size() / 2, v2.end())   // 将 v2 一半元素插入 v1 尾部\n
1
2

List Remove

  • 对于连续内存容器 vector/string/deque:
c.erase(remove(c.begin(), c.end(), 1963), c.end())\n\nbool badValue(int);\nc.erase(remove_if(c.begin(), c.end(), badValue), c.end());\n
1
2
3
4
  • 对于 list:
c.remove(1963)\n\nbool badValue(int);\nc.remove_if(badValue);  // higher order function\n
1
2
3
4
  • 对于关联容器:
c.erase(1963);\n
1

List Reserve and Swap

v.reserve(1000);    // 强制分配 1000 个元素内存单元, 防止反复回收/释放, 提高执行效率\n\nstring(s).swap(s);  // 回收多余内存单元(shrink to fit), 提高内存利用率\n
1
2
3

Heap Algorithm

std::make_heap(begin(numbers), end(numbers));\nstd::push_heap(begin(numbers), end(numbers));\nstd::pop_heap(begin(numbers), end(numbers));\nstd::is_heap\nstd::is_heap_until\n
1
2
3
4
5

Sorting Algorithms

sort\nstable_sort\nstd::is_sorted\nstd::is_sorted_until\n\npartial_sort\nnth_element: 0 - nth-1 sorted (< nth), nth+1 - end (> nth) unsorted\nsort_heap\ninplace_merge\n
1
2
3
4
5
6
7
8
9

Partitioning Algorithms

partition_point\npartition\nstable_partition\nstd::is_partitioned\nstd::is_partitioned_until\n
1
2
3
4
5

Permutation Algorithms

rotate\nshuffle\nprev_permutation\nnext_permutation\n
1
2
3
4

Numeric Algorithms

count\ncount_if\n\naccumulate\n(transform_)reduce\n\npartial_sum\n(transform_)inclusive_scan\n(transform_)exclusive_scan\n\ninner_product\nadjacent_difference\nsample\n
1
2
3
4
5
6
7
8
9
10
11
12
13

Query Algorithms

all_of\nany_of\nnone_of\n\nequal\nlexicographical_compare\nmismatch\n
1
2
3
4
5
6
7

Equal STL Algorithm

template<class InputIt1, class InputIt2>\nbool equal(\n    InputIt1 first1,\n    InputIt1 last1,\n    InputIt2 first2\n) {\n    for (; first1 != last1; ++first1, ++first2) {\n        if (!(*first1 == *first2)) {\n            return false;\n        }\n    }\n\n    return true;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool is_prefix(const std::string& s, const std::string& of) {\n    if (s.size() > of.size()) return false;\n    return std::equal(s.begin(), s.end(), of.begin());\n}\n\nbool is_suffix(const std::string& s, const std::string& of) {\n    if (s.size() > of.size()) return false;\n    auto diff = of.size() - s.size();\n    return std::equal(s.begin(), s.end(), of.begin() + diff);\n}\n\nbool is_palindrome(const std::string& s) {\n    return std::equal(s.begin(), s.begin() + s.size() / 2, s.rbegin());\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Search Algorithms

search\nfind\nfind_if\nfind_if_not\nfind_first_of\nfind_end\nadjacent_find\n\nequal_range\nlower_bound\nupper_bound\n\nmin_element\nmax_element\nminmax_element\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Find STL Algorithms

template<class InputIterator, class T>\n  InputIterator find (InputIterator first, InputIterator last, const T& val) {\n  while (first!=last) {\n    if (*first==val) return first;\n    ++first;\n  }\n  return last;\n}\n
1
2
3
4
5
6
7
8
auto it = std::find_if(begin(g_register_descriptors), end(g_register_descriptors),\n                      [r](auto&& rd) { return rd.r == r; });\n
1
2

Set Algorithms

set_difference\nset_intersection\nset_union\nset_symmetric_difference\nincludes\nmerge\n
1
2
3
4
5
6

Mover Algorithms

copy\ncopy_backward\ncopy_if\nremove_copy\nunique_copy\nreverse_copy\nrotate_copy\nreplace_copy\nreplace_copy_if\npartition_copy\npartial_sort_copy\n\nmove\nmove_backward\nswap_ranges\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Value Algorithms

fill\ngenerate\nreplace\nreplace_if\niota\n
1
2
3
4
5

Functional Algorithms

transform\nfor_each\n
1
2

Awesome Tips and Best Practice

  • 普通函数成员 与 析构函数 全部定义为虚函数
  • 不改变对象实例的函数 全部定义为 const 函数
  • 被改变参数/返回值 全部定义为 引用类型
  • 深拷贝(赋值)函数参数/返回值 全部定义为 引用类型
  • 函数(传值)参数全部定义为 const & 类型: 既减少值的多余复制, 又保证原值不会被误修改

静态成员 BP

  • 静态函数成员只能直接修改当前对象静态数据成员, 可以通过传参修改参数的普通数据成员
  • 静态函数成员体内实现为 inline 函数, 体外实现不能有 static 关键字
  • 不能在构造函数/析构函数中 创建/释放 静态数据成员的存储单元
  • 在类体内声明静态数据成员, 在类体外定义并初始化静态数据成员 List * List::head = NULL;(包括私有静态数据成员)
  • 局部类/union 类不可定义静态数据成员, 局部类/union 类可以定义静态函数成员

友元函数

  • 定义友元函数(使用 friend 修饰函数声明)时: 友元函数不是此处的函数成员, 可随意指定 返回类型与访问控制权限

单继承 BP

  • 只可恢复至基类原权限, 不可随意修改基类成员权限
  • 子类对象地址可以赋值给父类指针, 子类指针可以直接赋值给父类指针
  • 父类对象地址不可以赋值给子类指针, 父类指针即使强制类型转换后也不可以赋值给子类指针

构造与析构

  • 构造函数体前: 初始化只读成员、引用成员、对象成员、其他数据成员, 初始化顺序以定义顺序为准, 无关构造函数体前出现顺序
  • 构造函数体内: 再次赋值其他数据成员(不可再次只读成员、引用成员、对象成员)
  • 当定义了含参构造函数后, 编译器将不会再为对象自动添加无参构造函数
  • 默认无参构造函数不会初始化只读/引用成员, 且只会调用对象成员的无参构造函数(若对象成员没有无参构造函数,则编译器报错)

虚函数 BP

  • virtual 关键字只能在 struct/class 体内使用, 在体外实现相应函数成员时, 应去掉 virtual 关键字
  • 当在函数成员中调用虚函数时, 会根据 this 所指真实对象动态调用虚函数(如在父类函数成员中调用虚函数, 可能实际调用的是子类的虚函数)
  • 使用基类指针/基类引用指向派生类时, 只可按基类访问控制权限调用基类拥有的方法
  • 不可以new 抽象类(堆实例)/构造抽象类的栈实例, 函数参数/返回值不可直接定义为抽象类(调用时无法构造栈实例), 但可以定义为抽象类的引用/指针

Error BP

  • class 体内声明为 friend/static/virtual 方法, class 体外实现时不加 friend/static/virtual 关键字
  • const/static 使用错误
static int x;\nA(x++, y++); // 若 A 中改变x, 会使得 x++ 无效化\n
1
2
  • 不可以new 抽象类(堆实例)/构造抽象类的栈实例, 函数参数/返回值不可直接定义为抽象类(调用时无法构造栈实例), 但可以定义为抽象类的引用/指针
  • 返回类型不匹配错误
  • 初始化构造函数不存在(基类定义有参构造函数, 派生类必须显示调用父类构造函数)错误
  • 引用/const/对象成员初始化错误
  • 访问控制权限错误(e.g 在全局声明一个只有私有构造函数的类实例)
  • 派生控制权限错误(只可恢复原先权限, 不可任意修改父类成员权限)
  • 运算符重载分类错误(不可重载, 只可重载为...)
  • 成员指针与普通指针(静态成员指针)的混用错误
  • 单参数构造函数与强制类型转换重载函数在表达式中的隐式调用错误
  • 指针不可指向引用等引用类型的特性错误
    • 引用变量不分配存储单元: 不能引用 引用变量, 不能作为指针所指对象, 不能作为数组元素
    • 引用变量的值按字节(指针/地址)编码: 不能引用位段(无法按字节编码)
    • 引用变量不能引用引用变量, 只能引用另一个引用变量所引用的变量/地址
    • 引用变量可以引用 register 变量, 编译器会将其自动转为 auto 变量(为其分配地址)

Inherits Output

  • 每一行输出都为新的派生树(拥有独立的虚基类)
  • 每个对象成员都为新的派生树(拥有独立的虚基类)
',256);e.render=function(a,p,e,o,l,c){return n(),s("div",null,[t])};export default e;export{p as __pageData}; diff --git a/assets/language_cpp_cppBasicNotes.md.212edeb8.lean.js b/assets/language_cpp_cppBasicNotes.md.212edeb8.lean.js new file mode 100644 index 00000000000..d4fa5862fa5 --- /dev/null +++ b/assets/language_cpp_cppBasicNotes.md.212edeb8.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"C++ Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Workflow","slug":"basic-workflow"},{"level":3,"title":"初始化与回收","slug":"初始化与回收"},{"level":2,"title":"变量","slug":"变量"},{"level":3,"title":"指针","slug":"指针"},{"level":3,"title":"reference (引用类型)","slug":"reference-引用类型"},{"level":3,"title":"volatile 类型","slug":"volatile-类型"},{"level":2,"title":"运算符","slug":"运算符"},{"level":3,"title":"左/右值运算符(运算结果为左/右值)","slug":"左-右值运算符-运算结果为左-右值"},{"level":3,"title":"sizeof","slug":"sizeof"},{"level":2,"title":"Expression (表达式)","slug":"expression-表达式"},{"level":3,"title":"左/右值表达式","slug":"左-右值表达式"},{"level":2,"title":"联合(union)","slug":"联合-union"},{"level":3,"title":"位段","slug":"位段"},{"level":2,"title":"函数","slug":"函数"},{"level":3,"title":"Default Params","slug":"default-params"},{"level":3,"title":"内联函数(inline)","slug":"内联函数-inline"},{"level":2,"title":"类","slug":"类"},{"level":3,"title":"访问控制权限","slug":"访问控制权限"},{"level":3,"title":"构造函数","slug":"构造函数"},{"level":3,"title":"析构函数","slug":"析构函数"},{"level":3,"title":"构造与析构(重点)(P148)","slug":"构造与析构-重点-p148"},{"level":3,"title":"New and Delete","slug":"new-and-delete"},{"level":3,"title":"This Pointer","slug":"this-pointer"},{"level":3,"title":"成员指针 与 指针成员","slug":"成员指针-与-指针成员"},{"level":3,"title":"函数成员","slug":"函数成员"},{"level":3,"title":"静态成员","slug":"静态成员"},{"level":3,"title":"友元","slug":"友元"},{"level":3,"title":"单继承","slug":"单继承"},{"level":3,"title":"虚函数","slug":"虚函数"},{"level":2,"title":"作用域(Scope)","slug":"作用域-scope"},{"level":3,"title":"面向过程的作用域","slug":"面向过程的作用域"},{"level":3,"title":"面向对象的作用域","slug":"面向对象的作用域"},{"level":2,"title":"运算符重载","slug":"运算符重载"},{"level":3,"title":"重载原则","slug":"重载原则"},{"level":3,"title":"自增/减运算符","slug":"自增-减运算符"},{"level":3,"title":"成员运算符","slug":"成员运算符"},{"level":3,"title":"赋值运算符","slug":"赋值运算符"},{"level":3,"title":"强制类型转换","slug":"强制类型转换"},{"level":2,"title":"模板","slug":"模板"},{"level":3,"title":"模板调用形式","slug":"模板调用形式"},{"level":2,"title":"异常","slug":"异常"},{"level":3,"title":"异常对象的析构","slug":"异常对象的析构"},{"level":2,"title":"多态","slug":"多态"},{"level":2,"title":"STL","slug":"stl"},{"level":3,"title":"String","slug":"string"},{"level":3,"title":"Input Output","slug":"input-output"},{"level":3,"title":"Container","slug":"container"},{"level":3,"title":"Heap Algorithm","slug":"heap-algorithm"},{"level":3,"title":"Sorting Algorithms","slug":"sorting-algorithms"},{"level":3,"title":"Partitioning Algorithms","slug":"partitioning-algorithms"},{"level":3,"title":"Permutation Algorithms","slug":"permutation-algorithms"},{"level":3,"title":"Numeric Algorithms","slug":"numeric-algorithms"},{"level":3,"title":"Query Algorithms","slug":"query-algorithms"},{"level":3,"title":"Search Algorithms","slug":"search-algorithms"},{"level":3,"title":"Set Algorithms","slug":"set-algorithms"},{"level":3,"title":"Mover Algorithms","slug":"mover-algorithms"},{"level":3,"title":"Value Algorithms","slug":"value-algorithms"},{"level":3,"title":"Functional Algorithms","slug":"functional-algorithms"},{"level":2,"title":"Awesome Tips and Best Practice","slug":"awesome-tips-and-best-practice"},{"level":3,"title":"静态成员 BP","slug":"静态成员-bp"},{"level":3,"title":"友元函数","slug":"友元函数"},{"level":3,"title":"单继承 BP","slug":"单继承-bp"},{"level":3,"title":"虚函数 BP","slug":"虚函数-bp"},{"level":3,"title":"Error BP","slug":"error-bp"},{"level":3,"title":"Inherits Output","slug":"inherits-output"}],"relativePath":"language/cpp/cppBasicNotes.md","lastUpdated":1627196319000}',e={},t=a('',256);e.render=function(a,p,e,o,l,c){return n(),s("div",null,[t])};export default e;export{p as __pageData}; diff --git a/assets/language_go_goBasicNotes.md.38086837.js b/assets/language_go_goBasicNotes.md.38086837.js new file mode 100644 index 00000000000..610fa8e8202 --- /dev/null +++ b/assets/language_go_goBasicNotes.md.38086837.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"Go Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"CLI","slug":"cli"},{"level":3,"title":"Installation","slug":"installation"},{"level":3,"title":"Basic Command","slug":"basic-command"},{"level":2,"title":"Packages","slug":"packages"},{"level":3,"title":"package and import","slug":"package-and-import"},{"level":2,"title":"Variable","slug":"variable"},{"level":3,"title":"Type Declaration","slug":"type-declaration"},{"level":3,"title":"Type conversions","slug":"type-conversions"},{"level":3,"title":"struct","slug":"struct"},{"level":3,"title":"array","slug":"array"},{"level":3,"title":"slice","slug":"slice"},{"level":3,"title":"map","slug":"map"},{"level":2,"title":"Flow Control","slug":"flow-control"},{"level":3,"title":"if","slug":"if"},{"level":3,"title":"for","slug":"for"},{"level":3,"title":"switch","slug":"switch"},{"level":3,"title":"defer","slug":"defer"},{"level":2,"title":"Function","slug":"function"},{"level":3,"title":"Parameters and Return Value","slug":"parameters-and-return-value"},{"level":3,"title":"Methods","slug":"methods"},{"level":3,"title":"Interface","slug":"interface"},{"level":2,"title":"Concurrent","slug":"concurrent"},{"level":3,"title":"goroutine","slug":"goroutine"},{"level":3,"title":"channels","slug":"channels"}],"relativePath":"language/go/goBasicNotes.md","lastUpdated":1627196319000}',t={},e=a('

Go Basic Notes

CLI

Installation

sudo apt install golang\necho "export GOPATH=$HOME/gopath"\necho "export PATH=$PATH:$GOPATH/bin"\ngo env\n
1
2
3
4

Basic Command

go version\ngo run main.go\ngo fmt /path/to/test\n
1
2
3
  • go 的大部分工具的作用基本单位为 package(directories)

Build

# generate library\ngo build path/to/libpack\ngo install path/to/libpack\n\n# generate binary\ngo install path/to/mainpack\n
1
2
3
4
5
6

Test

# path/to/pack/demo.go\n# path/to/pack/demo_test.go\ngo test path/to/pack\n
1
2
3

Clean

go clean -i path/to/pack\n
1

Modules

  • remote packages
  • $GOPATH/bin/hello
go get github.com/golang/example/hello\n
1

Packages

package and import

  • for path/to/pack:
package pack\n
1
import (\n    "path/to/pack"\n)\n
1
2
3
  • 只有首字母大写的函数才可被成功导出, 首字母小写的函数为文件私有函数

Variable

Type Declaration

  • Go 将类型置于变量名后的理由: reads clearly, from left to right
  • := 不可用在函数外
// 简写类型/赋值\nvar i,j int = 1, 2\n\n// 省略类型\nvar c, python, java = true, false, "no!"\n\n// 省略 var 关键字\njavascript, ruby, cpp:= true, false, "no!"\n\n// 声明块\nimport (\n    "math/cmplx"\n)\n\nvar (\n    ToBe   bool       = false\n    MaxInt uint64     = 1<<64 - 1\n    z      complex128 = cmplx.Sqrt(-5 + 12i)\n)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Type conversions

var x int = 3\nvar y uint = uint(x)\nz := uint(x)\n
1
2
3

struct

type Vertex struct {\n    X int\n    Y int\n}\n\nvar (\n    v1 = Vertex{1, 2}\n    v2 = Vertex{X: 1}   // Y: 0\n    v3 = Vertex{}       // X: 0, Y: 0\n    vp = &Vertex{1, 2}  // *Vertex\n)\n
1
2
3
4
5
6
7
8
9
10
11

array

数组的长度是其类型的一部分

var a [2]string\na[0] = "Hello"\na[1] = "Golang"\n\nfmt.Println(a[0], a[1])\nfmt.Println(a)\n
1
2
3
4
5
6

slice

  • s[lo:lo] == nil
p := []int{2, 3, 5, 7, 11, 13}\n\nfmt.Println("p[1:4] ==", p[1:4])\nfmt.Println("p[:3] ==", p[:3])  // p[0:3]        => 0, 1, 2\nfmt.Println("p[4:]" ==, p[4:])  // p[4:len(p)-1] => 4, ..., len(p)-2\n
1
2
3
4
5
  • make 函数创建 slice
a := make([]int, 5)     // len(a) = 5\nb := make([]int, 0, 5)  // len(b) = 0, cap(b) = 5\nb = b[:cap(5)]          // len(b) = 5, cap(b) = 5\n
1
2
3
  • len && cap
// just shorten/extend, not drop elements\n// change len(p), keep cap(p)\np = p [:0]\np = p[:4]\n\n// drop its elements\n// change len(p) and cap(p)\np = p[2:]\n
1
2
3
4
5
6
7
8
  • append
append(s, 2, 3, 4)\n
1
  • range(iterator): 返回 2 个值(index int, element copy(s[index]) T), 在每一次迭代 index+=1
pow := []int{1, 2, 4, 8, 16, 32, 64, 128}\n\nfunc main() {\n    for i := range pow {\n        fmt.Printf("index == %d\\n", i)\n    }\n\n    for _, v := range pow {\n        fmt.Printf("value == %d\\n", v)\n    }\n\n    for i, v := range pow {\n        fmt.Printf("2**%d = %d\\n", i, v)\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

map

type Vertex struct {\n    Lat, Long float64\n}\n\nvar m map[string]Vertex = make(map[string]Vertex)\nm["Bell Labs"] = Vertex{\n    40.68433, -74.39967\n}\n\nml := map[string]Vertex{\n    "Bell Labs": Vertex{\n        40.68433, -74.39967,\n    },\n    "Google": {37.42202, -122.08408},\n}\n\ndelete(m, "Bell Labs")\n\nelement, ok_flag := m["Google"]\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Flow Control

if

if x < 0 {\n    return true\n}\n\n// scope of v: only in if/else statement\nif v := math.Pow(x, n); v < lim {\n    return v\n} else {\n    fmt.Printf("%g >= %g\\n", v, lim)\n}\n
1
2
3
4
5
6
7
8
9
10

for

for sum < 1000 {\n    sum += sum\n}\n\n// scope of i: only in for statement\nfor i := 0; i < 10; i++ {\n    sum += i\n}\n
1
2
3
4
5
6
7
8

switch

  • switch 中的 case 自动 break(除非使用 fallthrough 语句)
switch time.Saturday {\n    case today+0:\n        fmt.Println("Today.")\n    case today+1:\n        fmt.Println("Tomorrow.")\n    case today+2:\n        fmt.Println("In two days.")\n    default:\n        fmt.Println("Too far away.")\n}\n\n// scope of os: only in switch statement\nswitch os := runtime.GOOS; os {\n    case "darwin":\n        fmt.Println("OS X.")\n    case "linux":\n        fmt.Println("Linux.")\n    default:\n        fmt.Printf("%s", os)\n}\n\n// alias for if-else long chain\nswitch {    // switch true\n    case t.Hour() < 12:\n        fmt.Println("Good morning!")\n    case t.Hour() < 17:\n        fmt.Println("Good afternoon!")\n    default:\n        fmt.Println("Good evening!")\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

defer

defer 语句会将函数执行延迟至上层函数返回处(函数参数会立刻生成):

执行时机

函数设置返回值后, 即将返回调用函数前(若 defer 函数修改返回变量, 则会造成返回值与预期不一致)

func main() {\n    defer fmt.Println("!")\n    defer fmt.Println("world")\n    fmt.Println("hello")\n}\n\n=>\n\nfunc main() {\n    fmt.Println("hello")\n    fmt.Println("world")\n    fmt.Println("!")\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13

实质

return_value = xxx -> invoke defer functions(stack) -> return void

func f() (result int) {\n    defer func() {\n        result++\n    }()\n\n    return 0\n}\n\n=>\n\nfunc f() (result int) {\n    result = 0\n\n    func() {\n        result++\n    }()\n\n    return\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

应用场景

  • 资源回收
mu.Lock()\ndefer mu.Unlock()\n
1
2
  • panic 异常的捕获
func f() {\n    defer func() {\n        if r:= recover(); r!= nil {\n            fmt.Println("Recovered in f", r)\n        }\n    }()\n\n    fmt.Println("Calling g.")\n    g()\n    fmt.Println("Returned normally from g.")\n}\n\nfunc g() {\n    panic("ERROR")\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • 保证语句(在发生异常的情况下)始终会被执行
  • 有意修改返回值

Function

Parameters and Return Value

  • 简写参数类型
  • 多值返回函数
  • 命名返回值(注释文档)
func swap(x, y string) (string, string) {\n    return y, x\n}\n\nfunc swap_(x, y string) (x_, y_ string) {\n    x_, y_ = y, x\n    return\n}\n\nfunc main() {\n    a, b := swap("hello", "golang")\n    a_, b_ := swap_("hello", "golang")\n    fmt.Println(a, b)\n    fmt.Println(a_, b_)\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Methods

  • Go 中没有 class, 但可以在 struct/同一个包内的 type 上(receiver)定义方法
type Vertex struct {\n    X, Y float64\n}\n\nfunc (v *Vertex) Abs() float64 {\n    return math.Sqrt(v.X*v.X + v.Y*v.Y)\n}\n\nfunc main() {\n    v := &Vertex{3, 4}\n    fmt.Println(v.Abs())\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
type MyFloat float64\n\nfunc (f MyFloat) Abs() float64 {\n    if f < 0 {\n        return float64(-f)\n    } else {\n        return float64(f)\n    }\n}\n\nfunc main() {\n    f := MyFloat(-math.Sqrt2)\n    fmt.Println(f.Abs())\n
1
2
3
4
5
6
7
8
9
10
11
12
13

Receiver

  • pointer receiver: 可以改变原值(call by reference)
  • value receive: 不可以改变原值(call by value)
  • 调用 methods 时, 可以不考虑 v 是 value/pointer, go 会自动处理
func (v *Vertex) changeV() {\n    v.X += 1\n    v.Y += 1\n}\n\nv.changeV() => (&v).changeV()\n
1
2
3
4
5
6
func (v Vertex) Abs() {\n    return abs(v)\n}\n\n(&v).Abs() => v.Abs()\n
1
2
3
4
5
  • Best Practice: 在同一个类型上定义的所有方法最好统一 receiver 类型(全部 value receivers 或 全部 pointer receivers)

Interface

(value, type)

var i I\nvar t *T\ni = t   // => (nil, *T)\n
1
2
3
var i I     // => (nil, nil)\n
1

Type Assertions

  • 单返回值: 断言失败时产生 panic
  • 双返回值: 断言失败时不产生 panic
// create empty interface, ("hello", string)\nvar i interface{} = "hello"\n\ns := i.(string)\ns, ok := i.(string) // => true\nf, ok := i.(float64)// => false(no panic)\nf := i.(float64)    // => false with panic\n
1
2
3
4
5
6
7
  • type switches
switch v := i.(type) {\n    case int;\n        fmt.Println("Int.")\n    case string:\n        fmt.Println("String.")\n    default:\n        fmt.Printf("Other type.")\n}\n
1
2
3
4
5
6
7
8

Concurrent

goroutine

go f(x, y, z)   // => execute in a new goroutine with share memory\n
1

channels

  • typed conduit(类型管道)
  • block execution
var c chan int = make(chan int)\n\nc <- sum    // send sum to channel c\nv := <-c    // receive from channel c, assign value to v\n
1
2
3
4
func sum(s []int, c chan int) {\n    sum := 0\n\n    for _, v := range s {\n        sum += v\n    }\n\n    c <- sum\n}\n\nfunc main() {\n    s := []int{7, 2, 8, -9, 4, 0}\n\n    c := make(chan int)\n\n    go sum(s[:len(s)/2], c)\n    go sum(s[len(s/2):], c)\n\n    x, y = <-c, <-c\n\n    fmt.Println(x, y, x+y)\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

select

  • select(当所有情况都不满足时)可被阻塞
for {\n    select {\n        case c <- x:\n            x, y = y, x+y\n        case <- quit:\n            fmt.Println("quit")\n            return\n    }\n}\n
1
2
3
4
5
6
7
8
9

Worker Pools

package main\n\nimport "fmt"\nimport "time"\n\nfunc worker(id int, jobs <-chan int, results chan<- int) {\n    for j := range jobs {\n        fmt.Println("worker", id, "processing job", j)\n        time.Sleep(time.Second)\n        results <- j * 2\n    }\n}\n\nfunc main() {\n    jobs := make(chan int, 100)\n    results := make(chan int, 100)\n\n    for w := 1; w <= 3; w++ {\n        go worker(w, jobs, results)\n    }\n\n    for j := 1; j <= 9; j++ {\n        jobs <- j\n    }\n\n    close(jobs)\n\n    for a := 1; a <= 9; a++ {\n        <-results\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
',104);t.render=function(a,p,t,o,c,l){return n(),s("div",null,[e])};export default t;export{p as __pageData}; diff --git a/assets/language_go_goBasicNotes.md.38086837.lean.js b/assets/language_go_goBasicNotes.md.38086837.lean.js new file mode 100644 index 00000000000..04a2dc163bb --- /dev/null +++ b/assets/language_go_goBasicNotes.md.38086837.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"Go Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"CLI","slug":"cli"},{"level":3,"title":"Installation","slug":"installation"},{"level":3,"title":"Basic Command","slug":"basic-command"},{"level":2,"title":"Packages","slug":"packages"},{"level":3,"title":"package and import","slug":"package-and-import"},{"level":2,"title":"Variable","slug":"variable"},{"level":3,"title":"Type Declaration","slug":"type-declaration"},{"level":3,"title":"Type conversions","slug":"type-conversions"},{"level":3,"title":"struct","slug":"struct"},{"level":3,"title":"array","slug":"array"},{"level":3,"title":"slice","slug":"slice"},{"level":3,"title":"map","slug":"map"},{"level":2,"title":"Flow Control","slug":"flow-control"},{"level":3,"title":"if","slug":"if"},{"level":3,"title":"for","slug":"for"},{"level":3,"title":"switch","slug":"switch"},{"level":3,"title":"defer","slug":"defer"},{"level":2,"title":"Function","slug":"function"},{"level":3,"title":"Parameters and Return Value","slug":"parameters-and-return-value"},{"level":3,"title":"Methods","slug":"methods"},{"level":3,"title":"Interface","slug":"interface"},{"level":2,"title":"Concurrent","slug":"concurrent"},{"level":3,"title":"goroutine","slug":"goroutine"},{"level":3,"title":"channels","slug":"channels"}],"relativePath":"language/go/goBasicNotes.md","lastUpdated":1627196319000}',t={},e=a('',104);t.render=function(a,p,t,o,c,l){return n(),s("div",null,[e])};export default t;export{p as __pageData}; diff --git a/assets/language_haskell_haskellBasicNotes.md.998a4d73.js b/assets/language_haskell_haskellBasicNotes.md.998a4d73.js new file mode 100644 index 00000000000..a8dc6b533e6 --- /dev/null +++ b/assets/language_haskell_haskellBasicNotes.md.998a4d73.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as e}from"./app.6dd2a1b7.js";const t='{"title":"Haskell Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Platform","slug":"platform"},{"level":3,"title":"ghci","slug":"ghci"},{"level":3,"title":"ghc","slug":"ghc"},{"level":2,"title":"Unique Mark","slug":"unique-mark"},{"level":2,"title":"Expression","slug":"expression"},{"level":2,"title":"Type","slug":"type"},{"level":3,"title":"基本类型","slug":"基本类型"},{"level":3,"title":"List","slug":"list"},{"level":3,"title":"Tuple(原组)","slug":"tuple-原组"},{"level":3,"title":"泛型","slug":"泛型"},{"level":3,"title":"函数类型","slug":"函数类型"},{"level":2,"title":"基本语法","slug":"基本语法"},{"level":3,"title":"名字/函数定义","slug":"名字-函数定义"},{"level":2,"title":"Type Class","slug":"type-class"},{"level":3,"title":"=> 符号","slug":"符号"},{"level":3,"title":"Basic Type Class","slug":"basic-type-class"},{"level":3,"title":"Custom Type Class","slug":"custom-type-class"},{"level":3,"title":"data type","slug":"data-type"},{"level":2,"title":"函数","slug":"函数"},{"level":3,"title":"递归函数","slug":"递归函数"},{"level":3,"title":"高阶函数","slug":"高阶函数"},{"level":3,"title":"常用函数","slug":"常用函数"},{"level":2,"title":"模块","slug":"模块"},{"level":3,"title":"import","slug":"import"},{"level":3,"title":"建立模块","slug":"建立模块"},{"level":3,"title":"常用基础模块","slug":"常用基础模块"},{"level":2,"title":"输入与输出","slug":"输入与输出"},{"level":3,"title":"IO action","slug":"io-action"},{"level":3,"title":"Command Line","slug":"command-line"},{"level":3,"title":"ByteString","slug":"bytestring"},{"level":3,"title":"常用输入输出函数","slug":"常用输入输出函数"},{"level":2,"title":"异常","slug":"异常"},{"level":3,"title":"catch","slug":"catch"},{"level":2,"title":"Advanced Monad","slug":"advanced-monad"},{"level":3,"title":"Writer Monad","slug":"writer-monad"},{"level":3,"title":"Reader Monad","slug":"reader-monad"},{"level":3,"title":"State Monad","slug":"state-monad"},{"level":3,"title":"Error Monad","slug":"error-monad"},{"level":3,"title":"Self-Defined Monad","slug":"self-defined-monad"},{"level":2,"title":"注释","slug":"注释"},{"level":3,"title":"符号","slug":"符号-1"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"language/haskell/haskellBasicNotes.md","lastUpdated":1627196319000}',l={},o=a("h1",{id:"haskell-basic-notes"},[a("a",{class:"header-anchor",href:"#haskell-basic-notes","aria-hidden":"true"},"#"),e(" Haskell Basic Notes")],-1),p=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#haskell-basic-notes"},"Haskell Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#platform"},"Platform")]),a("li",null,[a("a",{href:"#unique-mark"},"Unique Mark")]),a("li",null,[a("a",{href:"#expression"},"Expression")]),a("li",null,[a("a",{href:"#type"},"Type")]),a("li",null,[a("a",{href:"#基本语法"},"基本语法")]),a("li",null,[a("a",{href:"#type-class"},"Type Class")]),a("li",null,[a("a",{href:"#函数"},"函数")]),a("li",null,[a("a",{href:"#模块"},"模块")]),a("li",null,[a("a",{href:"#输入与输出"},"输入与输出")]),a("li",null,[a("a",{href:"#异常"},"异常")]),a("li",null,[a("a",{href:"#advanced-monad"},"Advanced Monad")]),a("li",null,[a("a",{href:"#注释"},"注释")]),a("li",null,[a("a",{href:"#reference"},"Reference")])])])])])],-1),c=a("h2",{id:"platform"},[a("a",{class:"header-anchor",href:"#platform","aria-hidden":"true"},"#"),e(" Platform")],-1),r=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token function"},"apt-get"),e(),a("span",{class:"token function"},"install"),e(" haskell-Platform\n"),a("span",{class:"token function"},"apt-get"),e(),a("span",{class:"token function"},"install"),e(" ghc-mod\natom plugins "),a("span",{class:"token builtin class-name"},":"),e(" language-haskell autocomplete-haskell ide-haskell haskell-ghc-mod\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),u=a("h3",{id:"ghci"},[a("a",{class:"header-anchor",href:"#ghci","aria-hidden":"true"},"#"),e(" ghci")],-1),i=a("ul",null,[a("li",null,":l - load file"),a("li",null,":r - reload file"),a("li",null,":cd"),a("li",null,":edit - $EDITOR"),a("li",null,":m - module"),a("li",null,":q - quit"),a("li",null,":?"),a("li",null,":k - kind"),a("li",null,":t - type function"),a("li",null,":info - data/TypeClass")],-1),k=a("h3",{id:"ghc"},[a("a",{class:"header-anchor",href:"#ghc","aria-hidden":"true"},"#"),e(" ghc")],-1),b=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,"runghc *.hs/*.lhs\n")]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),m=a("h2",{id:"unique-mark"},[a("a",{class:"header-anchor",href:"#unique-mark","aria-hidden":"true"},"#"),e(" Unique Mark")],-1),h=a("p",null,[a("code",null,":+")],-1),d=a("p",null,"复数符 - 2 :+ 3 -> 2+3i",-1),v=a("p",null,[a("code",null,"_")],-1),g=a("p",null,"泛匹配符: 表示不关心此部分具体内容",-1),y=a("p",null,[a("code",null,"<-")],-1),f=a("p",null,"属于符号,用于 ListRange 中.",-1),w=a("p",null,[a("code",null,"=>")],-1),x=a("p",null,"类型约束分隔符",-1),S=a("p",null,[a("code",null,"->")],-1),L=a("h2",{id:"expression"},[a("a",{class:"header-anchor",href:"#expression","aria-hidden":"true"},"#"),e(" Expression")],-1),M=a("p",null,"if 语句也是表达式",-1),F=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"doubleSmallNumber"),e("' "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),T=a("h2",{id:"type"},[a("a",{class:"header-anchor",href:"#type","aria-hidden":"true"},"#"),e(" Type")],-1),I=a("h3",{id:"基本类型"},[a("a",{class:"header-anchor",href:"#基本类型","aria-hidden":"true"},"#"),e(" 基本类型")],-1),A=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token char string"},"'a'"),e("\n"),a("span",{class:"token char string"},"'a'"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Char"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token constant"},"True"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Bool"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token string"},'"HELLO!"'),e("\n"),a("span",{class:"token string"},'"HELLO!"'),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Bool"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),C=a("h4",{id:"int"},[a("a",{class:"header-anchor",href:"#int","aria-hidden":"true"},"#"),e(" Int")],-1),O=a("p",null,"有限整数",-1),N=a("h4",{id:"integer"},[a("a",{class:"header-anchor",href:"#integer","aria-hidden":"true"},"#"),e(" Integer")],-1),B=a("p",null,"无限整数(效率低)",-1),W=a("h4",{id:"float"},[a("a",{class:"header-anchor",href:"#float","aria-hidden":"true"},"#"),e(" Float")],-1),E=a("p",null,"单精度浮点数",-1),$=a("h4",{id:"double"},[a("a",{class:"header-anchor",href:"#double","aria-hidden":"true"},"#"),e(" Double")],-1),P=a("p",null,"双精度浮点数",-1),q=a("h4",{id:"bool"},[a("a",{class:"header-anchor",href:"#bool","aria-hidden":"true"},"#"),e(" Bool")],-1),R=a("p",null,"True/False",-1),z=a("h4",{id:"char"},[a("a",{class:"header-anchor",href:"#char","aria-hidden":"true"},"#"),e(" Char")],-1),D=a("h4",{id:"string"},[a("a",{class:"header-anchor",href:"#string","aria-hidden":"true"},"#"),e(" String")],-1),J=a("h4",{id:"ordering"},[a("a",{class:"header-anchor",href:"#ordering","aria-hidden":"true"},"#"),e(" Ordering")],-1),G=a("p",null,"LT,GT,EQ",-1),_=a("h4",{id:"word"},[a("a",{class:"header-anchor",href:"#word","aria-hidden":"true"},"#"),e(" Word")],-1),H=a("p",null,"Data.Word - unsigned int",-1),K=a("h4",{id:"rational"},[a("a",{class:"header-anchor",href:"#rational","aria-hidden":"true"},"#"),e(" Rational")],-1),Y=a("p",null,"有理数类型,用于高精度数学运算",-1),V=a("h3",{id:"list"},[a("a",{class:"header-anchor",href:"#list","aria-hidden":"true"},"#"),e(" List")],-1),U=a("h4",{id:"operator"},[a("a",{class:"header-anchor",href:"#operator","aria-hidden":"true"},"#"),e(" Operator")],-1),Z=a("ul",null,[a("li",null,"++"),a("li",null,":"),a("li",null,"!!"),a("li",null,">/"),e(),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),en=a("ul",null,[a("li",null,"reverse List 反转"),a("li",null,"take num List 返回 List 前 num 个元素组成的 List")],-1),tn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),ln=a("ul",null,[a("li",null,"drop num List 删除 List 前 num 个元素")],-1),on=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"drop"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"drop"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"drop"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),pn=a("ul",null,[a("li",null,[a("p",null,"maximum List 返回最大元素")]),a("li",null,[a("p",null,"minimum List 返回最小元素")]),a("li",null,[a("p",null,"sum List 返回 List 元素和")]),a("li",null,[a("p",null,"product List 返回 List 元素积")]),a("li",null,[a("p",null,[e("elem "),a("code",null,"elem"),e(" List 判断元素存在性")])])],-1),cn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"4"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"10"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),rn=a("ul",null,[a("li",null,[e("cycle List 返回循环无限数组("),a("em",null,"Haskell 惰性特性"),e(")")]),a("li",null,[e("repeat Elem 返回循环无限数组("),a("em",null,"Haskell 惰性特性"),e(")")]),a("li",null,"replicate num Elem 返回循环无限数组")],-1),un=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"cycle"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"repeat"),e(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),e(" "),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"replicate"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"10"),e(" "),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),kn=a("ul",null,[a("li",null,[e("takeWhile :: (a -> Bool) -> "),a("code",null,"[a]"),e(" -> "),a("code",null,"[a]"),e(" 遇到不符合限制条件的元素便停止遍历 List")])],-1),bn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token number"},"166650"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),mn=a("h4",{id:"range"},[a("a",{class:"header-anchor",href:"#range","aria-hidden":"true"},"#"),e(" Range")],-1),hn=a("p",null,"三要素: , 与 ..",-1),dn=a("ul",null,[a("li",null,"上限"),a("li",null,"下限"),a("li",null,[e("步长("),a("em",null,"仅可标明一次"),e(")")])],-1),vn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[e("上下限"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},"]"),e("\n步长为"),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},"]"),e("\n步长为"),a("span",{class:"token number"},"13"),e("无限"),a("span",{class:"token constant"},"List"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"13"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"26"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"24"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"13"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"26"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),gn=a("h4",{id:"list-comprehension-数学定义"},[a("a",{class:"header-anchor",href:"#list-comprehension-数学定义","aria-hidden":"true"},"#"),e(" List Comprehension(数学定义)")],-1),yn=a("p",null,"由类似集合定义的离散数学定义,来定义复杂的 List:",-1),fn=a("p",null,[a("code",null,"[expression | filter]")],-1),wn=a("p",null,[a("code",null,"[expression | x <\\- Range, Predicate(断言/限制条件)]")],-1),xn=a("ul",null,[a("li",null,[e("Range: "),a("code",null,","),e("分隔多个 Range(一般为 List)")]),a("li",null,[e("Predicate: "),a("code",null,","),e("分隔多个断言;每个断言均为 Boolean 表达式")])],-1),Sn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},">="),e(),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"14"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"18"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"50"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"x"),e(" `"),a("span",{class:"token builtin"},"mod"),e("` "),a("span",{class:"token number"},"7"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"52"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"59"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"66"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"73"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"80"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"87"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"94"),a("span",{class:"token punctuation"},"]"),e("\n\n\n"),a("span",{class:"token hvariable"},"boomBangs"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token string"},'"BOOM!"'),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token string"},'"BANG!"'),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"boomBangs"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"13"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"BOOM!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"BOOM!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"BANG!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"BANG!"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Ln=a("ul",null,[a("li",null,"多个 Range")],-1),Mn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"40"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"50"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"55"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"80"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"110"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Fn=a("ul",null,[a("li",null,"嵌套 Comprehension")],-1),Tn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"xxs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token builtin"},"even"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xxs"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),In=a("h3",{id:"tuple-原组"},[a("a",{class:"header-anchor",href:"#tuple-原组","aria-hidden":"true"},"#"),e(" Tuple(原组)")],-1),An=a("h4",{id:"内部差异性"},[a("a",{class:"header-anchor",href:"#内部差异性","aria-hidden":"true"},"#"),e(" 内部差异性")],-1),Cn=a("ul",null,[a("li",null,"同一 Tuple 里可存放不同 Type 的项")],-1),On=a("h4",{id:"外部差异性"},[a("a",{class:"header-anchor",href:"#外部差异性","aria-hidden":"true"},"#"),e(" 外部差异性")],-1),Nn=a("ul",null,[a("li",null,[e("数目不同或某项不同的 Tuple 属于不同 Type "),a("ul",null,[a("li",null,"不可置于同一 List 中"),a("li",null,"不同长度的 Tuple 不可比较(比较符只可用于相同 Type)")])])],-1),Bn=a("h4",{id:"tuple-function"},[a("a",{class:"header-anchor",href:"#tuple-function","aria-hidden":"true"},"#"),e(" Tuple Function")],-1),Wn=a("h5",{id:"二元组"},[a("a",{class:"header-anchor",href:"#二元组","aria-hidden":"true"},"#"),e(" 二元组")],-1),En=a("p",null,"fst/snd tuple 返回首项/尾项",-1),$n=a("p",null,"zip List1 List2 对应项配对,组成二元组 List",-1),Pn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zip"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"im"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"a"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"turtle"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"im"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"a"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"turtle"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zip"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"apple"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"orange"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"cherry"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"mango"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"apple"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"orange"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"cherry"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"mango"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),qn=a("h5",{id:"三元组"},[a("a",{class:"header-anchor",href:"#三元组","aria-hidden":"true"},"#"),e(" 三元组")],-1),Rn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"first"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"first"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e("\n\n"),a("span",{class:"token hvariable"},"second"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n"),a("span",{class:"token hvariable"},"second"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"y"),e("\n\n"),a("span",{class:"token hvariable"},"third"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),e("\n"),a("span",{class:"token hvariable"},"third"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"z"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"z"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),zn=a("h3",{id:"泛型"},[a("a",{class:"header-anchor",href:"#泛型","aria-hidden":"true"},"#"),e(" 泛型")],-1),Dn=a("p",null,[e("运用 Type 变量(只可为"),a("em",null,"单字符"),e("), 实现泛型参数与多态函数.")],-1),Jn=a("p",null,"借助 TypeClass 可轻松实现多态函数:",-1),Gn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token builtin"},"head"),e("\n"),a("span",{class:"token builtin"},"head"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n\n"),a("span",{class:"token comment"},"-- a 和 b 可为同类型"),e("\n"),a("span",{class:"token comment"},"-- 第一个参数与返回值必须同类型"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token builtin"},"fst"),e("\n"),a("span",{class:"token builtin"},"fst"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n\n"),a("span",{class:"token comment"},"-- 所有参数必须同类型,且必须为Num成员"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),_n=a("h3",{id:"函数类型"},[a("a",{class:"header-anchor",href:"#函数类型","aria-hidden":"true"},"#"),e(" 函数类型")],-1),Hn=a("ul",null,[a("li",null,"单个参数")],-1),Kn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"removeNonUppercase"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"removeNonUppercase"),e(),a("span",{class:"token hvariable"},"st"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"st"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"c"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Yn=a("ul",null,[a("li",null,"多个参数")],-1),Vn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"addThree"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"addThree"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token hvariable"},"z"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"z"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Un=a("h2",{id:"基本语法"},[a("a",{class:"header-anchor",href:"#基本语法","aria-hidden":"true"},"#"),e(" 基本语法")],-1),Zn=a("h3",{id:"名字-函数定义"},[a("a",{class:"header-anchor",href:"#名字-函数定义","aria-hidden":"true"},"#"),e(" 名字/函数定义")],-1),jn=a("h4",{id:"模式匹配-pattern-matching"},[a("a",{class:"header-anchor",href:"#模式匹配-pattern-matching","aria-hidden":"true"},"#"),e(" 模式匹配(Pattern Matching)")],-1),Qn=a("p",null,"当函数拥有多个函数体(模式)时,会从上至下进行匹配各模式,一旦匹配则只应用这一函数体.",-1),Xn=a("h5",{id:"best-practice"},[a("a",{class:"header-anchor",href:"#best-practice","aria-hidden":"true"},"#"),e(" Best Practice")],-1),ns=a("ul",null,[a("li",null,"代替 if-else/switch 语句"),a("li",null,"递归算法(将递归基础作为首模式,递归函数体作为尾模式)"),a("li",null,"List Range 中亦可使用模式匹配")],-1),ss=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"addVectors"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"addVectors"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x1"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"x2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y1"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"y2"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),as=a("h5",{id:"常用模式"},[a("a",{class:"header-anchor",href:"#常用模式","aria-hidden":"true"},"#"),e(" 常用模式")],-1),es=a("h6",{id:"as-模式"},[a("a",{class:"header-anchor",href:"#as-模式","aria-hidden":"true"},"#"),e(" as 模式")],-1),ts=a("p",null,"all@(pattern) - all 为指向 pattern 整体的引用",-1),ls=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"all"),a("span",{class:"token operator"},"@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},"-- 其中all与(x:y:xs)等价"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),os=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"capital"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"capital"),e(),a("span",{class:"token string"},'""'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Empty string, whoops!"'),e("\n"),a("span",{class:"token hvariable"},"capital"),e(),a("span",{class:"token builtin"},"all"),a("span",{class:"token operator"},"@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"The first letter of "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" is "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ps=a("h6",{id:"list-模式"},[a("a",{class:"header-anchor",href:"#list-模式","aria-hidden":"true"},"#"),e(" List 模式")],-1),cs=a("ul",null,[a("li",null,"x:xs"),a("li",null,"x:y:z:xs")],-1),rs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"error"),e(),a("span",{class:"token string"},'"Can\'t call head on an empty list, dummy!"'),e("\n"),a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),us=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"length"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n"),a("span",{class:"token builtin"},"length"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),e("\n"),a("span",{class:"token builtin"},"length"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"_"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token builtin"},"length"),e("' "),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),is=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),e("\n"),a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ks=a("h6",{id:"tuple"},[a("a",{class:"header-anchor",href:"#tuple","aria-hidden":"true"},"#"),e(" Tuple")],-1),bs=a("ul",null,[a("li",null,"(x, y)"),a("li",null,"(x, y, z)")],-1),ms=a("h4",{id:"guard-模式-与-where-绑定"},[a("a",{class:"header-anchor",href:"#guard-模式-与-where-绑定","aria-hidden":"true"},"#"),e(" guard 模式 与 where 绑定")],-1),hs=a("p",null,"子模式匹配: 运用布尔表达式实现判断,应用对应函数体",-1),ds=a("ul",null,[a("li",null,"关键符号: | 与 where"),a("li",null,"| 分隔函数体"),a("li",null,[e("where "),a("ul",null,[a("li",null,"可见性: 定义只对本模式可见的(私有)名字与(私有)函数"),a("li",null,"where 定义在最外层,使得各模式共享(私有)名字与(私有)函数"),a("li",null,[e("名字定义时可使用模式匹配 "),a("code",null,"where (head:_) = firstName")])])])],-1),vs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"bmiTell"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"RealFloat"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"bmiTell"),e(),a("span",{class:"token hvariable"},"weight"),e(),a("span",{class:"token hvariable"},"height"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"bmi"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token hvariable"},"skinny"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"You\'re underweight, you emo, you!"'),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"bmi"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token hvariable"},"normal"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},"\"You're supposedly normal. Pet, I bet you're ugly!\""),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"bmi"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token hvariable"},"fat"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"You\'re fat! Lose some weight, fatty!"'),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"You\'re a whale, congratulations!"'),e("\n "),a("span",{class:"token keyword"},"where"),e(),a("span",{class:"token hvariable"},"bmi"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"weight"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token hvariable"},"height"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"2"),e("\n "),a("span",{class:"token hvariable"},"skinny"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"18.5"),e("\n "),a("span",{class:"token hvariable"},"normal"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"25.0"),e("\n "),a("span",{class:"token hvariable"},"fat"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"30.0"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),gs=a("h4",{id:"let-绑定"},[a("a",{class:"header-anchor",href:"#let-绑定","aria-hidden":"true"},"#"),e(" let 绑定")],-1),ys=a("p",null,"类似 where,绑定对象为表达式/函数",-1),fs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"bindings"),e("\n"),a("span",{class:"token keyword"},"in"),e(" "),a("span",{class:"token hvariable"},"expressions"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ws=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"sideArea"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"r"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"h"),e("\n "),a("span",{class:"token hvariable"},"topArea"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"r"),e(),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token keyword"},"in"),e(" "),a("span",{class:"token hvariable"},"sideArea"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"topArea"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),xs=a("ul",null,[a("li",null,"可见性:in 作用域,只对本 guard 可见"),a("li",null,"可使用模式匹配"),a("li",null,"可用于 List Range 中")],-1),Ss=a("h4",{id:"case-表达式"},[a("a",{class:"header-anchor",href:"#case-表达式","aria-hidden":"true"},"#"),e(" case 表达式")],-1),Ls=a("ul",null,[a("li",null,"模式匹配是 case 表达式的特殊情况(语法糖:简化写法)"),a("li",null,"在函数中,模式匹配只能用于参数定义中,而 case 表达式可用于其他地方(let/where 绑定 普通表达式 guard 语句)")],-1),Ms=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token hvariable"},"expression"),e(),a("span",{class:"token keyword"},"of"),e(),a("span",{class:"token hvariable"},"pattern"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"result"),e("\n "),a("span",{class:"token hvariable"},"pattern"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"result"),e("\n "),a("span",{class:"token hvariable"},"pattern"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"result"),e("\n "),a("span",{class:"token operator"},"..."),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Fs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"describeList"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"describeList"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"The list is "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token keyword"},"of"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token string"},'"empty."'),e("\n "),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token string"},'"a singleton list."'),e("\n "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token string"},'"a longer list."'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ts=a("h2",{id:"type-class"},[a("a",{class:"header-anchor",href:"#type-class","aria-hidden":"true"},"#"),e(" Type Class")],-1),Is=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token builtin"},"fromIntegral"),e("\n"),a("span",{class:"token builtin"},"fromIntegral"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Integral"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),As=a("h3",{id:"符号"},[a("a",{class:"header-anchor",href:"#符号","aria-hidden":"true"},"#"),e(" => 符号")],-1),Cs=a("p",null,"=> 左部: 类约束(Class Constraint) => 右部: 函数类型(参数/返回值类型),其中参数类型同属 Class",-1),Os=a("h3",{id:"basic-type-class"},[a("a",{class:"header-anchor",href:"#basic-type-class","aria-hidden":"true"},"#"),e(" Basic Type Class")],-1),Ns=a("blockquote",null,[a("p",null,"ghci> :info typeClassName")],-1),Bs=a("h4",{id:"eq"},[a("a",{class:"header-anchor",href:"#eq","aria-hidden":"true"},"#"),e(" Eq")],-1),Ws=a("ul",null,[a("li",null,"功能: 成员类型可判断相等性"),a("li",null,"成员: 大部分基本类型(不包含函数类型)"),a("li",null,"方法: == 与 /= 函数")],-1),Es=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"not"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"/="),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"/="),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"not"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),$s=a("h4",{id:"ord"},[a("a",{class:"header-anchor",href:"#ord","aria-hidden":"true"},"#"),e(" Ord")],-1),Ps=a("p",null,"Ord 成员必为 Eq 成员: class (Eq a) => Ord a where",-1),qs=a("ul",null,[a("li",null,"功能: 成员类型可排序"),a("li",null,"成员: 大部分基本类型(不包含函数类型)"),a("li",null,[e("方法: "),a("ul",null,[a("li",null,"< > <= >= 函数"),a("li",null,"compare 函数 (Ord a) => a -> a -> Ordering")])])],-1),Rs=a("h4",{id:"show"},[a("a",{class:"header-anchor",href:"#show","aria-hidden":"true"},"#"),e(" Show")],-1),zs=a("ul",null,[a("li",null,"功能: 成员类型可用字符串表示"),a("li",null,"成员: 大部分基本类型(不包含函数类型)"),a("li",null,"方法: show 函数 (Show a) => a -> String")],-1),Ds=a("p",null,[a("em",null,"Tips"),e(": 结合 Read, 可用于字符串与数值之间的转化")],-1),Js=a("h4",{id:"read"},[a("a",{class:"header-anchor",href:"#read","aria-hidden":"true"},"#"),e(" Read")],-1),Gs=a("ul",null,[a("li",null,"功能: 可以将字串转为 Read 某成员类型"),a("li",null,"成员: 大部分基本类型(不包含函数类型)"),a("li",null,"方法: read 函数 (Read a) => String -> a")],-1),_s=a("p",null,[a("em",null,"Tips"),e(": 结合 Show, 可用于字符串与数值之间的转化")],-1),Hs=a("h4",{id:"enum"},[a("a",{class:"header-anchor",href:"#enum","aria-hidden":"true"},"#"),e(" Enum")],-1),Ks=a("ul",null,[a("li",null,[e("功能: 连续性(可枚举), 其成员类型可用于"),a("em",null,"Range"),e("中")]),a("li",null,"成员: () Bool Char Ordering Int Integer Float Double")],-1),Ys=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Thursday"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token constant"},"Sunday"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Vs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"succ"),e(),a("span",{class:"token constant"},"Monday"),e("\n"),a("span",{class:"token constant"},"Tuesday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"pred"),e(),a("span",{class:"token constant"},"Saturday"),e("\n"),a("span",{class:"token constant"},"Friday"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Us=a("h4",{id:"bounded"},[a("a",{class:"header-anchor",href:"#bounded","aria-hidden":"true"},"#"),e(" Bounded")],-1),Zs=a("ul",null,[a("li",null,"功能: 成员类型具有上下限"),a("li",null,[e("方法: minBound/maxBound 函数 (Bounded a) => a "),a("em",null,"无参多态常量/定义")])],-1),js=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"minBound"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Monday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"maxBound"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Sunday"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Qs=a("h4",{id:"num"},[a("a",{class:"header-anchor",href:"#num","aria-hidden":"true"},"#"),e(" Num")],-1),Xs=a("ul",null,[a("li",null,"功能: 成员类型具有数字特征"),a("li",null,"成员: 实数 整数 - Int Integer Float Double"),a("li",null,"方法: + - * abs 函数"),a("li",null,"实例: 所有数字都是多态常量/定义(可视为函数)")],-1),na=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token number"},"20"),e("\n"),a("span",{class:"token number"},"20"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"t"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"t"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),sa=a("h4",{id:"integral"},[a("a",{class:"header-anchor",href:"#integral","aria-hidden":"true"},"#"),e(" Integral")],-1),aa=a("ul",null,[a("li",null,"功能: 成员类型具有数字特征"),a("li",null,"成员: 整型 - Int Integer")],-1),ea=a("h4",{id:"floating"},[a("a",{class:"header-anchor",href:"#floating","aria-hidden":"true"},"#"),e(" Floating")],-1),ta=a("ul",null,[a("li",null,"功能: 成员类型具有数字特征"),a("li",null,"成员: 浮点型 - Float Double")],-1),la=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"left"}},"TypeClass"),a("th",{style:{"text-align":"left"}},"Method Feature")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"left"}},"Functor"),a("td",{style:{"text-align":"left"}},"f a + (a -> b) -> f b")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Applicative"),a("td",{style:{"text-align":"left"}},"f a + f (a -> b) -> f b")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Monad"),a("td",{style:{"text-align":"left"}},"m a + (a -> m b) -> m b")])])],-1),oa=a("h4",{id:"functor"},[a("a",{class:"header-anchor",href:"#functor","aria-hidden":"true"},"#"),e(),a("em",null,"Functor")],-1),pa=a("ul",null,[a("li",null,[e("成员: Maybe a, [], Either a, IO "),a("ul",null,[a("li",null,[e("成员 kind 必须为 "),a("code",null,"* -> *")]),a("li",null,[e("f "),a("em",null,"一元类型构造符(type constructor)")])])]),a("li",null,[e("必须遵守准则: "),a("ul",null,[a("li",null,"fmap id = id"),a("li",null,"fmap (f . g) F = fmap f (fmap g F)")])])],-1),ca=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"info"),e(),a("span",{class:"token constant"},"Functor"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"$"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ra=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"map"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Either"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Right"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Right"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token hvariable"},"x"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"action"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"result"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"action"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"result"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br")])],-1),ua=a("h4",{id:"control-applicative"},[a("a",{class:"header-anchor",href:"#control-applicative","aria-hidden":"true"},"#"),e(" Control Applicative")],-1),ia=a("ul",null,[a("li",null,[e("成员: f :: "),a("code",null,"* -> *"),e(),a("em",null,"一元类型构造符(type constructor)")]),a("li",null,"<*>: 参数为 2 个 functor 实例,其中一个包含一个函数")],-1),ka=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<$>"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e("\n"),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ba=a("ul",null,[a("li",null,"作用: 可以用单一一个函数操作多个 functor")],-1),ma=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<*>"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ha=a("h5",{id:"maybe"},[a("a",{class:"header-anchor",href:"#maybe","aria-hidden":"true"},"#"),e(" Maybe")],-1),da=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e("\n "),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"something"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"something"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),va=a("h5",{id:"collection"},[a("a",{class:"header-anchor",href:"#collection","aria-hidden":"true"},"#"),e(" Collection "),a("code",null,"[]")],-1),ga=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"fs"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"fs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ya=a("h5",{id:"io"},[a("a",{class:"header-anchor",href:"#io","aria-hidden":"true"},"#"),e(" IO")],-1),fa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"return"),e("\n "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"b"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),wa=a("h5",{id:"ziplist"},[a("a",{class:"header-anchor",href:"#ziplist","aria-hidden":"true"},"#"),e(" ZipList")],-1),xa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"repeat"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token hvariable"},"fs"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"zipWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"fs"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Sa=a("h5",{id:"multi-functor"},[a("a",{class:"header-anchor",href:"#multi-functor","aria-hidden":"true"},"#"),e(" Multi Functor")],-1),La=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"8"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Ma=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"40"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"50"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"55"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"80"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"110"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Fa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"myAction"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"myAction"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token builtin"},"getLine"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token builtin"},"getLine"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ta=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getZipList"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ia=a("h5",{id:"高度封装函数"},[a("a",{class:"header-anchor",href:"#高度封装函数","aria-hidden":"true"},"#"),e(" 高度封装函数")],-1),Aa=a("p",null,[a("code",null,"liftA2"),e(", 对两个 applicative 运用二元函数:")],-1),Ca=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"liftA2"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"c"),e("\n"),a("span",{class:"token hvariable"},"liftA2"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"b"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"liftA2"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},":"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},":"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Oa=a("h4",{id:"control-monad"},[a("a",{class:"header-anchor",href:"#control-monad","aria-hidden":"true"},"#"),e(" Control Monad")],-1),Na=a("ul",null,[a("li",null,"成员: 类型构造符(type constructor)")],-1),Ba=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"a"),e("\n\n"),a("span",{class:"token comment"},"{- bind -}"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">>="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),e("\n\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">>"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"y"),e("\n\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token hvariable"},"msg"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"error"),e(),a("span",{class:"token hvariable"},"msg"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),Wa=a("ul",null,[a("li",null,"特性: 允许返回值之间具有弹性交互")],-1),Ea=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"{- 当出现异常后,之后所有的值都变为Nothing -}"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token number"},"4"),e("\n "),a("span",{class:"token operator"},"\\"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),$a=a("p",null,"Monad Laws:",-1),Pa=a("ul",null,[a("li",null,[e("return 满足 Left identity: "),a("code",null,"return x >>= f 等于 f x")]),a("li",null,[e("return 满足 right identity: "),a("code",null,"m >>= return 等于 m")]),a("li",null,[e("Associativity: 结合律 "),a("code",null,"(m >>= f) >>= g 等于 m >>= (\\x -> f x >>= g)")])],-1),qa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"100000"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"100003"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"100000"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"100003"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token string"},'"move on up"'),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token string"},'"move on up"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token string"},'"Wah!"'),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Wah"),a("span",{class:"token operator"},"!"),e("\n\n"),a("span",{class:"token comment"},"{-Tips: 利用结合律合并两个 Monadic Function-}"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<=<"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<=<"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<=<"),e(),a("span",{class:"token hvariable"},"g"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),Ra=a("h5",{id:"maybe-monad"},[a("a",{class:"header-anchor",href:"#maybe-monad","aria-hidden":"true"},"#"),e(" Maybe Monad")],-1),za=a("p",null,"具有失败可能性的 context 封装,灵活处理异常(返回值为 Nothing)",-1),Da=a("h6",{id:"实现"},[a("a",{class:"header-anchor",href:"#实现","aria-hidden":"true"},"#"),e(" 实现")],-1),Ja=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"applyMaybe"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"b"),e("\n"),a("span",{class:"token hvariable"},"applyMaybe"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token hvariable"},"f"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"applyMaybe"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Ga=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),_a=a("h6",{id:"do-表示法"},[a("a",{class:"header-anchor",href:"#do-表示法","aria-hidden":"true"},"#"),e(" do 表示法")],-1),Ha=a("ul",null,[a("li",null,"在 do expression 中,每一行都是一个 monadic value"),a("li",null,"检查返回值,使用 <-")],-1),Ka=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e("\n "),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token string"},'"!"'),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e(" "),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token string"},'"!"'),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"->"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"routine"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token constant"},"Pole"),e("\n"),a("span",{class:"token hvariable"},"routine"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token hvariable"},"first"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token hvariable"},"start"),e("\n "),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token hvariable"},"second"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token hvariable"},"first"),e("\n "),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token hvariable"},"second"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br")])],-1),Ya=a("h5",{id:"list-monad"},[a("a",{class:"header-anchor",href:"#list-monad","aria-hidden":"true"},"#"),e(" List Monad")],-1),Va=a("ul",null,[a("li",null,"non-determinism(不确定性)")],-1),Ua=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1000"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1000"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"200"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2000"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"30"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"300"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3000"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Za=a("ul",null,[a("li",null,"实现")],-1),ja=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"concat"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Qa=a("ul",null,[a("li",null,[e("返回值交互: 下例中 n 与 return (n, ch) 进行交互 "),a("ul",null,[a("li",null,"list comprehension 与 do 表示法 均是 >>= 的语法糖")])]),a("li",null,"list comprehension: <- 与 条件表达式"),a("li",null,"do 表示法: <- 与 guard 函数")],-1),Xa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"ch"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"ch"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token comment"},"{- do 表示法 -}"),e("\n"),a("span",{class:"token hvariable"},"listOfTuples"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Int"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"listOfTuples"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"ch"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"ch"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"sevensOnly"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Int"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"sevensOnly"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"50"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"guard"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token char string"},"'7'"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e("\n\n"),a("span",{class:"token comment"},"{- list comprehension -}"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"ch"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"ch"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1),ne=a("h5",{id:"monadplus"},[a("a",{class:"header-anchor",href:"#monadplus","aria-hidden":"true"},"#"),e(" MonadPlus")],-1),se=a("p",null,"使 Monad 具有 Monoid 的性质(二元封闭运算)",-1),ae=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"MonadPlus"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mzero"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"mplus"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ee=a("h5",{id:"monad-algorithms"},[a("a",{class:"header-anchor",href:"#monad-algorithms","aria-hidden":"true"},"#"),e(" Monad Algorithms")],-1),te=a("h6",{id:"马走日"},[a("a",{class:"header-anchor",href:"#马走日","aria-hidden":"true"},"#"),e(" 马走日")],-1),le=a("ul",null,[a("li",null,"计算出可移动位置")],-1),oe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"KnightPos"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"KnightPos"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),e("'"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),e("'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"guard"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),e("' `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&&"),e(),a("span",{class:"token hvariable"},"r"),e("' `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),e("'"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),e("'"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),pe=a("ul",null,[a("li",null,"利用 >>= 向后传递多个可交互的位置")],-1),ce=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"in3"),e(),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"moveKnight"),e("\n\n"),a("span",{class:"token hvariable"},"in3"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"KnightPos"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"KnightPos"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"in3"),e(),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"first"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token hvariable"},"start"),e("\n "),a("span",{class:"token hvariable"},"second"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token hvariable"},"first"),e("\n "),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token hvariable"},"second"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),re=a("ul",null,[a("li",null,"最后完成完整函数: 产生所有三步的可能位置,检查其中一个位置是否在里面")],-1),ue=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"canReachIn3"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"KnightPos"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"KnightPos"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n"),a("span",{class:"token hvariable"},"canReachIn3"),e(),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token hvariable"},"end"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"end"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token hvariable"},"in3"),e(),a("span",{class:"token hvariable"},"start"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ie=a("h4",{id:"foldable"},[a("a",{class:"header-anchor",href:"#foldable","aria-hidden":"true"},"#"),e(" Foldable")],-1),ke=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.Foldable "),a("span",{class:"token keyword"},"as"),e(" F")]),e("\n\n"),a("span",{class:"token hvariable"},"foldMap"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Foldable"),e(),a("span",{class:"token hvariable"},"t"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),be=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"F.Foldable"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"foldMap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token constant"},"Empty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"mempty"),e("\n "),a("span",{class:"token hvariable"},"foldMap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"l"),e(),a("span",{class:"token hvariable"},"r"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"F.foldMap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"l"),e(),a("span",{class:"token operator"},"`mappend`"),e("\n "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(" "),a("span",{class:"token operator"},"`mappend`"),e("\n "),a("span",{class:"token hvariable"},"F.foldMap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"r"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),me=a("h4",{id:"data-monoid"},[a("a",{class:"header-anchor",href:"#data-monoid","aria-hidden":"true"},"#"),e(" Data Monoid")],-1),he=a("ul",null,[a("li",null,"成员: 必须为具体类型(*不可是类型构造符(type constructor))"),a("li",null,[e("准则(Monoid Law): "),a("ul",null,[a("li",null,[a("em",null,"结合律"),e(" a·(b·c) = (a·b)·c")]),a("li",null,[e("无需满足 a "),a("code",null,"mappend"),e(" b == b "),a("code",null,"mappend"),e(" a")])])])],-1),de=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"m"),e(" "),a("span",{class:"token comment"},"-- identity"),e("\n "),a("span",{class:"token hvariable"},"mappend"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e("\n "),a("span",{class:"token hvariable"},"mconcat"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e("\n "),a("span",{class:"token hvariable"},"mconcat"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token hvariable"},"mappend"),e(),a("span",{class:"token hvariable"},"mempty"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),ve=a("ul",null,[a("li",null,"实例")],-1),ge=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"mappend"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ye=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"newtype"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(" "),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"getProduct"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Bounded"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token number"},"1"),e("\n "),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getProduct"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token number"},"24"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),fe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"newtype"),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"getAny"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Bool"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Bounded"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token constant"},"False"),e("\n "),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"||"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getAny"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"mconcat"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"False"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"False"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"False"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),we=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"newtype"),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"getAll"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Bool"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Bounded"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"&&"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getAll"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"mconcat"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"False"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),xe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token constant"},"Ordering"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"EQ"),e("\n "),a("span",{class:"token constant"},"LT"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"LT"),e("\n "),a("span",{class:"token constant"},"EQ"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"y"),e("\n "),a("span",{class:"token constant"},"GT"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"GT"),e("\n\n"),a("span",{class:"token comment"},"-- Tips:"),e("\n"),a("span",{class:"token comment"},"-- mappend 在左边不等于 EQ 的情况下都会回传左边的值。相反地则回传右边的值"),e("\n"),a("span",{class:"token comment"},"-- 可代替多个 if/else 语句"),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.Monoid")]),e("\n\n"),a("span",{class:"token hvariable"},"lengthCompare"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Ordering"),e("\n"),a("span",{class:"token hvariable"},"lengthCompare"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"x"),e(" `"),a("span",{class:"token builtin"},"compare"),e("` "),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`mappend`"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"vowels"),e(),a("span",{class:"token hvariable"},"x"),e(" `"),a("span",{class:"token builtin"},"compare"),e("` "),a("span",{class:"token hvariable"},"vowels"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`mappend`"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(" `"),a("span",{class:"token builtin"},"compare"),e("` "),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"where"),e(),a("span",{class:"token hvariable"},"vowels"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"length"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token string"},'"aeiou"'),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),Se=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"m"),e("\n "),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"m"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"m1"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"m2"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"m1"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"m2"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getFirst"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token char string"},"'a'"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br")])],-1),Le=a("h3",{id:"custom-type-class"},[a("a",{class:"header-anchor",href:"#custom-type-class","aria-hidden":"true"},"#"),e(" Custom Type Class")],-1),Me=a("ul",null,[a("li",null,[e("创建新类: "),a("em",null,"可以只有声明没有实现")])],-1),Fe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"ClassName"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"defining"),e(),a("span",{class:"token hvariable"},"code"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Te=a("ul",null,[a("li",null,[e("创建已有类的实例: "),a("em",null,"必须实现所有已声明函数"),a("ul",null,[a("li",null,"作用等同于 deriving(自由度更大)"),a("li",null,"可以重写函数,去除默认函数处理,达到特定目的")])])],-1),Ie=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token operator"},"-"),e(" 先创建新类型\n"),a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"TrafficLight"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Red"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Yellow"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Green"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token constant"},"TrafficLight"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token constant"},"Red"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Red"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token constant"},"Green"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Green"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token constant"},"Yellow"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Yellow"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Show"),e(),a("span",{class:"token constant"},"TrafficLight"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token constant"},"Red"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Red light"'),e("\n "),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token constant"},"Yellow"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Yellow light"'),e("\n "),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token constant"},"Green"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Green light"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br")])],-1),Ae=a("ul",null,[a("li",null,[e("创建新类和实现实例时,使用 class constraint "),a("ul",null,[a("li",null,[e("可达到"),a("em",null,"类似于"),e("继承的效果")]),a("li",null,"可达到限制类型的效果")])])],-1),Ce=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token operator"},"..."),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"y"),e("\n "),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Oe=a("h3",{id:"data-type"},[a("a",{class:"header-anchor",href:"#data-type","aria-hidden":"true"},"#"),e(),a("code",null,"data"),e(" type")],-1),Ne=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"SelfDefinedTypeName"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token constant"},"ValueConstructorName"),e(),a("span",{class:"token constant"},"ValueType"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"TypeClass"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Be=a("ul",null,[a("li",null,"data 范例")],-1),We=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Shape"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Rectangle"),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ee=a("ul",null,[a("li",null,"导出 data")],-1),$e=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Shapes"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token constant"},"Point"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Shape"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Pe=a("ul",null,[a("li",null,"后构造器 > 前构造器")],-1),qe=a("blockquote",null,[a("p",null,"e.g True > False")],-1),Re=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Bool"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"True"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),ze=a("h4",{id:"函数特性"},[a("a",{class:"header-anchor",href:"#函数特性","aria-hidden":"true"},"#"),e(" 函数特性")],-1),De=a("p",null,"data type 也是函数,若省略参数亦会造成 Curry 化.",-1),Je=a("blockquote",null,[a("p",null,"e.g map fx list")],-1),Ge=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10.0"),e(),a("span",{class:"token number"},"20.0"),e(),a("span",{class:"token number"},"4.0"),a("span",{class:"token punctuation"},","),e("\n"),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10.0"),e(),a("span",{class:"token number"},"20.0"),e(),a("span",{class:"token number"},"5.0"),a("span",{class:"token punctuation"},","),e("\n"),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10.0"),e(),a("span",{class:"token number"},"20.0"),e(),a("span",{class:"token number"},"6.0"),a("span",{class:"token punctuation"},","),e("\n"),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10.0"),e(),a("span",{class:"token number"},"20.0"),e(),a("span",{class:"token number"},"6.0"),e("\n"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),_e=a("ul",null,[a("li",null,[e("Value Constructor:使用"),a("code",null,"ValueConstructorName ValueType .."),e("可构造出一个该类型的定义/名字")])],-1),He=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token number"},"20"),e(),a("span",{class:"token number"},"30"),e("\n"),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token number"},"20"),e(),a("span",{class:"token number"},"30"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ke=a("h4",{id:"记录语法-record-syntax"},[a("a",{class:"header-anchor",href:"#记录语法-record-syntax","aria-hidden":"true"},"#"),e(" 记录语法(Record Syntax)")],-1),Ye=a("ul",null,[a("li",null,"定义")],-1),Ve=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Person"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Person"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"firstName"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"lastName"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"age"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"height"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"phoneNumber"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"flavor"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n "),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Ue=a("ul",null,[a("li",null,"使用")],-1),Ze=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token hvariable"},"company"),a("span",{class:"token operator"},"="),a("span",{class:"token string"},'"Ford"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"model"),a("span",{class:"token operator"},"="),a("span",{class:"token string"},'"Mustang"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"year"),a("span",{class:"token operator"},"="),a("span",{class:"token number"},"1967"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token hvariable"},"company"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Ford"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"model"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Mustang"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"year"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1967"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),je=a("h4",{id:"类型参数-type-parameters"},[a("a",{class:"header-anchor",href:"#类型参数-type-parameters","aria-hidden":"true"},"#"),e(" 类型参数(Type Parameters)")],-1),Qe=a("p",null,"提高代码的复用性",-1),Xe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"company"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"model"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"b"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"year"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"c"),e("\n "),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),nt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"tellCar"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"tellCar"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token hvariable"},"company"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"model"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"year"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token string"},'"This "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" was made in "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"y"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),st=a("h5",{id:"maybe-value-constructor"},[a("a",{class:"header-anchor",href:"#maybe-value-constructor","aria-hidden":"true"},"#"),e(" Maybe value constructor")],-1),at=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),et=a("ul",null,[a("li",null,"Just 可实现转化:")],-1),tt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),lt=a("h4",{id:"deriving-派生"},[a("a",{class:"header-anchor",href:"#deriving-派生","aria-hidden":"true"},"#"),e(" Deriving(派生)")],-1),ot=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Day"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Monday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Tuesday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Wednesday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Thursday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Friday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Saturday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Sunday"),e("\n "),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Bounded"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Enum"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),pt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Wednesday"),e("\n"),a("span",{class:"token constant"},"Wednesday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token constant"},"Wednesday"),e("\n"),a("span",{class:"token string"},'"Wednesday"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"read"),e(),a("span",{class:"token string"},'"Saturday"'),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Saturday"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Saturday"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Sunday"),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Saturday"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Saturday"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Saturday"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Friday"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Monday"),e(" `"),a("span",{class:"token builtin"},"compare"),e("` "),a("span",{class:"token constant"},"Wednesday"),e("\n"),a("span",{class:"token constant"},"LT"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"minBound"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Monday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"maxBound"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Sunday"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"succ"),e(),a("span",{class:"token constant"},"Monday"),e("\n"),a("span",{class:"token constant"},"Tuesday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"pred"),e(),a("span",{class:"token constant"},"Saturday"),e("\n"),a("span",{class:"token constant"},"Friday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Thursday"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token constant"},"Sunday"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Thursday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Friday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Saturday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Sunday"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token builtin"},"minBound"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token builtin"},"maxBound"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Day"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Monday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Tuesday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Wednesday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Thursday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Friday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Saturday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Sunday"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br")])],-1),ct=a("h4",{id:"type-定义"},[a("a",{class:"header-anchor",href:"#type-定义","aria-hidden":"true"},"#"),e(" type 定义")],-1),rt=a("p",null,[e("为 data 声明别名 - "),a("code",null,"typedef")],-1),ut=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"PhoneNumber"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"Name"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"PhoneBook"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Name"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"PhoneNumber"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),it=a("ul",null,[a("li",null,"type 类型参数: 匹配 data 类型参数")],-1),kt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"AssocList"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"IntMap"),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"IntMap"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token constant"},"Int"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),bt=a("p",null,"类型别名,只可以在 Haskell 的类型部分中使用:",-1),mt=a("ul",null,[a("li",null,"定义新类型"),a("li",null,"类型声明"),a("li",null,"类型注释(:😃"),a("li",null,[e("禁止: 定义名字/定义 "),a("em",null,"AssocList [(1,2),(4,5),(7,9)]")])],-1),ht=a("h4",{id:"高级数据结构"},[a("a",{class:"header-anchor",href:"#高级数据结构","aria-hidden":"true"},"#"),e(" 高级数据结构")],-1),dt=a("h5",{id:"栈"},[a("a",{class:"header-anchor",href:"#栈","aria-hidden":"true"},"#"),e(" 栈")],-1),vt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Int"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"pop"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Int"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Stack"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"pop"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Stack"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"a"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),gt=a("h5",{id:"链表"},[a("a",{class:"header-anchor",href:"#链表","aria-hidden":"true"},"#"),e(" 链表")],-1),yt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"List"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Empty"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Cons"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"List"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"List"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Empty"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Cons"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"listHead"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"listTail"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"List"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ft=a("h5",{id:"二叉树"},[a("a",{class:"header-anchor",href:"#二叉树","aria-hidden":"true"},"#"),e(" 二叉树")],-1),wt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),xt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"singleton"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"singleton"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token constant"},"EmptyTree"),e("\n\n"),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"singleton"),e(),a("span",{class:"token hvariable"},"x"),e("\n"),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"left"),e(),a("span",{class:"token hvariable"},"right"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"left"),e(),a("span",{class:"token hvariable"},"right"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token hvariable"},"a"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"left"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"right"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"a"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"left"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"right"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n"),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"left"),e(),a("span",{class:"token hvariable"},"right"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token hvariable"},"a"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"left"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"a"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"right"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"nums"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"numsTree"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token hvariable"},"nums"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1),St=a("h2",{id:"函数"},[a("a",{class:"header-anchor",href:"#函数","aria-hidden":"true"},"#"),e(" 函数")],-1),Lt=a("h3",{id:"递归函数"},[a("a",{class:"header-anchor",href:"#递归函数","aria-hidden":"true"},"#"),e(" 递归函数")],-1),Mt=a("ul",null,[a("li",null,"边界条件"),a("li",null,"递归基础"),a("li",null,"递归函数体")],-1),Ft=a("h4",{id:"list-函数"},[a("a",{class:"header-anchor",href:"#list-函数","aria-hidden":"true"},"#"),e(" List 函数")],-1),Tt=a("ul",null,[a("li",null,"边界条件: 空 List"),a("li",null,"递归函数体: x:xs 取出首元素进行一般操作,对尾部进行递归操作.")],-1),It=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"error"),e(),a("span",{class:"token string"},'"maximum of empty list"'),e("\n"),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e("\n"),a("span",{class:"token comment"},"{-\nmaximum' (x:xs)\n | x > maxTail = x\n | otherwise = maxTail\n where maxTail = maximum' xs\n-}"),e("\n"),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),At=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"replicate"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"i"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"i"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"i"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"replicate"),e("' "),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token builtin"},"replicate"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ct=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"i"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"i"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"i"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token hvariable"},"_"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Ot=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Nt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"repeat"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"repeat"),e("' "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token builtin"},"repeat"),e("' "),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Bt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token hvariable"},"ys"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Wt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"elem"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n"),a("span",{class:"token builtin"},"elem"),e("' "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token builtin"},"elem"),e("' "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"x"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"a"),e(" `"),a("span",{class:"token builtin"},"elem"),e("'` "),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Et=a("p",null,"Awesome Quick Sort",-1),$t=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"smallerSorted"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"biggerSorted"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token hvariable"},"smallerSorted"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"biggerSorted"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Pt=a("h3",{id:"高阶函数"},[a("a",{class:"header-anchor",href:"#高阶函数","aria-hidden":"true"},"#"),e(" 高阶函数")],-1),qt=a("h4",{id:"curry-化"},[a("a",{class:"header-anchor",href:"#curry-化","aria-hidden":"true"},"#"),e(" Curry 化")],-1),Rt=a("p",null,[e("当传入不全参数时,会改变函数的类型,"),a("em",null,"返回值从单类型变成函数类型"),e(".")],-1),zt=a("ul",null,[a("li",null,"当传入不全参数时:"),a("li",null,"compare 的类型变为 (Ord a) => a -> (a -> Ordering)")],-1),Dt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"compareWithHundred"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Ordering"),e("\n"),a("span",{class:"token hvariable"},"compareWithHundred"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"compare"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Jt=a("ul",null,[a("li",null,"通过给二元中缀函数传递唯一参数:"),a("li",null,"中缀函数类型由 a -> a -> a 转为 a -> a")],-1),Gt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"divideByTen"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Floating"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"divideByTen"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Fractional"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),_t=a("ul",null,[a("li",null,[e("包装函数: "),a("ul",null,[a("li",null,"传入一个二元函数作为参数,便可实现 zipWithFunc"),a("li",null,"若在定义时便传入一个函数参数,便可实现 Curry 化")])])],-1),Ht=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token hvariable"},"ys"),e("\n\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"foo "'),e(","),a("span",{class:"token string"},'"bar "'),e(","),a("span",{class:"token string"},'"baz "'),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"fighters"'),e(","),a("span",{class:"token string"},'"hoppers"'),e(","),a("span",{class:"token string"},'"aldrin"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"foo fighters"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"bar hoppers"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"baz aldrin"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"replicate"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"30"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),Kt=a("ul",null,[a("li",null,"通过 Curry 化,还可省略参数")],-1),Yt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"bar"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e("\n"),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"bar"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Vt=a("h4",{id:"map-函数"},[a("a",{class:"header-anchor",href:"#map-函数","aria-hidden":"true"},"#"),e(" map 函数")],-1),Ut=a("p",null,"映射函数 - List Comprehension 的函数化",-1),Zt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),jt=a("ul",null,[a("li",null,[e("如果 map 传入的函数参数的类型为 a -> a -> a,则返回的 List 类型为"),a("code",null,"[a -> a]"),e("(f x 传参不完全,造成了 Curry 化).")])],-1),Qt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"listOfFun"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"listOfFun"),e(),a("span",{class:"token operator"},"!!"),e(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token number"},"20"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Xt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'"!"'),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"BIFF"'),e(","),a("span",{class:"token string"},'"BANG"'),e(","),a("span",{class:"token string"},'"POW"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"BIFF!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"BANG!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"POW!"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"replicate"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"25"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"36"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"49"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"64"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"fst"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),nl=a("h4",{id:"filter-函数"},[a("a",{class:"header-anchor",href:"#filter-函数","aria-hidden":"true"},"#"),e(" filter 函数")],-1),sl=a("p",null,"过滤函数 - Comprehension 的函数化",-1),al=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token hvariable"},"x"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token hvariable"},"xs"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),el=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"even"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"notNull"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"not"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"notNull"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"u LaUgH aT mE BeCaUsE I aM diFfeRent"'),e("\n"),a("span",{class:"token string"},'"uagameasadifeent"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"i lauGh At You BecAuse u r aLL the Same"'),e("\n"),a("span",{class:"token string"},'"GAYBALLS"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),tl=a("h4",{id:"fold-函数与-scan-函数"},[a("a",{class:"header-anchor",href:"#fold-函数与-scan-函数","aria-hidden":"true"},"#"),e(" fold 函数与 scan 函数")],-1),ll=a("p",null,[a("img",{src:"/awesome-notes/assets/foldl.8f7f9807.png",alt:"折叠函数"})],-1),ol=a("blockquote",null,[a("p",null,"如图中所示,左折叠时将 acc 视为第 0 个元素;右折叠时将 acc 视为最后一个元素.")],-1),pl=a("ul",null,[a("li",null,[e("三要素: "),a("ul",null,[a("li",null,"二元函数 \\acc x -> function 或 \\x acc -> function"),a("li",null,"初始累加值"),a("li",null,"待折叠 List")])]),a("li",null,[e("工作原理: "),a("ul",null,[a("li",null,"不断从 List 中取出元素,进行二元函数调用,直至 List 被取空"),a("li",null,"调用参数分别为 新取出元素 x 与 之前 n 次调用后的累加值 acc"),a("li",null,"返回值作为下次调用的累加值 acc")])]),a("li",null,[e("左折叠函数 "),a("ul",null,[a("li",null,[e("foldl "),a("code",null,"\\acc x ->")]),a("li",null,"foldl1: 取 List 首元素作为初始累加值")])])],-1),cl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Foldable"),e(),a("span",{class:"token hvariable"},"t"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),rl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ul=a("ul",null,[a("li",null,[e("右折叠函数 "),a("ul",null,[a("li",null,[e("foldr "),a("code",null,"\\x acc ->")]),a("li",null,"foldr1: 取 List 尾元素作为初始累加值")])])],-1),il=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Foldable"),e(),a("span",{class:"token hvariable"},"t"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),kl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"map"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"map"),e("' "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),bl=a("ul",null,[a("li",null,"更多范例")],-1),ml=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token builtin"},"product"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"product"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token builtin"},"filter"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"filter"),e("' "),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token builtin"},"last"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"last"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldl1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),hl=a("ul",null,[a("li",null,"scanl/scanr/scanl1/scanr1 函数会将每次折叠的结果都记录在一个 List 中")],-1),dl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"scanl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"scanr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"scanl1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"scanl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"flip"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},":"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),vl=a("ul",null,[a("li",null,"逆波兰表达式")],-1),gl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e("\n\n"),a("span",{class:"token hvariable"},"solveRPN"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"solveRPN"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"head"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"words"),e("\n"),a("span",{class:"token keyword"},"where"),e(" "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"*"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"+"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"-"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"/"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"^"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"**"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"ln"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"log"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token string"},'"sum"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token hvariable"},"numberString"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"read"),e(),a("span",{class:"token hvariable"},"numberString"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),yl=a("h4",{id:"lambda-表达式"},[a("a",{class:"header-anchor",href:"#lambda-表达式","aria-hidden":"true"},"#"),e(" lambda 表达式")],-1),fl=a("p",null,[a("code",null,"\\args -> function")],-1),wl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"30"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"153.0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"61.5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"31.0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"15.75"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6.6"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token builtin"},"flip"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),e("\n"),a("span",{class:"token builtin"},"flip"),e("' "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),xl=a("h4",{id:"函数-1"},[a("a",{class:"header-anchor",href:"#函数-1","aria-hidden":"true"},"#"),e(" $函数")],-1),Sl=a("p",null,[a("code",null,"($) :: (a -> b) -> a -> b")],-1),Ll=a("ul",null,[a("li",null,"特性: 优先级最低,右结合"),a("li",null,"功能: 改变优先级,相当于在右方添加括号")],-1),Ml=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"z"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"z"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Fl=a("h4",{id:"函数与-function-composition-函数组合"},[a("a",{class:"header-anchor",href:"#函数与-function-composition-函数组合","aria-hidden":"true"},"#"),e(" .函数与 Function composition(函数组合)")],-1),Tl=a("p",null,[a("code",null,"(.) :: (b -> c) -> (a -> b) -> a -> c")],-1),Il=a("p",null,[a("code",null,"f . g = \\x -> f (g x)"),e(" - f 的参数类型必须与 g 的返回值类型相同")],-1),Al=a("ul",null,[a("li",null,"功能: 可以去除函数调用括号")],-1),Cl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"fn"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"ceiling"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"negate"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"tan"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"cos"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token number"},"50"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token operator"},"->"),e(" 去括号"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"fn"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"ceiling"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"negate"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"tan"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"cos"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token number"},"50"),e(),a("span",{class:"token hvariable"},"x"),e("\n"),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Curry"),e("化"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"fn"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"ceiling"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"negate"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"tan"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"cos"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token number"},"50"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Ol=a("ul",null,[a("li",null,"Best Practice: 三种不同的函数写法")],-1),Nl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Integer"),e("\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Integer"),e("\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"sum"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Integer"),e("\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"oddSquares"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"belowLimit"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"oddSquares"),e("\n "),a("span",{class:"token keyword"},"in"),e(" "),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token hvariable"},"belowLimit"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Bl=a("h3",{id:"常用函数"},[a("a",{class:"header-anchor",href:"#常用函数","aria-hidden":"true"},"#"),e(" 常用函数")],-1),Wl=a("h4",{id:"无参函数"},[a("a",{class:"header-anchor",href:"#无参函数","aria-hidden":"true"},"#"),e(" 无参函数")],-1),El=a("p",null,"“定义”(或者“名字”)",-1),$l=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"sabertaz"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"It\'s a-me, sabertaz!"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Pl=a("h4",{id:"前缀函数"},[a("a",{class:"header-anchor",href:"#前缀函数","aria-hidden":"true"},"#"),e(" 前缀函数")],-1),ql=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"succ"),e(),a("span",{class:"token number"},"8"),e("\n"),a("span",{class:"token number"},"9"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Rl=a("p",null,"`FunctionName` - 可使前缀函数变为中缀函数",-1),zl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"92"),e(" `"),a("span",{class:"token builtin"},"div"),e("` "),a("span",{class:"token number"},"10"),e("\n"),a("span",{class:"token number"},"9"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Dl=a("h4",{id:"中缀函数"},[a("a",{class:"header-anchor",href:"#中缀函数","aria-hidden":"true"},"#"),e(" 中缀函数")],-1),Jl=a("p",null,[a("code",null,"+"),e(": 从类型定义可以看出,+左右两边参数必须为同类型")],-1),Gl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),_l=a("p",null,[a("code",null,"++"),e(": List 连接符,遍历前一 List")],-1),Hl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"hello"'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'"world"'),e("\n"),a("span",{class:"token string"},'"hello world"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Kl=a("p",null,[a("code",null,":"),e(": 连接单个元素")],-1),Yl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},":"),a("span",{class:"token string"},'" SMALL CAT"'),e("\n"),a("span",{class:"token string"},'"A SMALL CAT"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"5"),a("span",{class:"token operator"},":"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Vl=a("p",null,[a("code",null,"!!"),e(": 引用符")],-1),Ul=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"33.2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"96.2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11.2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"23.25"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"!!"),e(),a("span",{class:"token number"},"1"),e("\n"),a("span",{class:"token number"},"33.2"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Zl=a("h4",{id:"数学函数"},[a("a",{class:"header-anchor",href:"#数学函数","aria-hidden":"true"},"#"),e(" 数学函数")],-1),jl=a("ul",null,[a("li",null,[e("x "),a("code",null,"mod"),e(" y")]),a("li",null,[a("code",null,"even arg")]),a("li",null,[a("code",null,"odd arg")])],-1),Ql=a("h5",{id:"system-random"},[a("a",{class:"header-anchor",href:"#system-random","aria-hidden":"true"},"#"),e(" System Random")],-1),Xl=a("ul",null,[a("li",null,[a("code",null,"random :: (RandomGen g, Random a) => g -> (a, g)")]),a("li",null,"getStdGen"),a("li",null,"newStdGen")],-1),no=a("h4",{id:"数字函数"},[a("a",{class:"header-anchor",href:"#数字函数","aria-hidden":"true"},"#"),e(" 数字函数")],-1),so=a("ul",null,[a("li",null,"fromInteger 函数 (Num a) => Integer -> a"),a("li",null,"fromIntegral 函数 (Integral a, Num b) => a -> b")],-1),ao=a("h2",{id:"模块"},[a("a",{class:"header-anchor",href:"#模块","aria-hidden":"true"},"#"),e(" 模块")],-1),eo=a("h3",{id:"import"},[a("a",{class:"header-anchor",href:"#import","aria-hidden":"true"},"#"),e(" import")],-1),to=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"nub"),e(","),a("span",{class:"token builtin"},"sort"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List "),a("span",{class:"token keyword"},"hiding")]),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"nub"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),lo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.Map")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),oo=a("p",null,"调用 Map 中的 filter 时,必须使用 Data.Map.filter.",-1),po=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.Map "),a("span",{class:"token keyword"},"as"),e(" M")]),e("\n\n"),a("span",{class:"token constant"},"Data.Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token operator"},"<->"),e(),a("span",{class:"token constant"},"M"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"filter"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),co=a("h3",{id:"建立模块"},[a("a",{class:"header-anchor",href:"#建立模块","aria-hidden":"true"},"#"),e(" 建立模块")],-1),ro=a("h4",{id:"单一模块"},[a("a",{class:"header-anchor",href:"#单一模块","aria-hidden":"true"},"#"),e(" 单一模块")],-1),uo=a("p",null,[e("新建一个 "),a("code",null,"Geometry.hs"),e(" 的文件")],-1),io=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Geometry"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"sphereVolume"),e("\n,"),a("span",{class:"token hvariable"},"sphereArea"),e("\n,"),a("span",{class:"token hvariable"},"cubeVolume"),e("\n,"),a("span",{class:"token hvariable"},"cubeArea"),e("\n,"),a("span",{class:"token hvariable"},"cuboidArea"),e("\n,"),a("span",{class:"token hvariable"},"cuboidVolume"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n\n"),a("span",{class:"token hvariable"},"sphereVolume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"sphereVolume"),e(),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4.0"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token number"},"3.0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"sphereArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"sphereArea"),e(),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"cubeVolume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"cubeVolume"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"cuboidVolume"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e("\n\n"),a("span",{class:"token hvariable"},"cubeArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"cubeArea"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"cuboidArea"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e("\n\n"),a("span",{class:"token hvariable"},"cuboidVolume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"cuboidVolume"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"c"),e("\n\n"),a("span",{class:"token hvariable"},"cuboidArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"cuboidArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e("\n\n"),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br")])],-1),ko=a("ul",null,[a("li",null,"使用")],-1),bo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Geometry")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),mo=a("h4",{id:"子模块"},[a("a",{class:"header-anchor",href:"#子模块","aria-hidden":"true"},"#"),e(" 子模块")],-1),ho=a("p",null,[e("把 "),a("code",null,"Geometry"),e(" 分成三个子模块")],-1),vo=a("p",null,[e("建立一个 "),a("code",null,"Geometry"),e(" 文件夹(注意"),a("em",null,"首字母"),e("要大写),新建三个文件")],-1),go=a("ul",null,[a("li",null,"sphere.hs")],-1),yo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Geometry.Sphere"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"volume"),e("\n,"),a("span",{class:"token hvariable"},"area"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4.0"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token number"},"3.0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),fo=a("ul",null,[a("li",null,"cuboid.hs")],-1),wo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Geometry.Cuboid"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"volume"),e("\n,"),a("span",{class:"token hvariable"},"area"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"c"),e("\n\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e("\n\n"),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),xo=a("ul",null,[a("li",null,"cube.hs")],-1),So=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Geometry.Cube"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"volume"),e("\n,"),a("span",{class:"token hvariable"},"area"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Geometry.Cuboid "),a("span",{class:"token keyword"},"as"),e(" Cuboid")]),e("\n\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Cuboid.volume"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e("\n\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Cuboid.area"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),Lo=a("ul",null,[a("li",null,"使用")],-1),Mo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Geometry.Sphere")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Fo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Geometry.Sphere "),a("span",{class:"token keyword"},"as"),e(" Sphere")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Geometry.Cuboid "),a("span",{class:"token keyword"},"as"),e(" Cuboid")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Geometry.Cube "),a("span",{class:"token keyword"},"as"),e(" Cube")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),To=a("h3",{id:"常用基础模块"},[a("a",{class:"header-anchor",href:"#常用基础模块","aria-hidden":"true"},"#"),e(" 常用基础模块")],-1),Io=a("p",null,[a("a",{href:"https://github.com/MnO2/learnyouahaskell-zh/blob/develop/zh-cn/ch07/module.md",target:"_blank",rel:"noopener noreferrer"},"Learn you a haskell for great good")],-1),Ao=a("h4",{id:"data-list"},[a("a",{class:"header-anchor",href:"#data-list","aria-hidden":"true"},"#"),e(" Data.List")],-1),Co=a("ul",null,[a("li",null,"intersperse"),a("li",null,"intercalate"),a("li",null,"transpose")],-1),Oo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"transpose"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"transpose"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"there"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"guys"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"htg"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"ehu"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"yey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"rs"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"e"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),No=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"transpose"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"18"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"17"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Bo=a("p",null,[a("strong",null,"foldl'"),e(" 和 "),a("strong",null,"foldl1'"),e(" 是它们各自惰性实现的严格版本,可"),a("em",null,"防止溢出"),e("问题.")],-1),Wo=a("p",null,[a("strong",null,"concat"),e(" - 移除一级嵌套")],-1),Eo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"concat"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"foo"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"bar"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"car"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token string"},'"foobarcar"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"concat"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),$o=a("p",null,[a("strong",null,"concatMap"),e(" 函数与 "),a("code",null,"map"),e(" 一个 List 之后再 "),a("code",null,"concat"),e(" 它等价")],-1),Po=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"concatMap"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"replicate"),e(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),qo=a("p",null,"and",-1),Ro=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"and"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"and"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),zo=a("p",null,"or",-1),Do=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"or"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"or"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Jo=a("p",null,[a("strong",null,"any"),e(" 和 "),a("strong",null,"all"),e(" 使用 "),a("code",null,"any"),e(" 或 "),a("code",null,"all"),e(" 会更多些")],-1),Go=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"any"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"HEYGUYSwhatsup"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"any"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"HEYGUYSwhatsup"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),_o=a("p",null,[a("strong",null,"iterate"),e(" 取一个函数和一个值作参数。它会用该值去调用该函数并用所得的结果再次调用该函数,产生一个无限的 List.")],-1),Ho=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"iterate"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"1"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"64"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"128"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"256"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"512"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"iterate"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'"haha"'),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"haha"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"haha"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"hahahaha"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"hahahahahaha"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ko=a("p",null,"splitAt",-1),Yo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"splitAt"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token string"},'"heyman"'),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"man"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"splitAt"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token string"},'"heyman"'),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"heyman"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"splitAt"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"heyman"'),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"heyman"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"splitAt"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token string"},'"foobar"'),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token string"},'"barfoo"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),Vo=a("p",null,[a("strong",null,"takeWhile"),e(" 一旦遇到不符合条件的某元素就停止")],-1),Uo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token char string"},"' '"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"This is a sentence"'),e("\n"),a("span",{class:"token string"},'"This"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Zo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"53361"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),jo=a("p",null,[a("strong",null,"dropWhile"),e(" 扔掉符合条件的元素。一旦限制条件返回 "),a("code",null,"False"),e(",它就返回 List 的余下部分")],-1),Qo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"dropWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token char string"},"' '"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"This is a sentence"'),e("\n"),a("span",{class:"token string"},'" is a sentence"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"dropWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Xo=a("p",null,[a("strong",null,"span"),e(" - 扩展"),a("code",null,"takeWhile")],-1),np=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"fw"),e(","),a("span",{class:"token hvariable"},"rest"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"span"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token char string"},"' '"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token operator"},"\\"),e(),a("span",{class:"token string"},'"This is a sentence"'),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token string"},'"First word:"'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"fw"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'",the rest:"'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"rest"),e("\n"),a("span",{class:"token string"},'"First word: This,the rest: is a sentence"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),sp=a("p",null,[a("strong",null,"break"),e(" - 取反"),a("code",null,"span")],-1),ap=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"break"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"span"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ep=a("p",null,"sort",-1),tp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"sort"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"sort"),e(),a("span",{class:"token string"},'"This will be sorted soon"'),e("\n"),a("span",{class:"token string"},'" Tbdeehiillnooorssstw"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),lp=a("p",null,"group",-1),op=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"group"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),pp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"l"),a("span",{class:"token operator"},"@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"l"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"group"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"sort"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),cp=a("p",null,[a("strong",null,"inits"),e(" 和 "),a("strong",null,"tails")],-1),rp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"inits"),e(),a("span",{class:"token string"},'"w00t"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w0"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w00"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w00t"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"tails"),e(),a("span",{class:"token string"},'"w00t"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"w00t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"00t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"0t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"w"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"w00t"'),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token builtin"},"zip"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"inits"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"tails"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w00t"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"w"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"00t"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"w0"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"0t"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"w00"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"t"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"w00t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),up=a("p",null,[a("strong",null,"isInfixOf"),e(" 从一个 List 中搜索一个子 List")],-1),ip=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"search"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n"),a("span",{class:"token hvariable"},"search"),e(),a("span",{class:"token hvariable"},"needle"),e(),a("span",{class:"token hvariable"},"haystack"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"nLen"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"needle"),e("\n "),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token hvariable"},"nLen"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"needle"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token constant"},"True"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token constant"},"False"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"tails"),e(),a("span",{class:"token hvariable"},"haystack"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),kp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"cat"'),e(),a("span",{class:"token operator"},"`isInfixOf`"),e(),a("span",{class:"token string"},'"im a cat burglar"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"Cat"'),e(),a("span",{class:"token operator"},"`isInfixOf`"),e(),a("span",{class:"token string"},'"im a cat burglar"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"cats"'),e(),a("span",{class:"token operator"},"`isInfixOf`"),e(),a("span",{class:"token string"},'"im a cat burglar"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),bp=a("p",null,[a("strong",null,"isPrefixOf"),e(" 与 "),a("strong",null,"isSuffixOf")],-1),mp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"hey"'),e(),a("span",{class:"token operator"},"`isPrefixOf`"),e(),a("span",{class:"token string"},'"hey there!"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"hey"'),e(),a("span",{class:"token operator"},"`isPrefixOf`"),e(),a("span",{class:"token string"},'"oh hey there!"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"there!"'),e(),a("span",{class:"token operator"},"`isSuffixOf`"),e(),a("span",{class:"token string"},'"oh hey there!"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"there!"'),e(),a("span",{class:"token operator"},"`isSuffixOf`"),e(),a("span",{class:"token string"},'"oh hey there"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),hp=a("p",null,[a("strong",null,"elem"),e(" 与 "),a("strong",null,"notElem")],-1),dp=a("p",null,"partition",-1),vp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"partition"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"BOBsidneyMORGANeddy"'),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"BOBMORGAN"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"sidneyeddy"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"partition"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),gp=a("p",null,"find",-1),yp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"find"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"find"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"find"),e("\n"),a("span",{class:"token hvariable"},"find"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),fp=a("p",null,[a("strong",null,"elemIndex"),e(" '可能' (Maybe)返回我们找的元素的索引,若这一元素不存在,就返回 "),a("code",null,"Nothing"),e(".")],-1),wp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"elemIndex"),e("\n"),a("span",{class:"token hvariable"},"elemIndex"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"`elemIndex`"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token operator"},"`elemIndex`"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),xp=a("p",null,[a("strong",null,"elemIndices"),e(" 与"),a("code",null,"elemIndex"),e("相似.")],-1),Sp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token char string"},"' '"),e(),a("span",{class:"token operator"},"`elemIndices`"),e(),a("span",{class:"token string"},'"Where are the spaces?"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"13"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Lp=a("p",null,[a("strong",null,"findIndex"),e(" 与 "),a("code",null,"find"),e(" 相似. "),a("strong",null,"findIndices"),e(" 返回所有符合条件的索引.")],-1),Mp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"findIndex"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"findIndex"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"findIndices"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"Where Are The Caps?"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"14"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Fp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith3"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token hvariable"},"z"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"z"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"zip4"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Tp=a("p",null,[e("在处理来自文件或其它地方的输入时,"),a("strong",null,"lines"),e(" 会非常有用.")],-1),Ip=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token string"},'"first line\\nsecond line\\nthird line"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"first line"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"second line"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"third line"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ap=a("p",null,[a("strong",null,"unlines"),e(" 是 "),a("code",null,"lines"),e(" 的反函数.")],-1),Cp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"unlines"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"first line"'),e(","),a("span",{class:"token string"},'"second line"'),e(","),a("span",{class:"token string"},'"third line"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token string"},'"first line\\nsecond line\\nthird line\\n"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Op=a("p",null,[a("strong",null,"words"),e(" 和 "),a("strong",null,"unwords"),e(" 可以把一个字串分为一组单词或执行相反的操作.")],-1),Np=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"words"),e(),a("span",{class:"token string"},'"hey these are the words in this sentence"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"these"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"are"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"the"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"words"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"in"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"this"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"sentence"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"words"),e(),a("span",{class:"token string"},'"hey these are the words in this\\nsentence"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"these"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"are"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"the"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"words"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"in"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"this"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"sentence"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"unwords"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"there"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"mate"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token string"},'"hey there mate"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Bp=a("p",null,[a("strong",null,"nub"),e(" 可以将一个 List 中的重复元素全部筛掉.")],-1),Wp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"nub"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"nub"),e(),a("span",{class:"token string"},'"Lots of words and stuff"'),e("\n"),a("span",{class:"token string"},'"Lots fwrdanu"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ep=a("p",null,"delete",-1),$p=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),e(),a("span",{class:"token string"},'"hey there ghang!"'),e("\n"),a("span",{class:"token string"},'"ey there ghang!"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token string"},'"hey there ghang!"'),e("\n"),a("span",{class:"token string"},'"ey tere ghang!"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token string"},'"hey there ghang!"'),e("\n"),a("span",{class:"token string"},'"ey tere gang!"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Pp=a("p",null,[a("strong",null,"\\"),e(" 差集")],-1),qp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"\\\\"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"Im a big baby"'),e(),a("span",{class:"token operator"},"\\\\"),e(),a("span",{class:"token string"},'"big"'),e("\n"),a("span",{class:"token string"},'"Im a baby"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Rp=a("p",null,[a("strong",null,"union"),e(" 并集")],-1),zp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"hey man"'),e(),a("span",{class:"token operator"},"`union`"),e(),a("span",{class:"token string"},'"man what\'s up"'),e("\n"),a("span",{class:"token string"},'"hey manwt\'sup"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`union`"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Dp=a("p",null,[a("strong",null,"intersection"),e(" 交集")],-1),Jp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`intersect`"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Gp=a("p",null,"insert",-1),_p=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"insert"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"insert"),e(),a("span",{class:"token char string"},"'g'"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'f'"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'h'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'z'"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token string"},'"abcdefghijklmnopqrstuvwxyz"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"insert"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Hp=a("h5",{id:"修正-prelude-模块"},[a("a",{class:"header-anchor",href:"#修正-prelude-模块","aria-hidden":"true"},"#"),e(" 修正 Prelude 模块")],-1),Kp=a("p",null,[a("code",null,"length"),e(","),a("code",null,"take"),e(","),a("code",null,"drop"),e(","),a("code",null,"splitAt"),e(","),a("code",null,"!!"),e(" 和 "),a("code",null,"replicate"),a("code",null,"Data.List"),e(" 中包含了更通用的替代版,如: "),a("code",null,"genericLength,genericTake,genericDrop,genericSplitAt,genericIndex"),e(" 和 "),a("code",null,"genericReplicate")],-1),Yp=a("p",null,[a("code",null,"nub"),e(", "),a("code",null,"delete"),e(", "),a("code",null,"union"),e(", "),a("code",null,"intsect"),e(" 和 "),a("code",null,"group"),e(" 函数 也有各自的通用替代版 "),a("code",null,"nubBy"),e(","),a("code",null,"deleteBy"),e(","),a("code",null,"unionBy"),e(","),a("code",null,"intersectBy"),e(" 和 "),a("code",null,"groupBy"),e(", 它们的区别就是前一组函数使用 "),a("code",null,"(==)"),e(" 来测试是否相等,而带 "),a("code",null,"By"),e(" 的那组则取一个函数作参数来判定相等性.")],-1),Vp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"values"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"4.3"),e(","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),e(","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1.2"),e(","),a("span",{class:"token number"},"0.4"),e(","),a("span",{class:"token number"},"2.3"),e(","),a("span",{class:"token number"},"5.9"),e(","),a("span",{class:"token number"},"10.5"),e(","),a("span",{class:"token number"},"29.1"),e(","),a("span",{class:"token number"},"5.3"),e(","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),e(","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"14.5"),e(","),a("span",{class:"token number"},"2.9"),e(","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"groupBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"values"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"4.3"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1.2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5.9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10.5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29.1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5.3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"14.5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2.9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Up=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"on"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),e("\n"),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Zp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"groupBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"values"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"4.3"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1.2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5.9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10.5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29.1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5.3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"14.5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2.9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),jp=a("p",null,[a("code",null,"sort"),e(","),a("code",null,"insert"),e(","),a("code",null,"maximum"),e(" 和 "),a("code",null,"min"),e(" 都有各自的通用版本。 如 "),a("code",null,"groupBy"),e(" 类似,"),a("strong",null,"sortBy"),e(","),a("strong",null,"insertBy"),e(","),a("strong",null,"maximumBy"),e(" 和 "),a("strong",null,"minimumBy"),e(" 都取一个函数来比较两个元素的大小.")],-1),Qp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"sortBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"compare"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token builtin"},"length"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"xs"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Xp=a("h4",{id:"data-char"},[a("a",{class:"header-anchor",href:"#data-char","aria-hidden":"true"},"#"),e(" Data Char")],-1),nc=a("ul",null,[a("li",null,[a("strong",null,"isControl"),e(" 判断一个字符是否是控制字符。")]),a("li",null,[a("strong",null,"isSpace"),e(" 判断一个字符是否是空格字符,包括空格,tab,换行符等.")]),a("li",null,[a("strong",null,"isLower"),e(" 判断一个字符是否为小写.")]),a("li",null,[a("strong",null,"isUpper"),e(" 判断一个字符是否为大写。")]),a("li",null,[a("strong",null,"isAlpha"),e(" 判断一个字符是否为字母.")]),a("li",null,[a("strong",null,"isAlphaNum"),e(" 判断一个字符是否为字母或数字.")]),a("li",null,[a("strong",null,"isPrint"),e(" 判断一个字符是否是可打印的.")]),a("li",null,[a("strong",null,"isDigit"),e(" 判断一个字符是否为数字.")]),a("li",null,[a("strong",null,"isOctDigit"),e(" 判断一个字符是否为八进制数字.")]),a("li",null,[a("strong",null,"isHexDigit"),e(" 判断一个字符是否为十六进制数字.")]),a("li",null,[a("strong",null,"isLetter"),e(" 判断一个字符是否为字母.")]),a("li",null,[a("strong",null,"isMark"),e(" 判断是否为 unicode 注音字符,你如果是法国人就会经常用到的.")]),a("li",null,[a("strong",null,"isNumber"),e(" 判断一个字符是否为数字.")]),a("li",null,[a("strong",null,"isPunctuation"),e(" 判断一个字符是否为标点符号.")]),a("li",null,[a("strong",null,"isSymbol"),e("判断一个字符是否为货币符号.")]),a("li",null,[a("strong",null,"isSeparator"),e(" 判断一个字符是否为 unicode 空格或分隔符.")]),a("li",null,[a("strong",null,"isAscii"),e(" 判断一个字符是否在 unicode 字母表的前 128 位。")]),a("li",null,[a("strong",null,"isLatin1"),e(" 判断一个字符是否在 unicode 字母表的前 256 位.")]),a("li",null,[a("strong",null,"isAsciiUpper"),e(" 判断一个字符是否为大写的 ascii 字符.")]),a("li",null,[a("strong",null,"isAsciiLower"),e(" 判断一个字符是否为小写的 ascii 字符.")])],-1),sc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token builtin"},"isAlphaNum"),e(),a("span",{class:"token string"},'"bobby283"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token builtin"},"isAlphaNum"),e(),a("span",{class:"token string"},'"eddy the fish!"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ac=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"words"),e(),a("span",{class:"token string"},'"hey guys its me"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"guys"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"its"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"me"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"groupBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token builtin"},"isSpace"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"hey guys its me"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'" "'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"guys"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'" "'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"its"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'" "'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"me"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),ec=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"not"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"any"),e(),a("span",{class:"token builtin"},"isSpace"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"groupBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token builtin"},"isSpace"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token string"},'"hey guys its me"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"guys"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"its"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"me"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),tc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"' '"),e("\n"),a("span",{class:"token constant"},"Space"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"'A'"),e("\n"),a("span",{class:"token constant"},"UppercaseLetter"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"'a'"),e("\n"),a("span",{class:"token constant"},"LowercaseLetter"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"'.'"),e("\n"),a("span",{class:"token constant"},"OtherPunctuation"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"'9'"),e("\n"),a("span",{class:"token constant"},"DecimalNumber"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token string"},'" \\t\\nA9?|"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Space"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Control"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Control"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"UppercaseLetter"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"DecimalNumber"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"OtherPunctuation"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"MathSymbol"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),lc=a("ul",null,[a("li",null,[a("strong",null,"toUpper"),e(" 将一个字符转为大写字母,若该字符不是小写字母,就按原值返回.")]),a("li",null,[a("strong",null,"toLower"),e(" 将一个字符转为小写字母,若该字符不是大写字母,就按原值返回.")]),a("li",null,[a("strong",null,"toTitle"),e(" 将一个字符转为 title-case,对大多数字元而言,title-case 就是大写.")]),a("li",null,[a("strong",null,"digitToInt"),e(" 将一个字符转为 Int 值,而这一字符必须得在 "),a("code",null,"'1'..'9','a'..'f'"),e("或"),a("code",null,"'A'..'F'"),e(" 的范围之内.")])],-1),oc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"digitToInt"),e(),a("span",{class:"token string"},'"34538"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"digitToInt"),e(),a("span",{class:"token string"},'"FF85AB"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),pc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"intToDigit"),e(),a("span",{class:"token number"},"15"),e("\n"),a("span",{class:"token char string"},"'f'"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"intToDigit"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token char string"},"'5'"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),cc=a("p",null,[a("strong",null,"ord"),e(" 与 "),a("strong",null,"char"),e(" 函数可以将字符与其对应的数字相互转换.")],-1),rc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"ord"),e(),a("span",{class:"token char string"},"'a'"),e("\n"),a("span",{class:"token number"},"97"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"chr"),e(),a("span",{class:"token number"},"97"),e("\n"),a("span",{class:"token char string"},"'a'"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"ord"),e(),a("span",{class:"token string"},'"abcdefgh"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"97"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"98"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"99"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"101"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"102"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"103"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"104"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),uc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"encode"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"encode"),e(),a("span",{class:"token hvariable"},"shift"),e(),a("span",{class:"token hvariable"},"msg"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"ords"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"ord"),e(),a("span",{class:"token hvariable"},"msg"),e("\n "),a("span",{class:"token hvariable"},"shifted"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"shift"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"ords"),e("\n "),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"chr"),e(),a("span",{class:"token hvariable"},"shifted"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),ic=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"decode"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"decode"),e(),a("span",{class:"token hvariable"},"shift"),e(),a("span",{class:"token hvariable"},"msg"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"encode"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"negate"),e(),a("span",{class:"token hvariable"},"shift"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"msg"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),kc=a("h4",{id:"data-map"},[a("a",{class:"header-anchor",href:"#data-map","aria-hidden":"true"},"#"),e(" Data Map")],-1),bc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"snd"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"head"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),mc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token keyword"},"then"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"v"),e("\n "),a("span",{class:"token keyword"},"else"),e("\n "),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),hc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),dc=a("p",null,[a("strong",null,"fromList"),e(" 取一个关联列表,返回一个与之等价的 Map。")],-1),vc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),e(","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token hvariable"},"v"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),gc=a("p",null,"若其中存在重复的键,就将其忽略.",-1),yc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.empty"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),fc=a("p",null,"insert",-1),wc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token number"},"600"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token number"},"200"),e(),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"100"),e(" "),a("span",{class:"token hvariable"},"Map.empty"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"200"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"600"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token number"},"600"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token number"},"200"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.empty"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"200"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"600"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),xc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"fromList"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"Map.empty"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Sc=a("p",null,"null",-1),Lc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token hvariable"},"Map.empty"),e("\n"),a("span",{class:"token constant"},"True"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Mc=a("p",null,"size",-1),Fc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.size"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"5"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Tc=a("p",null,"singleton",-1),Ic=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.singleton"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"9"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token number"},"9"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.singleton"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"9"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ac=a("p",null,"lookup",-1),Cc=a("p",null,"member",-1),Oc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.member"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.member"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Nc=a("p",null,[a("strong",null,"map"),e(" 与 "),a("strong",null,"filter"),e(" 与其对应的 "),a("code",null,"List"),e(" 版本相似")],-1),Bc=a("p",null,[a("code",null,"toList"),e(" 是 "),a("code",null,"fromList"),e(" 的反函数")],-1),Wc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.toList"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"9"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.singleton"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ec=a("p",null,[a("strong",null,"keys"),e(" 与 "),a("strong",null,"elems")],-1),$c=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"phoneBook"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"betty"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"555-2938"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"betty"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"342-2492"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"bonnie"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"452-2928"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"patsy"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"493-2928"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"patsy"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"943-2929"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"patsy"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"827-9162"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"lucille"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"205-2928"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"wendy"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"939-8282"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"penny"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"853-2492"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"penny"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"555-2111"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),Pc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Map.fromListWith"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"number1"),e(),a("span",{class:"token hvariable"},"number2"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"number1"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'", "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"number2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),qc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token string"},'"patsy"'),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"phoneBook"),e("\n"),a("span",{class:"token string"},'"827-9162, 943-2929, 493-2928"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token string"},'"wendy"'),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"phoneBook"),e("\n"),a("span",{class:"token string"},'"939-8282"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token string"},'"betty"'),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"phoneBook"),e("\n"),a("span",{class:"token string"},'"342-2492,555-2938"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Rc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),e(","),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Map.fromListWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"xs"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token string"},'"patsy"'),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"phoneBook"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"827-9162"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"943-2929"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"493-2928"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),zc=a("p",null,"在遇到重复元素时,单选最大的那个值.",-1),Dc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.fromListWith"),e(),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Jc=a("p",null,"将相同键的值都加在一起.",-1),Gc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.fromListWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"108"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"62"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"37"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),_c=a("p",null,"insertWith",-1),Hc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.insertWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"103"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"339"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"104"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"103"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"339"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Kc=a("h4",{id:"data-set"},[a("a",{class:"header-anchor",href:"#data-set","aria-hidden":"true"},"#"),e(" Data Set")],-1),Yc=a("p",null,"内部元素排序且唯一",-1),Vc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.Set "),a("span",{class:"token keyword"},"as"),e(" Set")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Uc=a("p",null,"fromList",-1),Zc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"set1"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token hvariable"},"text1"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"set2"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token hvariable"},"text2"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"set1"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'" .?AIRadefhijlmnorstuy"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"set2"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'" !Tabcdefghilmnorstuvwy"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),jc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.intersection"),e(),a("span",{class:"token hvariable"},"set1"),e(),a("span",{class:"token hvariable"},"set2"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'" adefhilmnorstuy"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Qc=a("p",null,[a("strong",null,"difference"),e(" 差集")],-1),Xc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.difference"),e(),a("span",{class:"token hvariable"},"set1"),e(),a("span",{class:"token hvariable"},"set2"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'".?AIRj"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.difference"),e(),a("span",{class:"token hvariable"},"set2"),e(),a("span",{class:"token hvariable"},"set1"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'"!Tbcgvw"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),nr=a("p",null,[a("code",null,"union"),e(" 并集")],-1),sr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.union"),e(),a("span",{class:"token hvariable"},"set1"),e(),a("span",{class:"token hvariable"},"set2"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'" !.?AIRTabcdefghijlmnorstuvwy"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ar=a("p",null,[a("code",null,"null"),e(","),a("code",null,"size"),e(","),a("code",null,"member"),e(","),a("code",null,"empty"),e(","),a("code",null,"singleton"),e(","),a("code",null,"insert"),e(","),a("code",null,"delete")],-1),er=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Set"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token hvariable"},"Set.empty"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Set"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.size"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.singleton"),e(),a("span",{class:"token number"},"9"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.insert"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.insert"),e(),a("span",{class:"token number"},"8"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.delete"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),tr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`Set.isSubsetOf`"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`Set.isSubsetOf`"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`Set.isProperSubsetOf`"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`Set.isSubsetOf`"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),lr=a("p",null,[e("执行 "),a("code",null,"map"),e(" 和 "),a("code",null,"filter"),e(":")],-1),or=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Set"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Set"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),pr=a("ul",null,[a("li",null,"删除重复元素")],-1),cr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"setNub"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Set.toList"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token hvariable"},"xs"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"setNub"),e(),a("span",{class:"token string"},'"HEY WHATS CRACKALACKIN"'),e("\n"),a("span",{class:"token string"},'" ACEHIKLNRSTWY"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"nub"),e(),a("span",{class:"token string"},'"HEY WHATS CRACKALACKIN"'),e("\n"),a("span",{class:"token string"},'"HEY WATSCRKLIN"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),rr=a("h2",{id:"输入与输出"},[a("a",{class:"header-anchor",href:"#输入与输出","aria-hidden":"true"},"#"),e(" 输入与输出")],-1),ur=a("h3",{id:"io-action"},[a("a",{class:"header-anchor",href:"#io-action","aria-hidden":"true"},"#"),e(" IO action")],-1),ir=a("p",null,[a("code",null,"name <- IO action"),e(": 将 action 绑定至名字上,IO String -> String")],-1),kr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getLine"),e("\n"),a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token constant"},"String"),e("\n\n"),a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token constant"},"String"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),br=a("p",null,[e("在一个"),a("code",null,"do block"),e("中,最后一个"),a("code",null,"action"),e("不能绑定任何名字,它会被绑定成为"),a("code",null,"do block"),e("的结果值.")],-1),mr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token string"},'"Hello, what\'s your name?"'),e("\n "),a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getLine"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Hey "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'", you rock!"'),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),hr=a("h4",{id:"return"},[a("a",{class:"header-anchor",href:"#return","aria-hidden":"true"},"#"),e(" return")],-1),dr=a("ul",null,[a("li",null,"return 功能:将 pure value 包成 I/O actions,不会终止函数/程序"),a("li",null,[e("return 作用: "),a("ul",null,[a("li",null,"if condition then I/O action else I/O action"),a("li",null,[e("改变"),a("code",null,"do block形成的I/O action"),e("的结果值: otherIOAction -> return pureValue")])])])],-1),vr=a("blockquote",null,[a("p",null,'e.g return "haha" - String -> IO String')],-1),gr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"line"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getLine"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token hvariable"},"line"),e("\n "),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"reverseWords"),e(),a("span",{class:"token hvariable"},"line"),e("\n "),a("span",{class:"token hvariable"},"main"),e("\n\n"),a("span",{class:"token hvariable"},"reverseWords"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"reverseWords"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"unwords"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"reverse"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"words"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),yr=a("h3",{id:"command-line"},[a("a",{class:"header-anchor",href:"#command-line","aria-hidden":"true"},"#"),e(" Command Line")],-1),fr=a("blockquote",null,[a("p",null,"System.Environment")],-1),wr=a("ul",null,[a("li",null,[e("getArgs: "),a("code",null,"getArgs :: IO [String]")]),a("li",null,[e("getProgName: "),a("code",null,"getProgName :: IO String")])],-1),xr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.Environment")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"args"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"getArgs"),e("\n "),a("span",{class:"token hvariable"},"progName"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"getProgName"),e("\n "),a("span",{class:"token builtin"},"mapM"),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token hvariable"},"args"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token hvariable"},"progName"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),Sr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.Environment")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.Directory")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e("\n\n"),a("span",{class:"token hvariable"},"dispatch"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"dispatch"),e(),a("span",{class:"token operator"},"="),e(" "),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"add"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"add"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"view"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"view"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"remove"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"remove"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"command"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"args"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"getArgs"),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"action"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token hvariable"},"command"),e(),a("span",{class:"token hvariable"},"dispatch"),e("\n "),a("span",{class:"token hvariable"},"action"),e(),a("span",{class:"token hvariable"},"args"),e("\n\n"),a("span",{class:"token hvariable"},"add"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"add"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"fileName"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"todoItem"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"appendFile"),e(),a("span",{class:"token hvariable"},"fileName"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"todoItem"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'"\\n"'),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"view"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"view"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"fileName"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"readFile"),e(),a("span",{class:"token hvariable"},"fileName"),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"todoTasks"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token hvariable"},"contents"),e("\n "),a("span",{class:"token hvariable"},"numberedTasks"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"zipWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token hvariable"},"line"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" - "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"line"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token hvariable"},"todoTasks"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"unlines"),e(),a("span",{class:"token hvariable"},"numberedTasks"),e("\n\n"),a("span",{class:"token hvariable"},"remove"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"remove"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"fileName"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"numberString"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"openFile"),e(),a("span",{class:"token hvariable"},"fileName"),e(),a("span",{class:"token constant"},"ReadMode"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"tempName"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"tempHandle"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"openTempFile"),e(),a("span",{class:"token string"},'"."'),e(),a("span",{class:"token string"},'"temp"'),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"hGetContents"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"number"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"read"),e(),a("span",{class:"token hvariable"},"numberString"),e("\n "),a("span",{class:"token hvariable"},"todoTasks"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token hvariable"},"contents"),e("\n "),a("span",{class:"token hvariable"},"newTodoItems"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"todoTasks"),e(),a("span",{class:"token operator"},"!!"),e(),a("span",{class:"token hvariable"},"number"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"todoTasks"),e("\n "),a("span",{class:"token hvariable"},"hPutStr"),e(),a("span",{class:"token hvariable"},"tempHandle"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"unlines"),e(),a("span",{class:"token hvariable"},"newTodoItems"),e("\n "),a("span",{class:"token hvariable"},"hClose"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token hvariable"},"hClose"),e(),a("span",{class:"token hvariable"},"tempHandle"),e("\n "),a("span",{class:"token hvariable"},"removeFile"),e(),a("span",{class:"token hvariable"},"fileName"),e("\n "),a("span",{class:"token hvariable"},"renameFile"),e(),a("span",{class:"token hvariable"},"tempName"),e(),a("span",{class:"token hvariable"},"fileName"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br")])],-1),Lr=a("h3",{id:"bytestring"},[a("a",{class:"header-anchor",href:"#bytestring","aria-hidden":"true"},"#"),e(" ByteString")],-1),Mr=a("ul",null,[a("li",null,[a("code",null,"Data.ByteString")]),a("li",null,[a("code",null,"Data.ByteString.Lazy")])],-1),Fr=a("p",null,"lazy byteStrings 像装了一堆大小为 64K 的 strict byteStrings 的 list",-1),Tr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.ByteString.Lazy "),a("span",{class:"token keyword"},"as"),e(" B")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.ByteString "),a("span",{class:"token keyword"},"as"),e(" S")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ir=a("ul",null,[a("li",null,"pack/unpack"),a("li",null,"fromChunks/toChunks"),a("li",null,"cons/empty/head/tail/init/null/length/map/reverse/foldl/foldr/concat/takeWhile/filter")],-1),Ar=a("h3",{id:"常用输入输出函数"},[a("a",{class:"header-anchor",href:"#常用输入输出函数","aria-hidden":"true"},"#"),e(" 常用输入输出函数")],-1),Cr=a("h4",{id:"输出"},[a("a",{class:"header-anchor",href:"#输出","aria-hidden":"true"},"#"),e(" 输出")],-1),Or=a("h5",{id:"output-string"},[a("a",{class:"header-anchor",href:"#output-string","aria-hidden":"true"},"#"),e(" Output String")],-1),Nr=a("p",null,[a("code",null,"putChar"),e("/"),a("code",null,"putStr"),e("/"),a("code",null,"putStrLn"),e(":")],-1),Br=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putChar"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Wr=a("h5",{id:"print"},[a("a",{class:"header-anchor",href:"#print","aria-hidden":"true"},"#"),e(" print")],-1),Er=a("p",null,"print = putStrLn . show",-1),$r=a("h5",{id:"file-system-io"},[a("a",{class:"header-anchor",href:"#file-system-io","aria-hidden":"true"},"#"),e(" File System IO")],-1),Pr=a("ul",null,[a("li",null,[a("code",null,"hPutStr")]),a("li",null,[a("code",null,"hPutStrLn")])],-1),qr=a("h6",{id:"write-file"},[a("a",{class:"header-anchor",href:"#write-file","aria-hidden":"true"},"#"),e(" Write File")],-1),Rr=a("p",null,[a("code",null,"writefile :: FilePath -> String -> IO ()"),e(" - WriteMode, not AppendMode")],-1),zr=a("h6",{id:"append-file"},[a("a",{class:"header-anchor",href:"#append-file","aria-hidden":"true"},"#"),e(" Append File")],-1),Dr=a("h4",{id:"输入"},[a("a",{class:"header-anchor",href:"#输入","aria-hidden":"true"},"#"),e(" 输入")],-1),Jr=a("h5",{id:"getchar-io-char"},[a("a",{class:"header-anchor",href:"#getchar-io-char","aria-hidden":"true"},"#"),e(" getChar :: IO Char")],-1),Gr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getChar"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"/="),e(),a("span",{class:"token char string"},"' '"),e("\n "),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putChar"),e(),a("span",{class:"token hvariable"},"c"),e("\n "),a("span",{class:"token hvariable"},"main"),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),_r=a("h5",{id:"get-contents"},[a("a",{class:"header-anchor",href:"#get-contents","aria-hidden":"true"},"#"),e(" Get Contents")],-1),Hr=a("p",null,"getContents :: IO String (Lazy I/O) - 内容暂存在文件,需要使用时读取至内存区",-1),Kr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.Char")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getContents"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"toUpper"),e(),a("span",{class:"token hvariable"},"contents"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Yr=a("h5",{id:"file"},[a("a",{class:"header-anchor",href:"#file","aria-hidden":"true"},"#"),e(" File")],-1),Vr=a("h6",{id:"handle"},[a("a",{class:"header-anchor",href:"#handle","aria-hidden":"true"},"#"),e(" handle")],-1),Ur=a("ul",null,[a("li",null,[a("p",null,[a("code",null,"data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode")])]),a("li",null,[a("p",null,[a("code",null,"openFile :: FilePath(String) -> IOMode -> IO Handle")])]),a("li",null,[a("p",null,[a("code",null,"hGetContents :: Handle -> IO String")])]),a("li",null,[a("p",null,[a("code",null,"hClose :: Handle -> IO ()")])]),a("li",null,[a("p",null,[a("code",null,"hGetChar")])]),a("li",null,[a("p",null,[a("code",null,"hGetLine")])])],-1),Zr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"openFile"),e(),a("span",{class:"token string"},'"girlfriend.txt"'),e(),a("span",{class:"token constant"},"ReadMode"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"hGetContents"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token hvariable"},"contents"),e("\n "),a("span",{class:"token hvariable"},"hClose"),e(),a("span",{class:"token hvariable"},"handle"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),jr=a("h6",{id:"with-file"},[a("a",{class:"header-anchor",href:"#with-file","aria-hidden":"true"},"#"),e(" With File")],-1),Qr=a("p",null,[a("code",null,"withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a")],-1),Xr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"withFile"),e(),a("span",{class:"token string"},'"girlfriend.txt"'),e(),a("span",{class:"token constant"},"ReadMode"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"hGetContents"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token hvariable"},"contents"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),nu=a("h6",{id:"read-file"},[a("a",{class:"header-anchor",href:"#read-file","aria-hidden":"true"},"#"),e(" Read File")],-1),su=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"readFile"),e(),a("span",{class:"token string"},'"girlfriend.txt"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),au=a("h4",{id:"action"},[a("a",{class:"header-anchor",href:"#action","aria-hidden":"true"},"#"),e(" Action")],-1),eu=a("h5",{id:"when"},[a("a",{class:"header-anchor",href:"#when","aria-hidden":"true"},"#"),e(" When")],-1),tu=a("p",null,"Control.Monad.when :: (Applicative f) => Bool -> f () -> f ()",-1),lu=a("p",null,[a("code",null,"when bool表达式 I/O-Action"),e(" - 真时返回 Action,假时"),a("code",null,"return ()")],-1),ou=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Control.Monad")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getChar"),e("\n "),a("span",{class:"token hvariable"},"when"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"/="),e(),a("span",{class:"token char string"},"' '"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putChar"),e(),a("span",{class:"token hvariable"},"c"),e("\n "),a("span",{class:"token hvariable"},"main"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),pu=a("h5",{id:"sequence"},[a("a",{class:"header-anchor",href:"#sequence","aria-hidden":"true"},"#"),e(" sequence")],-1),cu=a("p",null,[e("sequence :: "),a("code",null,"[IO a]"),e(" -> IO "),a("code",null,"[a]")],-1),ru=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"rs"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"sequence"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token builtin"},"getLine"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token builtin"},"getLine"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token builtin"},"getLine"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token builtin"},"print"),e(),a("span",{class:"token hvariable"},"rs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),uu=a("h5",{id:"map"},[a("a",{class:"header-anchor",href:"#map","aria-hidden":"true"},"#"),e(" Map")],-1),iu=a("p",null,"mapM, Control.Monad.forM:",-1),ku=a("p",null,"= sequence . map",-1),bu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"mapM"),e(),a("span",{class:"token builtin"},"print"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"1"),e("\n"),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"mapM_"),e(),a("span",{class:"token builtin"},"print"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"1"),e("\n"),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token number"},"3"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),mu=a("h5",{id:"control-monad-forever"},[a("a",{class:"header-anchor",href:"#control-monad-forever","aria-hidden":"true"},"#"),e(" Control Monad forever")],-1),hu=a("p",null,"接受一个 I/O action 并回传一个永远作同一件事的 I/O action",-1),du=a("p",null,"以下代码实现了循环结构:",-1),vu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Control.Monad")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.Char")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"forever"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token string"},'"Give me some input: "'),e("\n "),a("span",{class:"token hvariable"},"l"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getLine"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"toUpper"),e(),a("span",{class:"token hvariable"},"l"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),gu=a("h5",{id:"hsetbuffering"},[a("a",{class:"header-anchor",href:"#hsetbuffering","aria-hidden":"true"},"#"),e(" hSetBuffering")],-1),yu=a("p",null,[a("code",null,"data BufferMode = NoBuffering | LineBuffering | BlockBuffering (Maybe Int)"),a("code",null,"hSetBuffering :: Handle -> BufferMode -> IO ()")],-1),fu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"withFile"),e(),a("span",{class:"token string"},'"something.txt"'),e(),a("span",{class:"token constant"},"ReadMode"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"hSetBuffering"),e(),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token constant"},"BlockBuffering"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"2048"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"hGetContents"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token hvariable"},"contents"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),wu=a("h5",{id:"hflush"},[a("a",{class:"header-anchor",href:"#hflush","aria-hidden":"true"},"#"),e(" hFlush")],-1),xu=a("h4",{id:"other"},[a("a",{class:"header-anchor",href:"#other","aria-hidden":"true"},"#"),e(" Other")],-1),Su=a("h5",{id:"lines"},[a("a",{class:"header-anchor",href:"#lines","aria-hidden":"true"},"#"),e(" lines")],-1),Lu=a("p",null,[a("code",null,"lines :: String -> [String]"),e(" - 按换行符将段落切割成句子")],-1),Mu=a("h5",{id:"interact"},[a("a",{class:"header-anchor",href:"#interact","aria-hidden":"true"},"#"),e(" interact")],-1),Fu=a("ul",null,[a("li",null,"arguments: String -> String 的函数"),a("li",null,"return: 一个 I/O action"),a("li",null,"function: I/O action 会读取输入,调用提供的函数,然后把函数的结果打印出来")],-1),Tu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"interact"),e(),a("span",{class:"token hvariable"},"shortLinesOnly"),e("\n\n"),a("span",{class:"token hvariable"},"shortLinesOnly"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"shortLinesOnly"),e(),a("span",{class:"token hvariable"},"input"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"allLines"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token hvariable"},"input"),e("\n "),a("span",{class:"token hvariable"},"shortLines"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"line"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"line"),e(),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"allLines"),e("\n "),a("span",{class:"token hvariable"},"result"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"unlines"),e(),a("span",{class:"token hvariable"},"shortLines"),e("\n "),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token hvariable"},"result"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),Iu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"interact"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"unlines"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"length"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"lines"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Au=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"respondPalindromes"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"unlines"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"->"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"isPalindrome"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token string"},'"palindrome"'),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token string"},'"not a palindrome"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"lines"),e("\n "),a("span",{class:"token keyword"},"where"),e(),a("span",{class:"token hvariable"},"isPalindrome"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token builtin"},"reverse"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Cu=a("h5",{id:"system-directory"},[a("a",{class:"header-anchor",href:"#system-directory","aria-hidden":"true"},"#"),e(" System Directory")],-1),Ou=a("ul",null,[a("li",null,"removeFile"),a("li",null,"renameFile")],-1),Nu=a("h2",{id:"异常"},[a("a",{class:"header-anchor",href:"#异常","aria-hidden":"true"},"#"),e(" 异常")],-1),Bu=a("p",null,"System.IO.Error",-1),Wu=a("h3",{id:"catch"},[a("a",{class:"header-anchor",href:"#catch","aria-hidden":"true"},"#"),e(" catch")],-1),Eu=a("p",null,"catch :: IO a -> (IOError -> IO a) -> IO a",-1),$u=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.Environment")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO.Error")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"toTry"),e(" `"),a("span",{class:"token builtin"},"catch"),e("` "),a("span",{class:"token hvariable"},"handler"),e("\n\n"),a("span",{class:"token hvariable"},"toTry"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"toTry"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"fileName"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"getArgs"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"readFile"),e(),a("span",{class:"token hvariable"},"fileName"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token string"},'"The file has "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token hvariable"},"contents"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" lines!"'),e("\n\n"),a("span",{class:"token hvariable"},"handler"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"IOError"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"handler"),e(),a("span",{class:"token hvariable"},"e"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"isDoesNotExistError"),e(),a("span",{class:"token hvariable"},"e"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token string"},'"The file doesn\'t exist!"'),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"isFullError"),e(),a("span",{class:"token hvariable"},"e"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"freeSomeSpace"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"isIllegalOperation"),e(),a("span",{class:"token hvariable"},"e"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"notifyCops"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"ioError"),e(),a("span",{class:"token hvariable"},"e"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),Pu=a("h2",{id:"advanced-monad"},[a("a",{class:"header-anchor",href:"#advanced-monad","aria-hidden":"true"},"#"),e(" Advanced Monad")],-1),qu=a("h3",{id:"writer-monad"},[a("a",{class:"header-anchor",href:"#writer-monad","aria-hidden":"true"},"#"),e(" Writer Monad")],-1),Ru=a("p",null,"Writer 可以让我们在计算的同时搜集所有 log 纪录,并汇集成一个 log 并附加在结果上",-1),zu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"applyLog"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"applyLog"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token builtin"},"log"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"newLog"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),a("span",{class:"token builtin"},"log"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"newLog"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Du=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"30"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"A freaking platoon."'),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`applyLog`"),e(),a("span",{class:"token hvariable"},"isBigGang"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"A freaking platoon.Compared gang size to 9"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"BathCat"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"Got outlaw name."'),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`applyLog`"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"Applied length"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"Got outlaw name.Applied length"'),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ju=a("h4",{id:"control-monad-writer"},[a("a",{class:"header-anchor",href:"#control-monad-writer","aria-hidden":"true"},"#"),e(" Control Monad Writer")],-1),Gu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"mempty"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"v"),e("'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"v"),e("'"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),_u=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Control.Monad.Writer")]),e("\n\n"),a("span",{class:"token hvariable"},"logNumber"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"logNumber"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"Got number: "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"multiWithLog"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"multiWithLog"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"logNumber"),e(),a("span",{class:"token number"},"3"),e("\n "),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"logNumber"),e(),a("span",{class:"token number"},"5"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token operator"},"*"),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),Hu=a("h3",{id:"reader-monad"},[a("a",{class:"header-anchor",href:"#reader-monad","aria-hidden":"true"},"#"),e(" Reader Monad")],-1),Ku=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"->"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"r"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"w"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"w"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Yu=a("h3",{id:"state-monad"},[a("a",{class:"header-anchor",href:"#state-monad","aria-hidden":"true"},"#"),e(" State Monad")],-1),Vu=a("h4",{id:"control-monad-state"},[a("a",{class:"header-anchor",href:"#control-monad-state","aria-hidden":"true"},"#"),e(" Control Monad State")],-1),Uu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"newtype"),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"runState"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token hvariable"},"h"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"newState"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token hvariable"},"s"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token hvariable"},"g"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token keyword"},"in"),e(" "),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"newState"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Zu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"get"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"put"),e(),a("span",{class:"token hvariable"},"newState"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"newState"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ju=a("h4",{id:"state-monad-case"},[a("a",{class:"header-anchor",href:"#state-monad-case","aria-hidden":"true"},"#"),e(" State Monad Case")],-1),Qu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Control.Monad.State")]),e("\n\n"),a("span",{class:"token hvariable"},"pop"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"pop"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"a"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"stackManipulation"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"stackManipulation"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token number"},"3"),e("\n "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"pop"),e("\n "),a("span",{class:"token hvariable"},"pop"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br")])],-1),Xu=a("h3",{id:"error-monad"},[a("a",{class:"header-anchor",href:"#error-monad","aria-hidden":"true"},"#"),e(" Error Monad")],-1),ni=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Error"),e(),a("span",{class:"token hvariable"},"e"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Either"),e(),a("span",{class:"token hvariable"},"e"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Right"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token constant"},"Right"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token hvariable"},"err"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token hvariable"},"err"),e("\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token hvariable"},"msg"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"strMsg"),e(),a("span",{class:"token hvariable"},"msg"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),si=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"strMsg"),e("\n"),a("span",{class:"token hvariable"},"strMsg"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Error"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"strMsg"),e(),a("span",{class:"token string"},'"boom!"'),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token string"},'"boom!"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ai=a("h4",{id:"liftm"},[a("a",{class:"header-anchor",href:"#liftm","aria-hidden":"true"},"#"),e(" liftM")],-1),ei=a("h4",{id:"join"},[a("a",{class:"header-anchor",href:"#join","aria-hidden":"true"},"#"),e(" join")],-1),ti=a("h4",{id:"filterm"},[a("a",{class:"header-anchor",href:"#filterm","aria-hidden":"true"},"#"),e(" filterM")],-1),li=a("h4",{id:"foldm"},[a("a",{class:"header-anchor",href:"#foldm","aria-hidden":"true"},"#"),e(" foldM")],-1),oi=a("h4",{id:"组合函数"},[a("a",{class:"header-anchor",href:"#组合函数","aria-hidden":"true"},"#"),e(),a("code",null,"<=<"),e("(组合函数)")],-1),pi=a("h3",{id:"self-defined-monad"},[a("a",{class:"header-anchor",href:"#self-defined-monad","aria-hidden":"true"},"#"),e(" Self-Defined Monad")],-1),ci=a("h2",{id:"注释"},[a("a",{class:"header-anchor",href:"#注释","aria-hidden":"true"},"#"),e(" 注释")],-1),ri=a("h3",{id:"符号-1"},[a("a",{class:"header-anchor",href:"#符号-1","aria-hidden":"true"},"#"),e(" 符号")],-1),ui=a("ul",null,[a("li",null,[a("code",null,"--"),e(" 单行注释")]),a("li",null,[a("code",null,"{- -}"),e(" 块注释")]),a("li",null,[a("code",null,"{-# #-}"),e(" (文件头部)编译器参数")])],-1),ii=a("h2",{id:"reference"},[a("a",{class:"header-anchor",href:"#reference","aria-hidden":"true"},"#"),e(" Reference")],-1),ki=a("ul",null,[a("li",null,[a("a",{href:"https://github.com/MnO2/learnyouahaskell-zh",target:"_blank",rel:"noopener noreferrer"},"Haskell Book")])],-1);l.render=function(a,e,t,l,bi,mi){return n(),s("div",null,[o,p,c,r,u,i,k,b,m,h,d,v,g,y,f,w,x,S,L,M,F,T,I,A,C,O,N,B,W,E,$,P,q,R,z,D,J,G,_,H,K,Y,V,U,Z,j,Q,X,nn,sn,an,en,tn,ln,on,pn,cn,rn,un,kn,bn,mn,hn,dn,vn,gn,yn,fn,wn,xn,Sn,Ln,Mn,Fn,Tn,In,An,Cn,On,Nn,Bn,Wn,En,$n,Pn,qn,Rn,zn,Dn,Jn,Gn,_n,Hn,Kn,Yn,Vn,Un,Zn,jn,Qn,Xn,ns,ss,as,es,ts,ls,os,ps,cs,rs,us,is,ks,bs,ms,hs,ds,vs,gs,ys,fs,ws,xs,Ss,Ls,Ms,Fs,Ts,Is,As,Cs,Os,Ns,Bs,Ws,Es,$s,Ps,qs,Rs,zs,Ds,Js,Gs,_s,Hs,Ks,Ys,Vs,Us,Zs,js,Qs,Xs,na,sa,aa,ea,ta,la,oa,pa,ca,ra,ua,ia,ka,ba,ma,ha,da,va,ga,ya,fa,wa,xa,Sa,La,Ma,Fa,Ta,Ia,Aa,Ca,Oa,Na,Ba,Wa,Ea,$a,Pa,qa,Ra,za,Da,Ja,Ga,_a,Ha,Ka,Ya,Va,Ua,Za,ja,Qa,Xa,ne,se,ae,ee,te,le,oe,pe,ce,re,ue,ie,ke,be,me,he,de,ve,ge,ye,fe,we,xe,Se,Le,Me,Fe,Te,Ie,Ae,Ce,Oe,Ne,Be,We,Ee,$e,Pe,qe,Re,ze,De,Je,Ge,_e,He,Ke,Ye,Ve,Ue,Ze,je,Qe,Xe,nt,st,at,et,tt,lt,ot,pt,ct,rt,ut,it,kt,bt,mt,ht,dt,vt,gt,yt,ft,wt,xt,St,Lt,Mt,Ft,Tt,It,At,Ct,Ot,Nt,Bt,Wt,Et,$t,Pt,qt,Rt,zt,Dt,Jt,Gt,_t,Ht,Kt,Yt,Vt,Ut,Zt,jt,Qt,Xt,nl,sl,al,el,tl,ll,ol,pl,cl,rl,ul,il,kl,bl,ml,hl,dl,vl,gl,yl,fl,wl,xl,Sl,Ll,Ml,Fl,Tl,Il,Al,Cl,Ol,Nl,Bl,Wl,El,$l,Pl,ql,Rl,zl,Dl,Jl,Gl,_l,Hl,Kl,Yl,Vl,Ul,Zl,jl,Ql,Xl,no,so,ao,eo,to,lo,oo,po,co,ro,uo,io,ko,bo,mo,ho,vo,go,yo,fo,wo,xo,So,Lo,Mo,Fo,To,Io,Ao,Co,Oo,No,Bo,Wo,Eo,$o,Po,qo,Ro,zo,Do,Jo,Go,_o,Ho,Ko,Yo,Vo,Uo,Zo,jo,Qo,Xo,np,sp,ap,ep,tp,lp,op,pp,cp,rp,up,ip,kp,bp,mp,hp,dp,vp,gp,yp,fp,wp,xp,Sp,Lp,Mp,Fp,Tp,Ip,Ap,Cp,Op,Np,Bp,Wp,Ep,$p,Pp,qp,Rp,zp,Dp,Jp,Gp,_p,Hp,Kp,Yp,Vp,Up,Zp,jp,Qp,Xp,nc,sc,ac,ec,tc,lc,oc,pc,cc,rc,uc,ic,kc,bc,mc,hc,dc,vc,gc,yc,fc,wc,xc,Sc,Lc,Mc,Fc,Tc,Ic,Ac,Cc,Oc,Nc,Bc,Wc,Ec,$c,Pc,qc,Rc,zc,Dc,Jc,Gc,_c,Hc,Kc,Yc,Vc,Uc,Zc,jc,Qc,Xc,nr,sr,ar,er,tr,lr,or,pr,cr,rr,ur,ir,kr,br,mr,hr,dr,vr,gr,yr,fr,wr,xr,Sr,Lr,Mr,Fr,Tr,Ir,Ar,Cr,Or,Nr,Br,Wr,Er,$r,Pr,qr,Rr,zr,Dr,Jr,Gr,_r,Hr,Kr,Yr,Vr,Ur,Zr,jr,Qr,Xr,nu,su,au,eu,tu,lu,ou,pu,cu,ru,uu,iu,ku,bu,mu,hu,du,vu,gu,yu,fu,wu,xu,Su,Lu,Mu,Fu,Tu,Iu,Au,Cu,Ou,Nu,Bu,Wu,Eu,$u,Pu,qu,Ru,zu,Du,Ju,Gu,_u,Hu,Ku,Yu,Vu,Uu,Zu,ju,Qu,Xu,ni,si,ai,ei,ti,li,oi,pi,ci,ri,ui,ii,ki])};export default l;export{t as __pageData}; diff --git a/assets/language_haskell_haskellBasicNotes.md.998a4d73.lean.js b/assets/language_haskell_haskellBasicNotes.md.998a4d73.lean.js new file mode 100644 index 00000000000..a8dc6b533e6 --- /dev/null +++ b/assets/language_haskell_haskellBasicNotes.md.998a4d73.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as e}from"./app.6dd2a1b7.js";const t='{"title":"Haskell Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Platform","slug":"platform"},{"level":3,"title":"ghci","slug":"ghci"},{"level":3,"title":"ghc","slug":"ghc"},{"level":2,"title":"Unique Mark","slug":"unique-mark"},{"level":2,"title":"Expression","slug":"expression"},{"level":2,"title":"Type","slug":"type"},{"level":3,"title":"基本类型","slug":"基本类型"},{"level":3,"title":"List","slug":"list"},{"level":3,"title":"Tuple(原组)","slug":"tuple-原组"},{"level":3,"title":"泛型","slug":"泛型"},{"level":3,"title":"函数类型","slug":"函数类型"},{"level":2,"title":"基本语法","slug":"基本语法"},{"level":3,"title":"名字/函数定义","slug":"名字-函数定义"},{"level":2,"title":"Type Class","slug":"type-class"},{"level":3,"title":"=> 符号","slug":"符号"},{"level":3,"title":"Basic Type Class","slug":"basic-type-class"},{"level":3,"title":"Custom Type Class","slug":"custom-type-class"},{"level":3,"title":"data type","slug":"data-type"},{"level":2,"title":"函数","slug":"函数"},{"level":3,"title":"递归函数","slug":"递归函数"},{"level":3,"title":"高阶函数","slug":"高阶函数"},{"level":3,"title":"常用函数","slug":"常用函数"},{"level":2,"title":"模块","slug":"模块"},{"level":3,"title":"import","slug":"import"},{"level":3,"title":"建立模块","slug":"建立模块"},{"level":3,"title":"常用基础模块","slug":"常用基础模块"},{"level":2,"title":"输入与输出","slug":"输入与输出"},{"level":3,"title":"IO action","slug":"io-action"},{"level":3,"title":"Command Line","slug":"command-line"},{"level":3,"title":"ByteString","slug":"bytestring"},{"level":3,"title":"常用输入输出函数","slug":"常用输入输出函数"},{"level":2,"title":"异常","slug":"异常"},{"level":3,"title":"catch","slug":"catch"},{"level":2,"title":"Advanced Monad","slug":"advanced-monad"},{"level":3,"title":"Writer Monad","slug":"writer-monad"},{"level":3,"title":"Reader Monad","slug":"reader-monad"},{"level":3,"title":"State Monad","slug":"state-monad"},{"level":3,"title":"Error Monad","slug":"error-monad"},{"level":3,"title":"Self-Defined Monad","slug":"self-defined-monad"},{"level":2,"title":"注释","slug":"注释"},{"level":3,"title":"符号","slug":"符号-1"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"language/haskell/haskellBasicNotes.md","lastUpdated":1627196319000}',l={},o=a("h1",{id:"haskell-basic-notes"},[a("a",{class:"header-anchor",href:"#haskell-basic-notes","aria-hidden":"true"},"#"),e(" Haskell Basic Notes")],-1),p=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#haskell-basic-notes"},"Haskell Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#platform"},"Platform")]),a("li",null,[a("a",{href:"#unique-mark"},"Unique Mark")]),a("li",null,[a("a",{href:"#expression"},"Expression")]),a("li",null,[a("a",{href:"#type"},"Type")]),a("li",null,[a("a",{href:"#基本语法"},"基本语法")]),a("li",null,[a("a",{href:"#type-class"},"Type Class")]),a("li",null,[a("a",{href:"#函数"},"函数")]),a("li",null,[a("a",{href:"#模块"},"模块")]),a("li",null,[a("a",{href:"#输入与输出"},"输入与输出")]),a("li",null,[a("a",{href:"#异常"},"异常")]),a("li",null,[a("a",{href:"#advanced-monad"},"Advanced Monad")]),a("li",null,[a("a",{href:"#注释"},"注释")]),a("li",null,[a("a",{href:"#reference"},"Reference")])])])])])],-1),c=a("h2",{id:"platform"},[a("a",{class:"header-anchor",href:"#platform","aria-hidden":"true"},"#"),e(" Platform")],-1),r=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token function"},"apt-get"),e(),a("span",{class:"token function"},"install"),e(" haskell-Platform\n"),a("span",{class:"token function"},"apt-get"),e(),a("span",{class:"token function"},"install"),e(" ghc-mod\natom plugins "),a("span",{class:"token builtin class-name"},":"),e(" language-haskell autocomplete-haskell ide-haskell haskell-ghc-mod\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),u=a("h3",{id:"ghci"},[a("a",{class:"header-anchor",href:"#ghci","aria-hidden":"true"},"#"),e(" ghci")],-1),i=a("ul",null,[a("li",null,":l - load file"),a("li",null,":r - reload file"),a("li",null,":cd"),a("li",null,":edit - $EDITOR"),a("li",null,":m - module"),a("li",null,":q - quit"),a("li",null,":?"),a("li",null,":k - kind"),a("li",null,":t - type function"),a("li",null,":info - data/TypeClass")],-1),k=a("h3",{id:"ghc"},[a("a",{class:"header-anchor",href:"#ghc","aria-hidden":"true"},"#"),e(" ghc")],-1),b=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,"runghc *.hs/*.lhs\n")]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),m=a("h2",{id:"unique-mark"},[a("a",{class:"header-anchor",href:"#unique-mark","aria-hidden":"true"},"#"),e(" Unique Mark")],-1),h=a("p",null,[a("code",null,":+")],-1),d=a("p",null,"复数符 - 2 :+ 3 -> 2+3i",-1),v=a("p",null,[a("code",null,"_")],-1),g=a("p",null,"泛匹配符: 表示不关心此部分具体内容",-1),y=a("p",null,[a("code",null,"<-")],-1),f=a("p",null,"属于符号,用于 ListRange 中.",-1),w=a("p",null,[a("code",null,"=>")],-1),x=a("p",null,"类型约束分隔符",-1),S=a("p",null,[a("code",null,"->")],-1),L=a("h2",{id:"expression"},[a("a",{class:"header-anchor",href:"#expression","aria-hidden":"true"},"#"),e(" Expression")],-1),M=a("p",null,"if 语句也是表达式",-1),F=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"doubleSmallNumber"),e("' "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),T=a("h2",{id:"type"},[a("a",{class:"header-anchor",href:"#type","aria-hidden":"true"},"#"),e(" Type")],-1),I=a("h3",{id:"基本类型"},[a("a",{class:"header-anchor",href:"#基本类型","aria-hidden":"true"},"#"),e(" 基本类型")],-1),A=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token char string"},"'a'"),e("\n"),a("span",{class:"token char string"},"'a'"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Char"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token constant"},"True"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Bool"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token string"},'"HELLO!"'),e("\n"),a("span",{class:"token string"},'"HELLO!"'),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Bool"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),C=a("h4",{id:"int"},[a("a",{class:"header-anchor",href:"#int","aria-hidden":"true"},"#"),e(" Int")],-1),O=a("p",null,"有限整数",-1),N=a("h4",{id:"integer"},[a("a",{class:"header-anchor",href:"#integer","aria-hidden":"true"},"#"),e(" Integer")],-1),B=a("p",null,"无限整数(效率低)",-1),W=a("h4",{id:"float"},[a("a",{class:"header-anchor",href:"#float","aria-hidden":"true"},"#"),e(" Float")],-1),E=a("p",null,"单精度浮点数",-1),$=a("h4",{id:"double"},[a("a",{class:"header-anchor",href:"#double","aria-hidden":"true"},"#"),e(" Double")],-1),P=a("p",null,"双精度浮点数",-1),q=a("h4",{id:"bool"},[a("a",{class:"header-anchor",href:"#bool","aria-hidden":"true"},"#"),e(" Bool")],-1),R=a("p",null,"True/False",-1),z=a("h4",{id:"char"},[a("a",{class:"header-anchor",href:"#char","aria-hidden":"true"},"#"),e(" Char")],-1),D=a("h4",{id:"string"},[a("a",{class:"header-anchor",href:"#string","aria-hidden":"true"},"#"),e(" String")],-1),J=a("h4",{id:"ordering"},[a("a",{class:"header-anchor",href:"#ordering","aria-hidden":"true"},"#"),e(" Ordering")],-1),G=a("p",null,"LT,GT,EQ",-1),_=a("h4",{id:"word"},[a("a",{class:"header-anchor",href:"#word","aria-hidden":"true"},"#"),e(" Word")],-1),H=a("p",null,"Data.Word - unsigned int",-1),K=a("h4",{id:"rational"},[a("a",{class:"header-anchor",href:"#rational","aria-hidden":"true"},"#"),e(" Rational")],-1),Y=a("p",null,"有理数类型,用于高精度数学运算",-1),V=a("h3",{id:"list"},[a("a",{class:"header-anchor",href:"#list","aria-hidden":"true"},"#"),e(" List")],-1),U=a("h4",{id:"operator"},[a("a",{class:"header-anchor",href:"#operator","aria-hidden":"true"},"#"),e(" Operator")],-1),Z=a("ul",null,[a("li",null,"++"),a("li",null,":"),a("li",null,"!!"),a("li",null,">/"),e(),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),en=a("ul",null,[a("li",null,"reverse List 反转"),a("li",null,"take num List 返回 List 前 num 个元素组成的 List")],-1),tn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),ln=a("ul",null,[a("li",null,"drop num List 删除 List 前 num 个元素")],-1),on=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"drop"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"drop"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"drop"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),pn=a("ul",null,[a("li",null,[a("p",null,"maximum List 返回最大元素")]),a("li",null,[a("p",null,"minimum List 返回最小元素")]),a("li",null,[a("p",null,"sum List 返回 List 元素和")]),a("li",null,[a("p",null,"product List 返回 List 元素积")]),a("li",null,[a("p",null,[e("elem "),a("code",null,"elem"),e(" List 判断元素存在性")])])],-1),cn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"4"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"10"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),rn=a("ul",null,[a("li",null,[e("cycle List 返回循环无限数组("),a("em",null,"Haskell 惰性特性"),e(")")]),a("li",null,[e("repeat Elem 返回循环无限数组("),a("em",null,"Haskell 惰性特性"),e(")")]),a("li",null,"replicate num Elem 返回循环无限数组")],-1),un=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"cycle"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"repeat"),e(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),e(" "),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"replicate"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"10"),e(" "),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),kn=a("ul",null,[a("li",null,[e("takeWhile :: (a -> Bool) -> "),a("code",null,"[a]"),e(" -> "),a("code",null,"[a]"),e(" 遇到不符合限制条件的元素便停止遍历 List")])],-1),bn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token number"},"166650"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),mn=a("h4",{id:"range"},[a("a",{class:"header-anchor",href:"#range","aria-hidden":"true"},"#"),e(" Range")],-1),hn=a("p",null,"三要素: , 与 ..",-1),dn=a("ul",null,[a("li",null,"上限"),a("li",null,"下限"),a("li",null,[e("步长("),a("em",null,"仅可标明一次"),e(")")])],-1),vn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[e("上下限"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},"]"),e("\n步长为"),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},"]"),e("\n步长为"),a("span",{class:"token number"},"13"),e("无限"),a("span",{class:"token constant"},"List"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"13"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"26"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"24"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"13"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"26"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),gn=a("h4",{id:"list-comprehension-数学定义"},[a("a",{class:"header-anchor",href:"#list-comprehension-数学定义","aria-hidden":"true"},"#"),e(" List Comprehension(数学定义)")],-1),yn=a("p",null,"由类似集合定义的离散数学定义,来定义复杂的 List:",-1),fn=a("p",null,[a("code",null,"[expression | filter]")],-1),wn=a("p",null,[a("code",null,"[expression | x <\\- Range, Predicate(断言/限制条件)]")],-1),xn=a("ul",null,[a("li",null,[e("Range: "),a("code",null,","),e("分隔多个 Range(一般为 List)")]),a("li",null,[e("Predicate: "),a("code",null,","),e("分隔多个断言;每个断言均为 Boolean 表达式")])],-1),Sn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},">="),e(),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"14"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"18"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"50"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"x"),e(" `"),a("span",{class:"token builtin"},"mod"),e("` "),a("span",{class:"token number"},"7"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"52"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"59"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"66"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"73"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"80"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"87"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"94"),a("span",{class:"token punctuation"},"]"),e("\n\n\n"),a("span",{class:"token hvariable"},"boomBangs"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token string"},'"BOOM!"'),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token string"},'"BANG!"'),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"boomBangs"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"13"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"BOOM!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"BOOM!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"BANG!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"BANG!"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Ln=a("ul",null,[a("li",null,"多个 Range")],-1),Mn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"40"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"50"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"55"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"80"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"110"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Fn=a("ul",null,[a("li",null,"嵌套 Comprehension")],-1),Tn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"xxs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token builtin"},"even"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xxs"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),In=a("h3",{id:"tuple-原组"},[a("a",{class:"header-anchor",href:"#tuple-原组","aria-hidden":"true"},"#"),e(" Tuple(原组)")],-1),An=a("h4",{id:"内部差异性"},[a("a",{class:"header-anchor",href:"#内部差异性","aria-hidden":"true"},"#"),e(" 内部差异性")],-1),Cn=a("ul",null,[a("li",null,"同一 Tuple 里可存放不同 Type 的项")],-1),On=a("h4",{id:"外部差异性"},[a("a",{class:"header-anchor",href:"#外部差异性","aria-hidden":"true"},"#"),e(" 外部差异性")],-1),Nn=a("ul",null,[a("li",null,[e("数目不同或某项不同的 Tuple 属于不同 Type "),a("ul",null,[a("li",null,"不可置于同一 List 中"),a("li",null,"不同长度的 Tuple 不可比较(比较符只可用于相同 Type)")])])],-1),Bn=a("h4",{id:"tuple-function"},[a("a",{class:"header-anchor",href:"#tuple-function","aria-hidden":"true"},"#"),e(" Tuple Function")],-1),Wn=a("h5",{id:"二元组"},[a("a",{class:"header-anchor",href:"#二元组","aria-hidden":"true"},"#"),e(" 二元组")],-1),En=a("p",null,"fst/snd tuple 返回首项/尾项",-1),$n=a("p",null,"zip List1 List2 对应项配对,组成二元组 List",-1),Pn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zip"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"im"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"a"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"turtle"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"im"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"a"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"turtle"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zip"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"apple"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"orange"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"cherry"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"mango"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"apple"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"orange"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"cherry"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"mango"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),qn=a("h5",{id:"三元组"},[a("a",{class:"header-anchor",href:"#三元组","aria-hidden":"true"},"#"),e(" 三元组")],-1),Rn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"first"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"first"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e("\n\n"),a("span",{class:"token hvariable"},"second"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n"),a("span",{class:"token hvariable"},"second"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"y"),e("\n\n"),a("span",{class:"token hvariable"},"third"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),e("\n"),a("span",{class:"token hvariable"},"third"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"z"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"z"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),zn=a("h3",{id:"泛型"},[a("a",{class:"header-anchor",href:"#泛型","aria-hidden":"true"},"#"),e(" 泛型")],-1),Dn=a("p",null,[e("运用 Type 变量(只可为"),a("em",null,"单字符"),e("), 实现泛型参数与多态函数.")],-1),Jn=a("p",null,"借助 TypeClass 可轻松实现多态函数:",-1),Gn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token builtin"},"head"),e("\n"),a("span",{class:"token builtin"},"head"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n\n"),a("span",{class:"token comment"},"-- a 和 b 可为同类型"),e("\n"),a("span",{class:"token comment"},"-- 第一个参数与返回值必须同类型"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token builtin"},"fst"),e("\n"),a("span",{class:"token builtin"},"fst"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n\n"),a("span",{class:"token comment"},"-- 所有参数必须同类型,且必须为Num成员"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),_n=a("h3",{id:"函数类型"},[a("a",{class:"header-anchor",href:"#函数类型","aria-hidden":"true"},"#"),e(" 函数类型")],-1),Hn=a("ul",null,[a("li",null,"单个参数")],-1),Kn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"removeNonUppercase"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"removeNonUppercase"),e(),a("span",{class:"token hvariable"},"st"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"st"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"c"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Yn=a("ul",null,[a("li",null,"多个参数")],-1),Vn=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"addThree"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"addThree"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token hvariable"},"z"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"z"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Un=a("h2",{id:"基本语法"},[a("a",{class:"header-anchor",href:"#基本语法","aria-hidden":"true"},"#"),e(" 基本语法")],-1),Zn=a("h3",{id:"名字-函数定义"},[a("a",{class:"header-anchor",href:"#名字-函数定义","aria-hidden":"true"},"#"),e(" 名字/函数定义")],-1),jn=a("h4",{id:"模式匹配-pattern-matching"},[a("a",{class:"header-anchor",href:"#模式匹配-pattern-matching","aria-hidden":"true"},"#"),e(" 模式匹配(Pattern Matching)")],-1),Qn=a("p",null,"当函数拥有多个函数体(模式)时,会从上至下进行匹配各模式,一旦匹配则只应用这一函数体.",-1),Xn=a("h5",{id:"best-practice"},[a("a",{class:"header-anchor",href:"#best-practice","aria-hidden":"true"},"#"),e(" Best Practice")],-1),ns=a("ul",null,[a("li",null,"代替 if-else/switch 语句"),a("li",null,"递归算法(将递归基础作为首模式,递归函数体作为尾模式)"),a("li",null,"List Range 中亦可使用模式匹配")],-1),ss=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"addVectors"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"addVectors"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x1"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"x2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"y1"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"y2"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),as=a("h5",{id:"常用模式"},[a("a",{class:"header-anchor",href:"#常用模式","aria-hidden":"true"},"#"),e(" 常用模式")],-1),es=a("h6",{id:"as-模式"},[a("a",{class:"header-anchor",href:"#as-模式","aria-hidden":"true"},"#"),e(" as 模式")],-1),ts=a("p",null,"all@(pattern) - all 为指向 pattern 整体的引用",-1),ls=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"all"),a("span",{class:"token operator"},"@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},"-- 其中all与(x:y:xs)等价"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),os=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"capital"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"capital"),e(),a("span",{class:"token string"},'""'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Empty string, whoops!"'),e("\n"),a("span",{class:"token hvariable"},"capital"),e(),a("span",{class:"token builtin"},"all"),a("span",{class:"token operator"},"@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"The first letter of "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" is "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ps=a("h6",{id:"list-模式"},[a("a",{class:"header-anchor",href:"#list-模式","aria-hidden":"true"},"#"),e(" List 模式")],-1),cs=a("ul",null,[a("li",null,"x:xs"),a("li",null,"x:y:z:xs")],-1),rs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"error"),e(),a("span",{class:"token string"},'"Can\'t call head on an empty list, dummy!"'),e("\n"),a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),us=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"length"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n"),a("span",{class:"token builtin"},"length"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),e("\n"),a("span",{class:"token builtin"},"length"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"_"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token builtin"},"length"),e("' "),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),is=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),e("\n"),a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ks=a("h6",{id:"tuple"},[a("a",{class:"header-anchor",href:"#tuple","aria-hidden":"true"},"#"),e(" Tuple")],-1),bs=a("ul",null,[a("li",null,"(x, y)"),a("li",null,"(x, y, z)")],-1),ms=a("h4",{id:"guard-模式-与-where-绑定"},[a("a",{class:"header-anchor",href:"#guard-模式-与-where-绑定","aria-hidden":"true"},"#"),e(" guard 模式 与 where 绑定")],-1),hs=a("p",null,"子模式匹配: 运用布尔表达式实现判断,应用对应函数体",-1),ds=a("ul",null,[a("li",null,"关键符号: | 与 where"),a("li",null,"| 分隔函数体"),a("li",null,[e("where "),a("ul",null,[a("li",null,"可见性: 定义只对本模式可见的(私有)名字与(私有)函数"),a("li",null,"where 定义在最外层,使得各模式共享(私有)名字与(私有)函数"),a("li",null,[e("名字定义时可使用模式匹配 "),a("code",null,"where (head:_) = firstName")])])])],-1),vs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"bmiTell"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"RealFloat"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"bmiTell"),e(),a("span",{class:"token hvariable"},"weight"),e(),a("span",{class:"token hvariable"},"height"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"bmi"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token hvariable"},"skinny"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"You\'re underweight, you emo, you!"'),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"bmi"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token hvariable"},"normal"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},"\"You're supposedly normal. Pet, I bet you're ugly!\""),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"bmi"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token hvariable"},"fat"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"You\'re fat! Lose some weight, fatty!"'),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"You\'re a whale, congratulations!"'),e("\n "),a("span",{class:"token keyword"},"where"),e(),a("span",{class:"token hvariable"},"bmi"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"weight"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token hvariable"},"height"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"2"),e("\n "),a("span",{class:"token hvariable"},"skinny"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"18.5"),e("\n "),a("span",{class:"token hvariable"},"normal"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"25.0"),e("\n "),a("span",{class:"token hvariable"},"fat"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"30.0"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),gs=a("h4",{id:"let-绑定"},[a("a",{class:"header-anchor",href:"#let-绑定","aria-hidden":"true"},"#"),e(" let 绑定")],-1),ys=a("p",null,"类似 where,绑定对象为表达式/函数",-1),fs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"bindings"),e("\n"),a("span",{class:"token keyword"},"in"),e(" "),a("span",{class:"token hvariable"},"expressions"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ws=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"sideArea"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"r"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"h"),e("\n "),a("span",{class:"token hvariable"},"topArea"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"r"),e(),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token keyword"},"in"),e(" "),a("span",{class:"token hvariable"},"sideArea"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"topArea"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),xs=a("ul",null,[a("li",null,"可见性:in 作用域,只对本 guard 可见"),a("li",null,"可使用模式匹配"),a("li",null,"可用于 List Range 中")],-1),Ss=a("h4",{id:"case-表达式"},[a("a",{class:"header-anchor",href:"#case-表达式","aria-hidden":"true"},"#"),e(" case 表达式")],-1),Ls=a("ul",null,[a("li",null,"模式匹配是 case 表达式的特殊情况(语法糖:简化写法)"),a("li",null,"在函数中,模式匹配只能用于参数定义中,而 case 表达式可用于其他地方(let/where 绑定 普通表达式 guard 语句)")],-1),Ms=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token hvariable"},"expression"),e(),a("span",{class:"token keyword"},"of"),e(),a("span",{class:"token hvariable"},"pattern"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"result"),e("\n "),a("span",{class:"token hvariable"},"pattern"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"result"),e("\n "),a("span",{class:"token hvariable"},"pattern"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"result"),e("\n "),a("span",{class:"token operator"},"..."),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Fs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"describeList"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"describeList"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"The list is "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token keyword"},"of"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token string"},'"empty."'),e("\n "),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token string"},'"a singleton list."'),e("\n "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token string"},'"a longer list."'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ts=a("h2",{id:"type-class"},[a("a",{class:"header-anchor",href:"#type-class","aria-hidden":"true"},"#"),e(" Type Class")],-1),Is=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token builtin"},"fromIntegral"),e("\n"),a("span",{class:"token builtin"},"fromIntegral"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Integral"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),As=a("h3",{id:"符号"},[a("a",{class:"header-anchor",href:"#符号","aria-hidden":"true"},"#"),e(" => 符号")],-1),Cs=a("p",null,"=> 左部: 类约束(Class Constraint) => 右部: 函数类型(参数/返回值类型),其中参数类型同属 Class",-1),Os=a("h3",{id:"basic-type-class"},[a("a",{class:"header-anchor",href:"#basic-type-class","aria-hidden":"true"},"#"),e(" Basic Type Class")],-1),Ns=a("blockquote",null,[a("p",null,"ghci> :info typeClassName")],-1),Bs=a("h4",{id:"eq"},[a("a",{class:"header-anchor",href:"#eq","aria-hidden":"true"},"#"),e(" Eq")],-1),Ws=a("ul",null,[a("li",null,"功能: 成员类型可判断相等性"),a("li",null,"成员: 大部分基本类型(不包含函数类型)"),a("li",null,"方法: == 与 /= 函数")],-1),Es=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"not"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"/="),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"/="),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"not"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),$s=a("h4",{id:"ord"},[a("a",{class:"header-anchor",href:"#ord","aria-hidden":"true"},"#"),e(" Ord")],-1),Ps=a("p",null,"Ord 成员必为 Eq 成员: class (Eq a) => Ord a where",-1),qs=a("ul",null,[a("li",null,"功能: 成员类型可排序"),a("li",null,"成员: 大部分基本类型(不包含函数类型)"),a("li",null,[e("方法: "),a("ul",null,[a("li",null,"< > <= >= 函数"),a("li",null,"compare 函数 (Ord a) => a -> a -> Ordering")])])],-1),Rs=a("h4",{id:"show"},[a("a",{class:"header-anchor",href:"#show","aria-hidden":"true"},"#"),e(" Show")],-1),zs=a("ul",null,[a("li",null,"功能: 成员类型可用字符串表示"),a("li",null,"成员: 大部分基本类型(不包含函数类型)"),a("li",null,"方法: show 函数 (Show a) => a -> String")],-1),Ds=a("p",null,[a("em",null,"Tips"),e(": 结合 Read, 可用于字符串与数值之间的转化")],-1),Js=a("h4",{id:"read"},[a("a",{class:"header-anchor",href:"#read","aria-hidden":"true"},"#"),e(" Read")],-1),Gs=a("ul",null,[a("li",null,"功能: 可以将字串转为 Read 某成员类型"),a("li",null,"成员: 大部分基本类型(不包含函数类型)"),a("li",null,"方法: read 函数 (Read a) => String -> a")],-1),_s=a("p",null,[a("em",null,"Tips"),e(": 结合 Show, 可用于字符串与数值之间的转化")],-1),Hs=a("h4",{id:"enum"},[a("a",{class:"header-anchor",href:"#enum","aria-hidden":"true"},"#"),e(" Enum")],-1),Ks=a("ul",null,[a("li",null,[e("功能: 连续性(可枚举), 其成员类型可用于"),a("em",null,"Range"),e("中")]),a("li",null,"成员: () Bool Char Ordering Int Integer Float Double")],-1),Ys=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Thursday"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token constant"},"Sunday"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Vs=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"succ"),e(),a("span",{class:"token constant"},"Monday"),e("\n"),a("span",{class:"token constant"},"Tuesday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"pred"),e(),a("span",{class:"token constant"},"Saturday"),e("\n"),a("span",{class:"token constant"},"Friday"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Us=a("h4",{id:"bounded"},[a("a",{class:"header-anchor",href:"#bounded","aria-hidden":"true"},"#"),e(" Bounded")],-1),Zs=a("ul",null,[a("li",null,"功能: 成员类型具有上下限"),a("li",null,[e("方法: minBound/maxBound 函数 (Bounded a) => a "),a("em",null,"无参多态常量/定义")])],-1),js=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"minBound"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Monday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"maxBound"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Sunday"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Qs=a("h4",{id:"num"},[a("a",{class:"header-anchor",href:"#num","aria-hidden":"true"},"#"),e(" Num")],-1),Xs=a("ul",null,[a("li",null,"功能: 成员类型具有数字特征"),a("li",null,"成员: 实数 整数 - Int Integer Float Double"),a("li",null,"方法: + - * abs 函数"),a("li",null,"实例: 所有数字都是多态常量/定义(可视为函数)")],-1),na=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token number"},"20"),e("\n"),a("span",{class:"token number"},"20"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"t"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"t"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),sa=a("h4",{id:"integral"},[a("a",{class:"header-anchor",href:"#integral","aria-hidden":"true"},"#"),e(" Integral")],-1),aa=a("ul",null,[a("li",null,"功能: 成员类型具有数字特征"),a("li",null,"成员: 整型 - Int Integer")],-1),ea=a("h4",{id:"floating"},[a("a",{class:"header-anchor",href:"#floating","aria-hidden":"true"},"#"),e(" Floating")],-1),ta=a("ul",null,[a("li",null,"功能: 成员类型具有数字特征"),a("li",null,"成员: 浮点型 - Float Double")],-1),la=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"left"}},"TypeClass"),a("th",{style:{"text-align":"left"}},"Method Feature")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"left"}},"Functor"),a("td",{style:{"text-align":"left"}},"f a + (a -> b) -> f b")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Applicative"),a("td",{style:{"text-align":"left"}},"f a + f (a -> b) -> f b")]),a("tr",null,[a("td",{style:{"text-align":"left"}},"Monad"),a("td",{style:{"text-align":"left"}},"m a + (a -> m b) -> m b")])])],-1),oa=a("h4",{id:"functor"},[a("a",{class:"header-anchor",href:"#functor","aria-hidden":"true"},"#"),e(),a("em",null,"Functor")],-1),pa=a("ul",null,[a("li",null,[e("成员: Maybe a, [], Either a, IO "),a("ul",null,[a("li",null,[e("成员 kind 必须为 "),a("code",null,"* -> *")]),a("li",null,[e("f "),a("em",null,"一元类型构造符(type constructor)")])])]),a("li",null,[e("必须遵守准则: "),a("ul",null,[a("li",null,"fmap id = id"),a("li",null,"fmap (f . g) F = fmap f (fmap g F)")])])],-1),ca=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"info"),e(),a("span",{class:"token constant"},"Functor"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"$"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ra=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"map"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Either"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Right"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Right"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token hvariable"},"x"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"action"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"result"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"action"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"result"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br")])],-1),ua=a("h4",{id:"control-applicative"},[a("a",{class:"header-anchor",href:"#control-applicative","aria-hidden":"true"},"#"),e(" Control Applicative")],-1),ia=a("ul",null,[a("li",null,[e("成员: f :: "),a("code",null,"* -> *"),e(),a("em",null,"一元类型构造符(type constructor)")]),a("li",null,"<*>: 参数为 2 个 functor 实例,其中一个包含一个函数")],-1),ka=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<$>"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e("\n"),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ba=a("ul",null,[a("li",null,"作用: 可以用单一一个函数操作多个 functor")],-1),ma=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Functor"),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<*>"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ha=a("h5",{id:"maybe"},[a("a",{class:"header-anchor",href:"#maybe","aria-hidden":"true"},"#"),e(" Maybe")],-1),da=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e("\n "),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"something"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"fmap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"something"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),va=a("h5",{id:"collection"},[a("a",{class:"header-anchor",href:"#collection","aria-hidden":"true"},"#"),e(" Collection "),a("code",null,"[]")],-1),ga=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"fs"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"fs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ya=a("h5",{id:"io"},[a("a",{class:"header-anchor",href:"#io","aria-hidden":"true"},"#"),e(" IO")],-1),fa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"return"),e("\n "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"b"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),wa=a("h5",{id:"ziplist"},[a("a",{class:"header-anchor",href:"#ziplist","aria-hidden":"true"},"#"),e(" ZipList")],-1),xa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"repeat"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token hvariable"},"fs"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"zipWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"fs"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Sa=a("h5",{id:"multi-functor"},[a("a",{class:"header-anchor",href:"#multi-functor","aria-hidden":"true"},"#"),e(" Multi Functor")],-1),La=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"8"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"pure"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Ma=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"40"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"50"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"55"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"80"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"110"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Fa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"myAction"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"myAction"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token builtin"},"getLine"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token builtin"},"getLine"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ta=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getZipList"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"ZipList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ia=a("h5",{id:"高度封装函数"},[a("a",{class:"header-anchor",href:"#高度封装函数","aria-hidden":"true"},"#"),e(" 高度封装函数")],-1),Aa=a("p",null,[a("code",null,"liftA2"),e(", 对两个 applicative 运用二元函数:")],-1),Ca=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"liftA2"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Applicative"),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"c"),e("\n"),a("span",{class:"token hvariable"},"liftA2"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token hvariable"},"b"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"liftA2"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},":"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},":"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Oa=a("h4",{id:"control-monad"},[a("a",{class:"header-anchor",href:"#control-monad","aria-hidden":"true"},"#"),e(" Control Monad")],-1),Na=a("ul",null,[a("li",null,"成员: 类型构造符(type constructor)")],-1),Ba=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"a"),e("\n\n"),a("span",{class:"token comment"},"{- bind -}"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">>="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),e("\n\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">>"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"y"),e("\n\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token hvariable"},"msg"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"error"),e(),a("span",{class:"token hvariable"},"msg"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),Wa=a("ul",null,[a("li",null,"特性: 允许返回值之间具有弹性交互")],-1),Ea=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"{- 当出现异常后,之后所有的值都变为Nothing -}"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token number"},"4"),e("\n "),a("span",{class:"token operator"},"\\"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),$a=a("p",null,"Monad Laws:",-1),Pa=a("ul",null,[a("li",null,[e("return 满足 Left identity: "),a("code",null,"return x >>= f 等于 f x")]),a("li",null,[e("return 满足 right identity: "),a("code",null,"m >>= return 等于 m")]),a("li",null,[e("Associativity: 结合律 "),a("code",null,"(m >>= f) >>= g 等于 m >>= (\\x -> f x >>= g)")])],-1),qa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"100000"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"100003"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"100000"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"100003"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token string"},'"move on up"'),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token string"},'"move on up"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token string"},'"Wah!"'),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token constant"},"Wah"),a("span",{class:"token operator"},"!"),e("\n\n"),a("span",{class:"token comment"},"{-Tips: 利用结合律合并两个 Monadic Function-}"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<=<"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<=<"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"<=<"),e(),a("span",{class:"token hvariable"},"g"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),Ra=a("h5",{id:"maybe-monad"},[a("a",{class:"header-anchor",href:"#maybe-monad","aria-hidden":"true"},"#"),e(" Maybe Monad")],-1),za=a("p",null,"具有失败可能性的 context 封装,灵活处理异常(返回值为 Nothing)",-1),Da=a("h6",{id:"实现"},[a("a",{class:"header-anchor",href:"#实现","aria-hidden":"true"},"#"),e(" 实现")],-1),Ja=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"applyMaybe"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"b"),e("\n"),a("span",{class:"token hvariable"},"applyMaybe"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token hvariable"},"f"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"applyMaybe"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Ga=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),_a=a("h6",{id:"do-表示法"},[a("a",{class:"header-anchor",href:"#do-表示法","aria-hidden":"true"},"#"),e(" do 表示法")],-1),Ha=a("ul",null,[a("li",null,"在 do expression 中,每一行都是一个 monadic value"),a("li",null,"检查返回值,使用 <-")],-1),Ka=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e("\n "),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token string"},'"!"'),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e(" "),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token string"},'"!"'),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"->"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"routine"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token constant"},"Pole"),e("\n"),a("span",{class:"token hvariable"},"routine"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token hvariable"},"first"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token hvariable"},"start"),e("\n "),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token hvariable"},"second"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"landRight"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token hvariable"},"first"),e("\n "),a("span",{class:"token hvariable"},"landLeft"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token hvariable"},"second"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br")])],-1),Ya=a("h5",{id:"list-monad"},[a("a",{class:"header-anchor",href:"#list-monad","aria-hidden":"true"},"#"),e(" List Monad")],-1),Va=a("ul",null,[a("li",null,"non-determinism(不确定性)")],-1),Ua=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<$>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<*>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1000"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1000"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"200"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2000"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"30"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"300"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3000"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Za=a("ul",null,[a("li",null,"实现")],-1),ja=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"concat"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Qa=a("ul",null,[a("li",null,[e("返回值交互: 下例中 n 与 return (n, ch) 进行交互 "),a("ul",null,[a("li",null,"list comprehension 与 do 表示法 均是 >>= 的语法糖")])]),a("li",null,"list comprehension: <- 与 条件表达式"),a("li",null,"do 表示法: <- 与 guard 函数")],-1),Xa=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"ch"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"ch"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token comment"},"{- do 表示法 -}"),e("\n"),a("span",{class:"token hvariable"},"listOfTuples"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Int"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"listOfTuples"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"ch"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"ch"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"sevensOnly"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Int"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"sevensOnly"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"50"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"guard"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token char string"},"'7'"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e("\n\n"),a("span",{class:"token comment"},"{- list comprehension -}"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"ch"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"ch"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token char string"},"'b'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1),ne=a("h5",{id:"monadplus"},[a("a",{class:"header-anchor",href:"#monadplus","aria-hidden":"true"},"#"),e(" MonadPlus")],-1),se=a("p",null,"使 Monad 具有 Monoid 的性质(二元封闭运算)",-1),ae=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"MonadPlus"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mzero"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"mplus"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ee=a("h5",{id:"monad-algorithms"},[a("a",{class:"header-anchor",href:"#monad-algorithms","aria-hidden":"true"},"#"),e(" Monad Algorithms")],-1),te=a("h6",{id:"马走日"},[a("a",{class:"header-anchor",href:"#马走日","aria-hidden":"true"},"#"),e(" 马走日")],-1),le=a("ul",null,[a("li",null,"计算出可移动位置")],-1),oe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"KnightPos"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"KnightPos"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),e("'"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),e("'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"guard"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),e("' `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&&"),e(),a("span",{class:"token hvariable"},"r"),e("' `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),e("'"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"r"),e("'"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),pe=a("ul",null,[a("li",null,"利用 >>= 向后传递多个可交互的位置")],-1),ce=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"in3"),e(),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"moveKnight"),e("\n\n"),a("span",{class:"token hvariable"},"in3"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"KnightPos"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"KnightPos"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"in3"),e(),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"first"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token hvariable"},"start"),e("\n "),a("span",{class:"token hvariable"},"second"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token hvariable"},"first"),e("\n "),a("span",{class:"token hvariable"},"moveKnight"),e(),a("span",{class:"token hvariable"},"second"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),re=a("ul",null,[a("li",null,"最后完成完整函数: 产生所有三步的可能位置,检查其中一个位置是否在里面")],-1),ue=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"canReachIn3"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"KnightPos"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"KnightPos"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n"),a("span",{class:"token hvariable"},"canReachIn3"),e(),a("span",{class:"token hvariable"},"start"),e(),a("span",{class:"token hvariable"},"end"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"end"),e(" `"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token hvariable"},"in3"),e(),a("span",{class:"token hvariable"},"start"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ie=a("h4",{id:"foldable"},[a("a",{class:"header-anchor",href:"#foldable","aria-hidden":"true"},"#"),e(" Foldable")],-1),ke=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.Foldable "),a("span",{class:"token keyword"},"as"),e(" F")]),e("\n\n"),a("span",{class:"token hvariable"},"foldMap"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Foldable"),e(),a("span",{class:"token hvariable"},"t"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),be=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"F.Foldable"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"foldMap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token constant"},"Empty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"mempty"),e("\n "),a("span",{class:"token hvariable"},"foldMap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"l"),e(),a("span",{class:"token hvariable"},"r"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"F.foldMap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"l"),e(),a("span",{class:"token operator"},"`mappend`"),e("\n "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(" "),a("span",{class:"token operator"},"`mappend`"),e("\n "),a("span",{class:"token hvariable"},"F.foldMap"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"r"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),me=a("h4",{id:"data-monoid"},[a("a",{class:"header-anchor",href:"#data-monoid","aria-hidden":"true"},"#"),e(" Data Monoid")],-1),he=a("ul",null,[a("li",null,"成员: 必须为具体类型(*不可是类型构造符(type constructor))"),a("li",null,[e("准则(Monoid Law): "),a("ul",null,[a("li",null,[a("em",null,"结合律"),e(" a·(b·c) = (a·b)·c")]),a("li",null,[e("无需满足 a "),a("code",null,"mappend"),e(" b == b "),a("code",null,"mappend"),e(" a")])])])],-1),de=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"m"),e(" "),a("span",{class:"token comment"},"-- identity"),e("\n "),a("span",{class:"token hvariable"},"mappend"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e("\n "),a("span",{class:"token hvariable"},"mconcat"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"m"),e("\n "),a("span",{class:"token hvariable"},"mconcat"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token hvariable"},"mappend"),e(),a("span",{class:"token hvariable"},"mempty"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),ve=a("ul",null,[a("li",null,"实例")],-1),ge=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"mappend"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ye=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"newtype"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(" "),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"getProduct"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Bounded"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token number"},"1"),e("\n "),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getProduct"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Product"),e(),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token number"},"24"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),fe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"newtype"),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"getAny"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Bool"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Bounded"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token constant"},"False"),e("\n "),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"||"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getAny"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"mconcat"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token constant"},"Any"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"False"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"False"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"False"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),we=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"newtype"),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"getAll"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Bool"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Bounded"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"&&"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getAll"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"mconcat"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token constant"},"All"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"False"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),xe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token constant"},"Ordering"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"EQ"),e("\n "),a("span",{class:"token constant"},"LT"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"LT"),e("\n "),a("span",{class:"token constant"},"EQ"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"y"),e("\n "),a("span",{class:"token constant"},"GT"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"GT"),e("\n\n"),a("span",{class:"token comment"},"-- Tips:"),e("\n"),a("span",{class:"token comment"},"-- mappend 在左边不等于 EQ 的情况下都会回传左边的值。相反地则回传右边的值"),e("\n"),a("span",{class:"token comment"},"-- 可代替多个 if/else 语句"),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.Monoid")]),e("\n\n"),a("span",{class:"token hvariable"},"lengthCompare"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Ordering"),e("\n"),a("span",{class:"token hvariable"},"lengthCompare"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"x"),e(" `"),a("span",{class:"token builtin"},"compare"),e("` "),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`mappend`"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"vowels"),e(),a("span",{class:"token hvariable"},"x"),e(" `"),a("span",{class:"token builtin"},"compare"),e("` "),a("span",{class:"token hvariable"},"vowels"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`mappend`"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(" `"),a("span",{class:"token builtin"},"compare"),e("` "),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"where"),e(),a("span",{class:"token hvariable"},"vowels"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"length"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token string"},'"aeiou"'),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),Se=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"m"),e("\n "),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"m"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"m1"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"m2"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"m1"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"m2"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"mempty"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token constant"},"Nothing"),e("\n "),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"getFirst"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token char string"},"'a'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token constant"},"First"),e(),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token char string"},"'a'"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br")])],-1),Le=a("h3",{id:"custom-type-class"},[a("a",{class:"header-anchor",href:"#custom-type-class","aria-hidden":"true"},"#"),e(" Custom Type Class")],-1),Me=a("ul",null,[a("li",null,[e("创建新类: "),a("em",null,"可以只有声明没有实现")])],-1),Fe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token constant"},"ClassName"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token hvariable"},"defining"),e(),a("span",{class:"token hvariable"},"code"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Te=a("ul",null,[a("li",null,[e("创建已有类的实例: "),a("em",null,"必须实现所有已声明函数"),a("ul",null,[a("li",null,"作用等同于 deriving(自由度更大)"),a("li",null,"可以重写函数,去除默认函数处理,达到特定目的")])])],-1),Ie=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token operator"},"-"),e(" 先创建新类型\n"),a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"TrafficLight"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Red"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Yellow"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Green"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token constant"},"TrafficLight"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token constant"},"Red"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Red"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token constant"},"Green"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Green"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token constant"},"Yellow"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Yellow"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Show"),e(),a("span",{class:"token constant"},"TrafficLight"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token constant"},"Red"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Red light"'),e("\n "),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token constant"},"Yellow"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Yellow light"'),e("\n "),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token constant"},"Green"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Green light"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br")])],-1),Ae=a("ul",null,[a("li",null,[e("创建新类和实现实例时,使用 class constraint "),a("ul",null,[a("li",null,[e("可达到"),a("em",null,"类似于"),e("继承的效果")]),a("li",null,"可达到限制类型的效果")])])],-1),Ce=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token operator"},"..."),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"y"),e("\n "),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Oe=a("h3",{id:"data-type"},[a("a",{class:"header-anchor",href:"#data-type","aria-hidden":"true"},"#"),e(),a("code",null,"data"),e(" type")],-1),Ne=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"SelfDefinedTypeName"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token constant"},"ValueConstructorName"),e(),a("span",{class:"token constant"},"ValueType"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"TypeClass"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Be=a("ul",null,[a("li",null,"data 范例")],-1),We=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Shape"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Rectangle"),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token constant"},"Point"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ee=a("ul",null,[a("li",null,"导出 data")],-1),$e=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Shapes"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token constant"},"Point"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Shape"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Pe=a("ul",null,[a("li",null,"后构造器 > 前构造器")],-1),qe=a("blockquote",null,[a("p",null,"e.g True > False")],-1),Re=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Bool"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"True"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),ze=a("h4",{id:"函数特性"},[a("a",{class:"header-anchor",href:"#函数特性","aria-hidden":"true"},"#"),e(" 函数特性")],-1),De=a("p",null,"data type 也是函数,若省略参数亦会造成 Curry 化.",-1),Je=a("blockquote",null,[a("p",null,"e.g map fx list")],-1),Ge=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10.0"),e(),a("span",{class:"token number"},"20.0"),e(),a("span",{class:"token number"},"4.0"),a("span",{class:"token punctuation"},","),e("\n"),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10.0"),e(),a("span",{class:"token number"},"20.0"),e(),a("span",{class:"token number"},"5.0"),a("span",{class:"token punctuation"},","),e("\n"),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10.0"),e(),a("span",{class:"token number"},"20.0"),e(),a("span",{class:"token number"},"6.0"),a("span",{class:"token punctuation"},","),e("\n"),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10.0"),e(),a("span",{class:"token number"},"20.0"),e(),a("span",{class:"token number"},"6.0"),e("\n"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),_e=a("ul",null,[a("li",null,[e("Value Constructor:使用"),a("code",null,"ValueConstructorName ValueType .."),e("可构造出一个该类型的定义/名字")])],-1),He=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token number"},"20"),e(),a("span",{class:"token number"},"30"),e("\n"),a("span",{class:"token constant"},"Circle"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token number"},"20"),e(),a("span",{class:"token number"},"30"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ke=a("h4",{id:"记录语法-record-syntax"},[a("a",{class:"header-anchor",href:"#记录语法-record-syntax","aria-hidden":"true"},"#"),e(" 记录语法(Record Syntax)")],-1),Ye=a("ul",null,[a("li",null,"定义")],-1),Ve=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Person"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Person"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"firstName"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"lastName"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"age"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"height"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"phoneNumber"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"flavor"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n "),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Ue=a("ul",null,[a("li",null,"使用")],-1),Ze=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token hvariable"},"company"),a("span",{class:"token operator"},"="),a("span",{class:"token string"},'"Ford"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"model"),a("span",{class:"token operator"},"="),a("span",{class:"token string"},'"Mustang"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"year"),a("span",{class:"token operator"},"="),a("span",{class:"token number"},"1967"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token hvariable"},"company"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Ford"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"model"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"Mustang"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"year"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1967"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),je=a("h4",{id:"类型参数-type-parameters"},[a("a",{class:"header-anchor",href:"#类型参数-type-parameters","aria-hidden":"true"},"#"),e(" 类型参数(Type Parameters)")],-1),Qe=a("p",null,"提高代码的复用性",-1),Xe=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"company"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"model"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"b"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"year"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"c"),e("\n "),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),nt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"tellCar"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"tellCar"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Car"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token hvariable"},"company"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"model"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"m"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"year"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token string"},'"This "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"m"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" was made in "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"y"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),st=a("h5",{id:"maybe-value-constructor"},[a("a",{class:"header-anchor",href:"#maybe-value-constructor","aria-hidden":"true"},"#"),e(" Maybe value constructor")],-1),at=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),et=a("ul",null,[a("li",null,"Just 可实现转化:")],-1),tt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),lt=a("h4",{id:"deriving-派生"},[a("a",{class:"header-anchor",href:"#deriving-派生","aria-hidden":"true"},"#"),e(" Deriving(派生)")],-1),ot=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Day"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Monday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Tuesday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Wednesday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Thursday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Friday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Saturday"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Sunday"),e("\n "),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Bounded"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Enum"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),pt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Wednesday"),e("\n"),a("span",{class:"token constant"},"Wednesday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token constant"},"Wednesday"),e("\n"),a("span",{class:"token string"},'"Wednesday"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"read"),e(),a("span",{class:"token string"},'"Saturday"'),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Saturday"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Saturday"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Sunday"),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Saturday"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token constant"},"Saturday"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Saturday"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Friday"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Monday"),e(" `"),a("span",{class:"token builtin"},"compare"),e("` "),a("span",{class:"token constant"},"Wednesday"),e("\n"),a("span",{class:"token constant"},"LT"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"minBound"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Monday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"maxBound"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Day"),e("\n"),a("span",{class:"token constant"},"Sunday"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"succ"),e(),a("span",{class:"token constant"},"Monday"),e("\n"),a("span",{class:"token constant"},"Tuesday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"pred"),e(),a("span",{class:"token constant"},"Saturday"),e("\n"),a("span",{class:"token constant"},"Friday"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Thursday"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token constant"},"Sunday"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Thursday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Friday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Saturday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Sunday"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token builtin"},"minBound"),e(),a("span",{class:"token operator"},".."),e(),a("span",{class:"token builtin"},"maxBound"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Day"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Monday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Tuesday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Wednesday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Thursday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Friday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Saturday"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Sunday"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br")])],-1),ct=a("h4",{id:"type-定义"},[a("a",{class:"header-anchor",href:"#type-定义","aria-hidden":"true"},"#"),e(" type 定义")],-1),rt=a("p",null,[e("为 data 声明别名 - "),a("code",null,"typedef")],-1),ut=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Char"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"PhoneNumber"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"Name"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"PhoneBook"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Name"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"PhoneNumber"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),it=a("ul",null,[a("li",null,"type 类型参数: 匹配 data 类型参数")],-1),kt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"AssocList"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"IntMap"),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"IntMap"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token constant"},"Int"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),bt=a("p",null,"类型别名,只可以在 Haskell 的类型部分中使用:",-1),mt=a("ul",null,[a("li",null,"定义新类型"),a("li",null,"类型声明"),a("li",null,"类型注释(:😃"),a("li",null,[e("禁止: 定义名字/定义 "),a("em",null,"AssocList [(1,2),(4,5),(7,9)]")])],-1),ht=a("h4",{id:"高级数据结构"},[a("a",{class:"header-anchor",href:"#高级数据结构","aria-hidden":"true"},"#"),e(" 高级数据结构")],-1),dt=a("h5",{id:"栈"},[a("a",{class:"header-anchor",href:"#栈","aria-hidden":"true"},"#"),e(" 栈")],-1),vt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"type"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Int"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"pop"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Int"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Stack"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"pop"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Stack"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"a"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),gt=a("h5",{id:"链表"},[a("a",{class:"header-anchor",href:"#链表","aria-hidden":"true"},"#"),e(" 链表")],-1),yt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"List"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Empty"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Cons"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"List"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"List"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Empty"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Cons"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"listHead"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"listTail"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"List"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"}"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),ft=a("h5",{id:"二叉树"},[a("a",{class:"header-anchor",href:"#二叉树","aria-hidden":"true"},"#"),e(" 二叉树")],-1),wt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"data"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"deriving"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Show"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Read"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Eq"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),xt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"singleton"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"singleton"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token constant"},"EmptyTree"),e("\n\n"),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"singleton"),e(),a("span",{class:"token hvariable"},"x"),e("\n"),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"left"),e(),a("span",{class:"token hvariable"},"right"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"left"),e(),a("span",{class:"token hvariable"},"right"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token hvariable"},"a"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"left"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"right"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"a"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"left"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"right"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Tree"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n"),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Node"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"left"),e(),a("span",{class:"token hvariable"},"right"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token hvariable"},"a"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"left"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"a"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"treeElem"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"right"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"nums"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"numsTree"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token hvariable"},"treeInsert"),e(),a("span",{class:"token constant"},"EmptyTree"),e(),a("span",{class:"token hvariable"},"nums"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1),St=a("h2",{id:"函数"},[a("a",{class:"header-anchor",href:"#函数","aria-hidden":"true"},"#"),e(" 函数")],-1),Lt=a("h3",{id:"递归函数"},[a("a",{class:"header-anchor",href:"#递归函数","aria-hidden":"true"},"#"),e(" 递归函数")],-1),Mt=a("ul",null,[a("li",null,"边界条件"),a("li",null,"递归基础"),a("li",null,"递归函数体")],-1),Ft=a("h4",{id:"list-函数"},[a("a",{class:"header-anchor",href:"#list-函数","aria-hidden":"true"},"#"),e(" List 函数")],-1),Tt=a("ul",null,[a("li",null,"边界条件: 空 List"),a("li",null,"递归函数体: x:xs 取出首元素进行一般操作,对尾部进行递归操作.")],-1),It=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"error"),e(),a("span",{class:"token string"},'"maximum of empty list"'),e("\n"),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e("\n"),a("span",{class:"token comment"},"{-\nmaximum' (x:xs)\n | x > maxTail = x\n | otherwise = maxTail\n where maxTail = maximum' xs\n-}"),e("\n"),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),At=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"replicate"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"i"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"i"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"i"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"replicate"),e("' "),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token builtin"},"replicate"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ct=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"i"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"i"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"i"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token hvariable"},"_"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token builtin"},"take"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"n"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Ot=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Nt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"repeat"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"repeat"),e("' "),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token builtin"},"repeat"),e("' "),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Bt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token builtin"},"zip"),e("' "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token hvariable"},"ys"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Wt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"elem"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n"),a("span",{class:"token builtin"},"elem"),e("' "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token builtin"},"elem"),e("' "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"x"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"True"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"a"),e(" `"),a("span",{class:"token builtin"},"elem"),e("'` "),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Et=a("p",null,"Awesome Quick Sort",-1),$t=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"smallerSorted"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"biggerSorted"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"quicksort"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token hvariable"},"smallerSorted"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"biggerSorted"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Pt=a("h3",{id:"高阶函数"},[a("a",{class:"header-anchor",href:"#高阶函数","aria-hidden":"true"},"#"),e(" 高阶函数")],-1),qt=a("h4",{id:"curry-化"},[a("a",{class:"header-anchor",href:"#curry-化","aria-hidden":"true"},"#"),e(" Curry 化")],-1),Rt=a("p",null,[e("当传入不全参数时,会改变函数的类型,"),a("em",null,"返回值从单类型变成函数类型"),e(".")],-1),zt=a("ul",null,[a("li",null,"当传入不全参数时:"),a("li",null,"compare 的类型变为 (Ord a) => a -> (a -> Ordering)")],-1),Dt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"compareWithHundred"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Ordering"),e("\n"),a("span",{class:"token hvariable"},"compareWithHundred"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"compare"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Jt=a("ul",null,[a("li",null,"通过给二元中缀函数传递唯一参数:"),a("li",null,"中缀函数类型由 a -> a -> a 转为 a -> a")],-1),Gt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"divideByTen"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Floating"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"divideByTen"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Fractional"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),_t=a("ul",null,[a("li",null,[e("包装函数: "),a("ul",null,[a("li",null,"传入一个二元函数作为参数,便可实现 zipWithFunc"),a("li",null,"若在定义时便传入一个函数参数,便可实现 Curry 化")])])],-1),Ht=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token hvariable"},"ys"),e("\n\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"foo "'),e(","),a("span",{class:"token string"},'"bar "'),e(","),a("span",{class:"token string"},'"baz "'),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"fighters"'),e(","),a("span",{class:"token string"},'"hoppers"'),e(","),a("span",{class:"token string"},'"aldrin"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"foo fighters"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"bar hoppers"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"baz aldrin"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"replicate"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"zipWith"),e("' "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"30"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),Kt=a("ul",null,[a("li",null,"通过 Curry 化,还可省略参数")],-1),Yt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"bar"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e("\n"),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"bar"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Vt=a("h4",{id:"map-函数"},[a("a",{class:"header-anchor",href:"#map-函数","aria-hidden":"true"},"#"),e(" map 函数")],-1),Ut=a("p",null,"映射函数 - List Comprehension 的函数化",-1),Zt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),jt=a("ul",null,[a("li",null,[e("如果 map 传入的函数参数的类型为 a -> a -> a,则返回的 List 类型为"),a("code",null,"[a -> a]"),e("(f x 传参不完全,造成了 Curry 化).")])],-1),Qt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"listOfFun"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"listOfFun"),e(),a("span",{class:"token operator"},"!!"),e(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token number"},"20"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Xt=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'"!"'),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"BIFF"'),e(","),a("span",{class:"token string"},'"BANG"'),e(","),a("span",{class:"token string"},'"POW"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"BIFF!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"BANG!"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"POW!"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"replicate"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"25"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"36"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"49"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"64"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"fst"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),nl=a("h4",{id:"filter-函数"},[a("a",{class:"header-anchor",href:"#filter-函数","aria-hidden":"true"},"#"),e(" filter 函数")],-1),sl=a("p",null,"过滤函数 - Comprehension 的函数化",-1),al=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token hvariable"},"x"),e(" "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token hvariable"},"xs"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),el=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"even"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"notNull"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"not"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token hvariable"},"notNull"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"u LaUgH aT mE BeCaUsE I aM diFfeRent"'),e("\n"),a("span",{class:"token string"},'"uagameasadifeent"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"i lauGh At You BecAuse u r aLL the Same"'),e("\n"),a("span",{class:"token string"},'"GAYBALLS"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),tl=a("h4",{id:"fold-函数与-scan-函数"},[a("a",{class:"header-anchor",href:"#fold-函数与-scan-函数","aria-hidden":"true"},"#"),e(" fold 函数与 scan 函数")],-1),ll=a("p",null,[a("img",{src:"/awesome-notes/assets/foldl.8f7f9807.png",alt:"折叠函数"})],-1),ol=a("blockquote",null,[a("p",null,"如图中所示,左折叠时将 acc 视为第 0 个元素;右折叠时将 acc 视为最后一个元素.")],-1),pl=a("ul",null,[a("li",null,[e("三要素: "),a("ul",null,[a("li",null,"二元函数 \\acc x -> function 或 \\x acc -> function"),a("li",null,"初始累加值"),a("li",null,"待折叠 List")])]),a("li",null,[e("工作原理: "),a("ul",null,[a("li",null,"不断从 List 中取出元素,进行二元函数调用,直至 List 被取空"),a("li",null,"调用参数分别为 新取出元素 x 与 之前 n 次调用后的累加值 acc"),a("li",null,"返回值作为下次调用的累加值 acc")])]),a("li",null,[e("左折叠函数 "),a("ul",null,[a("li",null,[e("foldl "),a("code",null,"\\acc x ->")]),a("li",null,"foldl1: 取 List 首元素作为初始累加值")])])],-1),cl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Foldable"),e(),a("span",{class:"token hvariable"},"t"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),rl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"sum"),e("' "),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ul=a("ul",null,[a("li",null,[e("右折叠函数 "),a("ul",null,[a("li",null,[e("foldr "),a("code",null,"\\x acc ->")]),a("li",null,"foldr1: 取 List 尾元素作为初始累加值")])])],-1),il=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Foldable"),e(),a("span",{class:"token hvariable"},"t"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),kl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"map"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"map"),e("' "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),bl=a("ul",null,[a("li",null,"更多范例")],-1),ml=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"maximum"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"reverse"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token builtin"},"product"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"product"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token builtin"},"filter"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token builtin"},"filter"),e("' "),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"p"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"head"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token builtin"},"last"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token builtin"},"last"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldl1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),hl=a("ul",null,[a("li",null,"scanl/scanr/scanl1/scanr1 函数会将每次折叠的结果都记录在一个 List 中")],-1),dl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"scanl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"scanr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"scanl1"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"scanl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"flip"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},":"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),vl=a("ul",null,[a("li",null,"逆波兰表达式")],-1),gl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e("\n\n"),a("span",{class:"token hvariable"},"solveRPN"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"solveRPN"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"head"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"words"),e("\n"),a("span",{class:"token keyword"},"where"),e(" "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"*"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"+"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"-"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"/"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"y"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"^"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"**"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"ys"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"ln"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"log"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token string"},'"sum"'),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"foldingFunction"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token hvariable"},"numberString"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"read"),e(),a("span",{class:"token hvariable"},"numberString"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),yl=a("h4",{id:"lambda-表达式"},[a("a",{class:"header-anchor",href:"#lambda-表达式","aria-hidden":"true"},"#"),e(" lambda 表达式")],-1),fl=a("p",null,[a("code",null,"\\args -> function")],-1),wl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"30"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"153.0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"61.5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"31.0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"15.75"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6.6"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token builtin"},"flip"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),e("\n"),a("span",{class:"token builtin"},"flip"),e("' "),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),xl=a("h4",{id:"函数-1"},[a("a",{class:"header-anchor",href:"#函数-1","aria-hidden":"true"},"#"),e(" $函数")],-1),Sl=a("p",null,[a("code",null,"($) :: (a -> b) -> a -> b")],-1),Ll=a("ul",null,[a("li",null,"特性: 优先级最低,右结合"),a("li",null,"功能: 改变优先级,相当于在右方添加括号")],-1),Ml=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"z"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"z"),e(),a("span",{class:"token hvariable"},"x"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Fl=a("h4",{id:"函数与-function-composition-函数组合"},[a("a",{class:"header-anchor",href:"#函数与-function-composition-函数组合","aria-hidden":"true"},"#"),e(" .函数与 Function composition(函数组合)")],-1),Tl=a("p",null,[a("code",null,"(.) :: (b -> c) -> (a -> b) -> a -> c")],-1),Il=a("p",null,[a("code",null,"f . g = \\x -> f (g x)"),e(" - f 的参数类型必须与 g 的返回值类型相同")],-1),Al=a("ul",null,[a("li",null,"功能: 可以去除函数调用括号")],-1),Cl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"fn"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"ceiling"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"negate"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"tan"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"cos"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token number"},"50"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token operator"},"->"),e(" 去括号"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"fn"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"ceiling"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"negate"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"tan"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"cos"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token number"},"50"),e(),a("span",{class:"token hvariable"},"x"),e("\n"),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Curry"),e("化"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token hvariable"},"fn"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"ceiling"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"negate"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"tan"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"cos"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token number"},"50"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Ol=a("ul",null,[a("li",null,"Best Practice: 三种不同的函数写法")],-1),Nl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Integer"),e("\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Integer"),e("\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"sum"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Integer"),e("\n"),a("span",{class:"token hvariable"},"oddSquareSum"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"oddSquares"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token hvariable"},"belowLimit"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"oddSquares"),e("\n "),a("span",{class:"token keyword"},"in"),e(" "),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token hvariable"},"belowLimit"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Bl=a("h3",{id:"常用函数"},[a("a",{class:"header-anchor",href:"#常用函数","aria-hidden":"true"},"#"),e(" 常用函数")],-1),Wl=a("h4",{id:"无参函数"},[a("a",{class:"header-anchor",href:"#无参函数","aria-hidden":"true"},"#"),e(" 无参函数")],-1),El=a("p",null,"“定义”(或者“名字”)",-1),$l=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"sabertaz"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"It\'s a-me, sabertaz!"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Pl=a("h4",{id:"前缀函数"},[a("a",{class:"header-anchor",href:"#前缀函数","aria-hidden":"true"},"#"),e(" 前缀函数")],-1),ql=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"succ"),e(),a("span",{class:"token number"},"8"),e("\n"),a("span",{class:"token number"},"9"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Rl=a("p",null,"`FunctionName` - 可使前缀函数变为中缀函数",-1),zl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"92"),e(" `"),a("span",{class:"token builtin"},"div"),e("` "),a("span",{class:"token number"},"10"),e("\n"),a("span",{class:"token number"},"9"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Dl=a("h4",{id:"中缀函数"},[a("a",{class:"header-anchor",href:"#中缀函数","aria-hidden":"true"},"#"),e(" 中缀函数")],-1),Jl=a("p",null,[a("code",null,"+"),e(": 从类型定义可以看出,+左右两边参数必须为同类型")],-1),Gl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Num"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),_l=a("p",null,[a("code",null,"++"),e(": List 连接符,遍历前一 List")],-1),Hl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"hello"'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'"world"'),e("\n"),a("span",{class:"token string"},'"hello world"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Kl=a("p",null,[a("code",null,":"),e(": 连接单个元素")],-1),Yl=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},":"),a("span",{class:"token string"},'" SMALL CAT"'),e("\n"),a("span",{class:"token string"},'"A SMALL CAT"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"5"),a("span",{class:"token operator"},":"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Vl=a("p",null,[a("code",null,"!!"),e(": 引用符")],-1),Ul=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"33.2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"96.2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11.2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"23.25"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"!!"),e(),a("span",{class:"token number"},"1"),e("\n"),a("span",{class:"token number"},"33.2"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Zl=a("h4",{id:"数学函数"},[a("a",{class:"header-anchor",href:"#数学函数","aria-hidden":"true"},"#"),e(" 数学函数")],-1),jl=a("ul",null,[a("li",null,[e("x "),a("code",null,"mod"),e(" y")]),a("li",null,[a("code",null,"even arg")]),a("li",null,[a("code",null,"odd arg")])],-1),Ql=a("h5",{id:"system-random"},[a("a",{class:"header-anchor",href:"#system-random","aria-hidden":"true"},"#"),e(" System Random")],-1),Xl=a("ul",null,[a("li",null,[a("code",null,"random :: (RandomGen g, Random a) => g -> (a, g)")]),a("li",null,"getStdGen"),a("li",null,"newStdGen")],-1),no=a("h4",{id:"数字函数"},[a("a",{class:"header-anchor",href:"#数字函数","aria-hidden":"true"},"#"),e(" 数字函数")],-1),so=a("ul",null,[a("li",null,"fromInteger 函数 (Num a) => Integer -> a"),a("li",null,"fromIntegral 函数 (Integral a, Num b) => a -> b")],-1),ao=a("h2",{id:"模块"},[a("a",{class:"header-anchor",href:"#模块","aria-hidden":"true"},"#"),e(" 模块")],-1),eo=a("h3",{id:"import"},[a("a",{class:"header-anchor",href:"#import","aria-hidden":"true"},"#"),e(" import")],-1),to=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"nub"),e(","),a("span",{class:"token builtin"},"sort"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List "),a("span",{class:"token keyword"},"hiding")]),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"nub"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),lo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.Map")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),oo=a("p",null,"调用 Map 中的 filter 时,必须使用 Data.Map.filter.",-1),po=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.Map "),a("span",{class:"token keyword"},"as"),e(" M")]),e("\n\n"),a("span",{class:"token constant"},"Data.Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token operator"},"<->"),e(),a("span",{class:"token constant"},"M"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"filter"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),co=a("h3",{id:"建立模块"},[a("a",{class:"header-anchor",href:"#建立模块","aria-hidden":"true"},"#"),e(" 建立模块")],-1),ro=a("h4",{id:"单一模块"},[a("a",{class:"header-anchor",href:"#单一模块","aria-hidden":"true"},"#"),e(" 单一模块")],-1),uo=a("p",null,[e("新建一个 "),a("code",null,"Geometry.hs"),e(" 的文件")],-1),io=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Geometry"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"sphereVolume"),e("\n,"),a("span",{class:"token hvariable"},"sphereArea"),e("\n,"),a("span",{class:"token hvariable"},"cubeVolume"),e("\n,"),a("span",{class:"token hvariable"},"cubeArea"),e("\n,"),a("span",{class:"token hvariable"},"cuboidArea"),e("\n,"),a("span",{class:"token hvariable"},"cuboidVolume"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n\n"),a("span",{class:"token hvariable"},"sphereVolume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"sphereVolume"),e(),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4.0"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token number"},"3.0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"sphereArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"sphereArea"),e(),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"cubeVolume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"cubeVolume"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"cuboidVolume"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e("\n\n"),a("span",{class:"token hvariable"},"cubeArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"cubeArea"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"cuboidArea"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e("\n\n"),a("span",{class:"token hvariable"},"cuboidVolume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"cuboidVolume"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"c"),e("\n\n"),a("span",{class:"token hvariable"},"cuboidArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"cuboidArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e("\n\n"),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br")])],-1),ko=a("ul",null,[a("li",null,"使用")],-1),bo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Geometry")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),mo=a("h4",{id:"子模块"},[a("a",{class:"header-anchor",href:"#子模块","aria-hidden":"true"},"#"),e(" 子模块")],-1),ho=a("p",null,[e("把 "),a("code",null,"Geometry"),e(" 分成三个子模块")],-1),vo=a("p",null,[e("建立一个 "),a("code",null,"Geometry"),e(" 文件夹(注意"),a("em",null,"首字母"),e("要大写),新建三个文件")],-1),go=a("ul",null,[a("li",null,"sphere.hs")],-1),yo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Geometry.Sphere"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"volume"),e("\n,"),a("span",{class:"token hvariable"},"area"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4.0"),e(),a("span",{class:"token operator"},"/"),e(),a("span",{class:"token number"},"3.0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token builtin"},"pi"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"radius"),e(),a("span",{class:"token operator"},"^"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),fo=a("ul",null,[a("li",null,"cuboid.hs")],-1),wo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Geometry.Cuboid"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"volume"),e("\n,"),a("span",{class:"token hvariable"},"area"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"c"),e("\n\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),e("\n\n"),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"rectangleArea"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token hvariable"},"b"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),xo=a("ul",null,[a("li",null,"cube.hs")],-1),So=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token constant"},"Geometry.Cube"),e("\n"),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"volume"),e("\n,"),a("span",{class:"token hvariable"},"area"),e("\n"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Geometry.Cuboid "),a("span",{class:"token keyword"},"as"),e(" Cuboid")]),e("\n\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"volume"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Cuboid.volume"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e("\n\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Float"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Float"),e("\n"),a("span",{class:"token hvariable"},"area"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Cuboid.area"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e(),a("span",{class:"token hvariable"},"side"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),Lo=a("ul",null,[a("li",null,"使用")],-1),Mo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Geometry.Sphere")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Fo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Geometry.Sphere "),a("span",{class:"token keyword"},"as"),e(" Sphere")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Geometry.Cuboid "),a("span",{class:"token keyword"},"as"),e(" Cuboid")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Geometry.Cube "),a("span",{class:"token keyword"},"as"),e(" Cube")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),To=a("h3",{id:"常用基础模块"},[a("a",{class:"header-anchor",href:"#常用基础模块","aria-hidden":"true"},"#"),e(" 常用基础模块")],-1),Io=a("p",null,[a("a",{href:"https://github.com/MnO2/learnyouahaskell-zh/blob/develop/zh-cn/ch07/module.md",target:"_blank",rel:"noopener noreferrer"},"Learn you a haskell for great good")],-1),Ao=a("h4",{id:"data-list"},[a("a",{class:"header-anchor",href:"#data-list","aria-hidden":"true"},"#"),e(" Data.List")],-1),Co=a("ul",null,[a("li",null,"intersperse"),a("li",null,"intercalate"),a("li",null,"transpose")],-1),Oo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"transpose"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"transpose"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"there"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"guys"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"htg"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"ehu"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"yey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"rs"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"e"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),No=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"transpose"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"18"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"17"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Bo=a("p",null,[a("strong",null,"foldl'"),e(" 和 "),a("strong",null,"foldl1'"),e(" 是它们各自惰性实现的严格版本,可"),a("em",null,"防止溢出"),e("问题.")],-1),Wo=a("p",null,[a("strong",null,"concat"),e(" - 移除一级嵌套")],-1),Eo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"concat"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"foo"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"bar"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"car"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token string"},'"foobarcar"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"concat"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),$o=a("p",null,[a("strong",null,"concatMap"),e(" 函数与 "),a("code",null,"map"),e(" 一个 List 之后再 "),a("code",null,"concat"),e(" 它等价")],-1),Po=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"concatMap"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"replicate"),e(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),qo=a("p",null,"and",-1),Ro=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"and"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"and"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),zo=a("p",null,"or",-1),Do=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"or"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"or"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Jo=a("p",null,[a("strong",null,"any"),e(" 和 "),a("strong",null,"all"),e(" 使用 "),a("code",null,"any"),e(" 或 "),a("code",null,"all"),e(" 会更多些")],-1),Go=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"any"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"HEYGUYSwhatsup"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"any"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"HEYGUYSwhatsup"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),_o=a("p",null,[a("strong",null,"iterate"),e(" 取一个函数和一个值作参数。它会用该值去调用该函数并用所得的结果再次调用该函数,产生一个无限的 List.")],-1),Ho=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"iterate"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"1"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"64"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"128"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"256"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"512"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"iterate"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'"haha"'),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"haha"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"haha"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"hahahaha"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"hahahahahaha"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ko=a("p",null,"splitAt",-1),Yo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"splitAt"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token string"},'"heyman"'),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"man"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"splitAt"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token string"},'"heyman"'),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"heyman"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"splitAt"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"heyman"'),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"heyman"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"splitAt"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token string"},'"foobar"'),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token string"},'"barfoo"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),Vo=a("p",null,[a("strong",null,"takeWhile"),e(" 一旦遇到不符合条件的某元素就停止")],-1),Uo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token char string"},"' '"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"This is a sentence"'),e("\n"),a("span",{class:"token string"},'"This"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Zo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"sum"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"takeWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"^"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"53361"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),jo=a("p",null,[a("strong",null,"dropWhile"),e(" 扔掉符合条件的元素。一旦限制条件返回 "),a("code",null,"False"),e(",它就返回 List 的余下部分")],-1),Qo=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"dropWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token char string"},"' '"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"This is a sentence"'),e("\n"),a("span",{class:"token string"},'" is a sentence"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"dropWhile"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Xo=a("p",null,[a("strong",null,"span"),e(" - 扩展"),a("code",null,"takeWhile")],-1),np=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"fw"),e(","),a("span",{class:"token hvariable"},"rest"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"span"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token char string"},"' '"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token operator"},"\\"),e(),a("span",{class:"token string"},'"This is a sentence"'),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token string"},'"First word:"'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"fw"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'",the rest:"'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"rest"),e("\n"),a("span",{class:"token string"},'"First word: This,the rest: is a sentence"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),sp=a("p",null,[a("strong",null,"break"),e(" - 取反"),a("code",null,"span")],-1),ap=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"break"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"span"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"/="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ep=a("p",null,"sort",-1),tp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"sort"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"sort"),e(),a("span",{class:"token string"},'"This will be sorted soon"'),e("\n"),a("span",{class:"token string"},'" Tbdeehiillnooorssstw"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),lp=a("p",null,"group",-1),op=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"group"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),pp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"l"),a("span",{class:"token operator"},"@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"l"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"group"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"sort"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),cp=a("p",null,[a("strong",null,"inits"),e(" 和 "),a("strong",null,"tails")],-1),rp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"inits"),e(),a("span",{class:"token string"},'"w00t"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w0"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w00"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w00t"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"tails"),e(),a("span",{class:"token string"},'"w00t"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"w00t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"00t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"0t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"w"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"w00t"'),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token builtin"},"zip"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"inits"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"tails"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"w00t"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"w"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"00t"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"w0"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"0t"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"w00"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"t"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"w00t"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'""'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),up=a("p",null,[a("strong",null,"isInfixOf"),e(" 从一个 List 中搜索一个子 List")],-1),ip=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"search"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),e("\n"),a("span",{class:"token hvariable"},"search"),e(),a("span",{class:"token hvariable"},"needle"),e(),a("span",{class:"token hvariable"},"haystack"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"nLen"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"needle"),e("\n "),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token builtin"},"foldl"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token builtin"},"take"),e(),a("span",{class:"token hvariable"},"nLen"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"needle"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token constant"},"True"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token constant"},"False"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"tails"),e(),a("span",{class:"token hvariable"},"haystack"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),kp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"cat"'),e(),a("span",{class:"token operator"},"`isInfixOf`"),e(),a("span",{class:"token string"},'"im a cat burglar"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"Cat"'),e(),a("span",{class:"token operator"},"`isInfixOf`"),e(),a("span",{class:"token string"},'"im a cat burglar"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"cats"'),e(),a("span",{class:"token operator"},"`isInfixOf`"),e(),a("span",{class:"token string"},'"im a cat burglar"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),bp=a("p",null,[a("strong",null,"isPrefixOf"),e(" 与 "),a("strong",null,"isSuffixOf")],-1),mp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"hey"'),e(),a("span",{class:"token operator"},"`isPrefixOf`"),e(),a("span",{class:"token string"},'"hey there!"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"hey"'),e(),a("span",{class:"token operator"},"`isPrefixOf`"),e(),a("span",{class:"token string"},'"oh hey there!"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"there!"'),e(),a("span",{class:"token operator"},"`isSuffixOf`"),e(),a("span",{class:"token string"},'"oh hey there!"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"there!"'),e(),a("span",{class:"token operator"},"`isSuffixOf`"),e(),a("span",{class:"token string"},'"oh hey there"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),hp=a("p",null,[a("strong",null,"elem"),e(" 与 "),a("strong",null,"notElem")],-1),dp=a("p",null,"partition",-1),vp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"partition"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"BOBsidneyMORGANeddy"'),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"BOBMORGAN"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"sidneyeddy"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"partition"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),gp=a("p",null,"find",-1),yp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"find"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"find"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"find"),e("\n"),a("span",{class:"token hvariable"},"find"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Bool"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"a"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),fp=a("p",null,[a("strong",null,"elemIndex"),e(" '可能' (Maybe)返回我们找的元素的索引,若这一元素不存在,就返回 "),a("code",null,"Nothing"),e(".")],-1),wp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"elemIndex"),e("\n"),a("span",{class:"token hvariable"},"elemIndex"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"`elemIndex`"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"10"),e(),a("span",{class:"token operator"},"`elemIndex`"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),xp=a("p",null,[a("strong",null,"elemIndices"),e(" 与"),a("code",null,"elemIndex"),e("相似.")],-1),Sp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token char string"},"' '"),e(),a("span",{class:"token operator"},"`elemIndices`"),e(),a("span",{class:"token string"},'"Where are the spaces?"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"13"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Lp=a("p",null,[a("strong",null,"findIndex"),e(" 与 "),a("code",null,"find"),e(" 相似. "),a("strong",null,"findIndices"),e(" 返回所有符合条件的索引.")],-1),Mp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"findIndex"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"findIndex"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"findIndices"),e(),a("span",{class:"token punctuation"},"("),e("`"),a("span",{class:"token builtin"},"elem"),e("` "),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'A'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'Z'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"Where Are The Caps?"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"14"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Fp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"zipWith3"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token hvariable"},"z"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"z"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"zip4"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Tp=a("p",null,[e("在处理来自文件或其它地方的输入时,"),a("strong",null,"lines"),e(" 会非常有用.")],-1),Ip=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token string"},'"first line\\nsecond line\\nthird line"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"first line"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"second line"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"third line"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ap=a("p",null,[a("strong",null,"unlines"),e(" 是 "),a("code",null,"lines"),e(" 的反函数.")],-1),Cp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"unlines"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"first line"'),e(","),a("span",{class:"token string"},'"second line"'),e(","),a("span",{class:"token string"},'"third line"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token string"},'"first line\\nsecond line\\nthird line\\n"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Op=a("p",null,[a("strong",null,"words"),e(" 和 "),a("strong",null,"unwords"),e(" 可以把一个字串分为一组单词或执行相反的操作.")],-1),Np=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"words"),e(),a("span",{class:"token string"},'"hey these are the words in this sentence"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"these"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"are"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"the"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"words"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"in"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"this"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"sentence"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"words"),e(),a("span",{class:"token string"},'"hey these are the words in this\\nsentence"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"these"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"are"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"the"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"words"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"in"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"this"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"sentence"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"unwords"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"there"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"mate"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token string"},'"hey there mate"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Bp=a("p",null,[a("strong",null,"nub"),e(" 可以将一个 List 中的重复元素全部筛掉.")],-1),Wp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"nub"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"nub"),e(),a("span",{class:"token string"},'"Lots of words and stuff"'),e("\n"),a("span",{class:"token string"},'"Lots fwrdanu"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ep=a("p",null,"delete",-1),$p=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),e(),a("span",{class:"token string"},'"hey there ghang!"'),e("\n"),a("span",{class:"token string"},'"ey there ghang!"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token string"},'"hey there ghang!"'),e("\n"),a("span",{class:"token string"},'"ey tere ghang!"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token char string"},"'h'"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token string"},'"hey there ghang!"'),e("\n"),a("span",{class:"token string"},'"ey tere gang!"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Pp=a("p",null,[a("strong",null,"\\"),e(" 差集")],-1),qp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"\\\\"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"Im a big baby"'),e(),a("span",{class:"token operator"},"\\\\"),e(),a("span",{class:"token string"},'"big"'),e("\n"),a("span",{class:"token string"},'"Im a baby"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Rp=a("p",null,[a("strong",null,"union"),e(" 并集")],-1),zp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token string"},'"hey man"'),e(),a("span",{class:"token operator"},"`union`"),e(),a("span",{class:"token string"},'"man what\'s up"'),e("\n"),a("span",{class:"token string"},'"hey manwt\'sup"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`union`"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Dp=a("p",null,[a("strong",null,"intersection"),e(" 交集")],-1),Jp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`intersect`"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Gp=a("p",null,"insert",-1),_p=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"insert"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"insert"),e(),a("span",{class:"token char string"},"'g'"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'a'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'f'"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token char string"},"'h'"),a("span",{class:"token operator"},".."),a("span",{class:"token char string"},"'z'"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token string"},'"abcdefghijklmnopqrstuvwxyz"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"insert"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Hp=a("h5",{id:"修正-prelude-模块"},[a("a",{class:"header-anchor",href:"#修正-prelude-模块","aria-hidden":"true"},"#"),e(" 修正 Prelude 模块")],-1),Kp=a("p",null,[a("code",null,"length"),e(","),a("code",null,"take"),e(","),a("code",null,"drop"),e(","),a("code",null,"splitAt"),e(","),a("code",null,"!!"),e(" 和 "),a("code",null,"replicate"),a("code",null,"Data.List"),e(" 中包含了更通用的替代版,如: "),a("code",null,"genericLength,genericTake,genericDrop,genericSplitAt,genericIndex"),e(" 和 "),a("code",null,"genericReplicate")],-1),Yp=a("p",null,[a("code",null,"nub"),e(", "),a("code",null,"delete"),e(", "),a("code",null,"union"),e(", "),a("code",null,"intsect"),e(" 和 "),a("code",null,"group"),e(" 函数 也有各自的通用替代版 "),a("code",null,"nubBy"),e(","),a("code",null,"deleteBy"),e(","),a("code",null,"unionBy"),e(","),a("code",null,"intersectBy"),e(" 和 "),a("code",null,"groupBy"),e(", 它们的区别就是前一组函数使用 "),a("code",null,"(==)"),e(" 来测试是否相等,而带 "),a("code",null,"By"),e(" 的那组则取一个函数作参数来判定相等性.")],-1),Vp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"values"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"4.3"),e(","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),e(","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1.2"),e(","),a("span",{class:"token number"},"0.4"),e(","),a("span",{class:"token number"},"2.3"),e(","),a("span",{class:"token number"},"5.9"),e(","),a("span",{class:"token number"},"10.5"),e(","),a("span",{class:"token number"},"29.1"),e(","),a("span",{class:"token number"},"5.3"),e(","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),e(","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"14.5"),e(","),a("span",{class:"token number"},"2.9"),e(","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"groupBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"values"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"4.3"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1.2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5.9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10.5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29.1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5.3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"14.5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2.9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Up=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"on"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"c"),e("\n"),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token hvariable"},"y"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Zp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"groupBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"values"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"4.3"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1.2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5.9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10.5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29.1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5.3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"2.4"),a("span",{class:"token punctuation"},","),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"14.5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2.9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2.3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),jp=a("p",null,[a("code",null,"sort"),e(","),a("code",null,"insert"),e(","),a("code",null,"maximum"),e(" 和 "),a("code",null,"min"),e(" 都有各自的通用版本。 如 "),a("code",null,"groupBy"),e(" 类似,"),a("strong",null,"sortBy"),e(","),a("strong",null,"insertBy"),e(","),a("strong",null,"maximumBy"),e(" 和 "),a("strong",null,"minimumBy"),e(" 都取一个函数来比较两个元素的大小.")],-1),Qp=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"sortBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"compare"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token builtin"},"length"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"xs"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Xp=a("h4",{id:"data-char"},[a("a",{class:"header-anchor",href:"#data-char","aria-hidden":"true"},"#"),e(" Data Char")],-1),nc=a("ul",null,[a("li",null,[a("strong",null,"isControl"),e(" 判断一个字符是否是控制字符。")]),a("li",null,[a("strong",null,"isSpace"),e(" 判断一个字符是否是空格字符,包括空格,tab,换行符等.")]),a("li",null,[a("strong",null,"isLower"),e(" 判断一个字符是否为小写.")]),a("li",null,[a("strong",null,"isUpper"),e(" 判断一个字符是否为大写。")]),a("li",null,[a("strong",null,"isAlpha"),e(" 判断一个字符是否为字母.")]),a("li",null,[a("strong",null,"isAlphaNum"),e(" 判断一个字符是否为字母或数字.")]),a("li",null,[a("strong",null,"isPrint"),e(" 判断一个字符是否是可打印的.")]),a("li",null,[a("strong",null,"isDigit"),e(" 判断一个字符是否为数字.")]),a("li",null,[a("strong",null,"isOctDigit"),e(" 判断一个字符是否为八进制数字.")]),a("li",null,[a("strong",null,"isHexDigit"),e(" 判断一个字符是否为十六进制数字.")]),a("li",null,[a("strong",null,"isLetter"),e(" 判断一个字符是否为字母.")]),a("li",null,[a("strong",null,"isMark"),e(" 判断是否为 unicode 注音字符,你如果是法国人就会经常用到的.")]),a("li",null,[a("strong",null,"isNumber"),e(" 判断一个字符是否为数字.")]),a("li",null,[a("strong",null,"isPunctuation"),e(" 判断一个字符是否为标点符号.")]),a("li",null,[a("strong",null,"isSymbol"),e("判断一个字符是否为货币符号.")]),a("li",null,[a("strong",null,"isSeparator"),e(" 判断一个字符是否为 unicode 空格或分隔符.")]),a("li",null,[a("strong",null,"isAscii"),e(" 判断一个字符是否在 unicode 字母表的前 128 位。")]),a("li",null,[a("strong",null,"isLatin1"),e(" 判断一个字符是否在 unicode 字母表的前 256 位.")]),a("li",null,[a("strong",null,"isAsciiUpper"),e(" 判断一个字符是否为大写的 ascii 字符.")]),a("li",null,[a("strong",null,"isAsciiLower"),e(" 判断一个字符是否为小写的 ascii 字符.")])],-1),sc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token builtin"},"isAlphaNum"),e(),a("span",{class:"token string"},'"bobby283"'),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"all"),e(),a("span",{class:"token builtin"},"isAlphaNum"),e(),a("span",{class:"token string"},'"eddy the fish!"'),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ac=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"words"),e(),a("span",{class:"token string"},'"hey guys its me"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"guys"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"its"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"me"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"groupBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token builtin"},"isSpace"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token string"},'"hey guys its me"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'" "'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"guys"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'" "'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"its"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'" "'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"me"'),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),ec=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"not"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"any"),e(),a("span",{class:"token builtin"},"isSpace"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"groupBy"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"=="),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`on`"),e(),a("span",{class:"token builtin"},"isSpace"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token string"},'"hey guys its me"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"hey"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"guys"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"its"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"me"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),tc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"' '"),e("\n"),a("span",{class:"token constant"},"Space"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"'A'"),e("\n"),a("span",{class:"token constant"},"UppercaseLetter"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"'a'"),e("\n"),a("span",{class:"token constant"},"LowercaseLetter"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"'.'"),e("\n"),a("span",{class:"token constant"},"OtherPunctuation"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token char string"},"'9'"),e("\n"),a("span",{class:"token constant"},"DecimalNumber"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token hvariable"},"generalCategory"),e(),a("span",{class:"token string"},'" \\t\\nA9?|"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"Space"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Control"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"Control"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"UppercaseLetter"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"DecimalNumber"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"OtherPunctuation"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"MathSymbol"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),lc=a("ul",null,[a("li",null,[a("strong",null,"toUpper"),e(" 将一个字符转为大写字母,若该字符不是小写字母,就按原值返回.")]),a("li",null,[a("strong",null,"toLower"),e(" 将一个字符转为小写字母,若该字符不是大写字母,就按原值返回.")]),a("li",null,[a("strong",null,"toTitle"),e(" 将一个字符转为 title-case,对大多数字元而言,title-case 就是大写.")]),a("li",null,[a("strong",null,"digitToInt"),e(" 将一个字符转为 Int 值,而这一字符必须得在 "),a("code",null,"'1'..'9','a'..'f'"),e("或"),a("code",null,"'A'..'F'"),e(" 的范围之内.")])],-1),oc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"digitToInt"),e(),a("span",{class:"token string"},'"34538"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"digitToInt"),e(),a("span",{class:"token string"},'"FF85AB"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),pc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"intToDigit"),e(),a("span",{class:"token number"},"15"),e("\n"),a("span",{class:"token char string"},"'f'"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"intToDigit"),e(),a("span",{class:"token number"},"5"),e("\n"),a("span",{class:"token char string"},"'5'"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),cc=a("p",null,[a("strong",null,"ord"),e(" 与 "),a("strong",null,"char"),e(" 函数可以将字符与其对应的数字相互转换.")],-1),rc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"ord"),e(),a("span",{class:"token char string"},"'a'"),e("\n"),a("span",{class:"token number"},"97"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"chr"),e(),a("span",{class:"token number"},"97"),e("\n"),a("span",{class:"token char string"},"'a'"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"ord"),e(),a("span",{class:"token string"},'"abcdefgh"'),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"97"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"98"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"99"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"101"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"102"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"103"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"104"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),uc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"encode"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"encode"),e(),a("span",{class:"token hvariable"},"shift"),e(),a("span",{class:"token hvariable"},"msg"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"ords"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"ord"),e(),a("span",{class:"token hvariable"},"msg"),e("\n "),a("span",{class:"token hvariable"},"shifted"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token hvariable"},"shift"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"ords"),e("\n "),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"chr"),e(),a("span",{class:"token hvariable"},"shifted"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),ic=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"decode"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"decode"),e(),a("span",{class:"token hvariable"},"shift"),e(),a("span",{class:"token hvariable"},"msg"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"encode"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"negate"),e(),a("span",{class:"token hvariable"},"shift"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"msg"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),kc=a("h4",{id:"data-map"},[a("a",{class:"header-anchor",href:"#data-map","aria-hidden":"true"},"#"),e(" Data Map")],-1),bc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"snd"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"head"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),mc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Nothing"),e("\n"),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token keyword"},"then"),e("\n "),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"v"),e("\n "),a("span",{class:"token keyword"},"else"),e("\n "),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),hc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Eq"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Maybe"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token hvariable"},"findKey"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"key"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token constant"},"Nothing"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),dc=a("p",null,[a("strong",null,"fromList"),e(" 取一个关联列表,返回一个与之等价的 Map。")],-1),vc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),e(","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token hvariable"},"v"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),gc=a("p",null,"若其中存在重复的键,就将其忽略.",-1),yc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.empty"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),fc=a("p",null,"insert",-1),wc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token number"},"600"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token number"},"200"),e(),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"100"),e(" "),a("span",{class:"token hvariable"},"Map.empty"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"200"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"600"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token number"},"600"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token number"},"200"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.empty"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"200"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"600"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),xc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"fromList"),e("' "),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token hvariable"},"v"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e("' "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"foldr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"acc"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token hvariable"},"acc"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"Map.empty"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Sc=a("p",null,"null",-1),Lc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token hvariable"},"Map.empty"),e("\n"),a("span",{class:"token constant"},"True"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Mc=a("p",null,"size",-1),Fc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.size"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"5"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Tc=a("p",null,"singleton",-1),Ic=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.singleton"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"9"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"5"),e(),a("span",{class:"token number"},"9"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.singleton"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"9"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ac=a("p",null,"lookup",-1),Cc=a("p",null,"member",-1),Oc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.member"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.member"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Nc=a("p",null,[a("strong",null,"map"),e(" 与 "),a("strong",null,"filter"),e(" 与其对应的 "),a("code",null,"List"),e(" 版本相似")],-1),Bc=a("p",null,[a("code",null,"toList"),e(" 是 "),a("code",null,"fromList"),e(" 的反函数")],-1),Wc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.toList"),a("span",{class:"token operator"}," . "),a("span",{class:"token hvariable"},"Map.insert"),e(),a("span",{class:"token number"},"9"),e(),a("span",{class:"token number"},"2"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.singleton"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ec=a("p",null,[a("strong",null,"keys"),e(" 与 "),a("strong",null,"elems")],-1),$c=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"phoneBook"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"betty"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"555-2938"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"betty"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"342-2492"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"bonnie"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"452-2928"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"patsy"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"493-2928"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"patsy"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"943-2929"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"patsy"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"827-9162"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"lucille"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"205-2928"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"wendy"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"939-8282"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"penny"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"853-2492"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"penny"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"555-2111"'),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),Pc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Map.fromListWith"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"number1"),e(),a("span",{class:"token hvariable"},"number2"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"number1"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'", "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"number2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),qc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token string"},'"patsy"'),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"phoneBook"),e("\n"),a("span",{class:"token string"},'"827-9162, 943-2929, 493-2928"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token string"},'"wendy"'),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"phoneBook"),e("\n"),a("span",{class:"token string"},'"939-8282"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token string"},'"betty"'),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"phoneBook"),e("\n"),a("span",{class:"token string"},'"342-2492,555-2938"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Rc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Ord"),e(),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),e(","),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Map.Map"),e(),a("span",{class:"token hvariable"},"k"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Map.fromListWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"k"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"xs"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Map"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token string"},'"patsy"'),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"phoneBookToMap"),e(),a("span",{class:"token hvariable"},"phoneBook"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"827-9162"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"943-2929"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"493-2928"'),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),zc=a("p",null,"在遇到重复元素时,单选最大的那个值.",-1),Dc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.fromListWith"),e(),a("span",{class:"token builtin"},"max"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Jc=a("p",null,"将相同键的值都加在一起.",-1),Gc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.fromListWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"29"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"108"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"62"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"37"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),_c=a("p",null,"insertWith",-1),Hc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Map.insertWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token number"},"3"),e(),a("span",{class:"token number"},"100"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Map.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"103"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"339"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"104"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"103"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"339"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Kc=a("h4",{id:"data-set"},[a("a",{class:"header-anchor",href:"#data-set","aria-hidden":"true"},"#"),e(" Data Set")],-1),Yc=a("p",null,"内部元素排序且唯一",-1),Vc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.Set "),a("span",{class:"token keyword"},"as"),e(" Set")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Uc=a("p",null,"fromList",-1),Zc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"set1"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token hvariable"},"text1"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"set2"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token hvariable"},"text2"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"set1"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'" .?AIRadefhijlmnorstuy"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"set2"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'" !Tabcdefghilmnorstuvwy"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),jc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.intersection"),e(),a("span",{class:"token hvariable"},"set1"),e(),a("span",{class:"token hvariable"},"set2"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'" adefhilmnorstuy"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Qc=a("p",null,[a("strong",null,"difference"),e(" 差集")],-1),Xc=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.difference"),e(),a("span",{class:"token hvariable"},"set1"),e(),a("span",{class:"token hvariable"},"set2"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'".?AIRj"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.difference"),e(),a("span",{class:"token hvariable"},"set2"),e(),a("span",{class:"token hvariable"},"set1"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'"!Tbcgvw"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),nr=a("p",null,[a("code",null,"union"),e(" 并集")],-1),sr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.union"),e(),a("span",{class:"token hvariable"},"set1"),e(),a("span",{class:"token hvariable"},"set2"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token string"},'" !.?AIRTabcdefghijlmnorstuvwy"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ar=a("p",null,[a("code",null,"null"),e(","),a("code",null,"size"),e(","),a("code",null,"member"),e(","),a("code",null,"empty"),e(","),a("code",null,"singleton"),e(","),a("code",null,"insert"),e(","),a("code",null,"delete")],-1),er=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Set"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token hvariable"},"Set.empty"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Set"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.size"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.singleton"),e(),a("span",{class:"token number"},"9"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.insert"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.insert"),e(),a("span",{class:"token number"},"8"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token operator"},".."),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"9"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.delete"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),tr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`Set.isSubsetOf`"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`Set.isSubsetOf`"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"True"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`Set.isProperSubsetOf`"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"`Set.isSubsetOf`"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token constant"},"False"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),lr=a("p",null,[e("执行 "),a("code",null,"map"),e(" 和 "),a("code",null,"filter"),e(":")],-1),or=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Set"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token builtin"},"odd"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token constant"},"Set"),a("span",{class:"token punctuation"},"."),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"fromList"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"6"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),pr=a("ul",null,[a("li",null,"删除重复元素")],-1),cr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"setNub"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"Set.toList"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"Set.fromList"),e(),a("span",{class:"token hvariable"},"xs"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"setNub"),e(),a("span",{class:"token string"},'"HEY WHATS CRACKALACKIN"'),e("\n"),a("span",{class:"token string"},'" ACEHIKLNRSTWY"'),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"nub"),e(),a("span",{class:"token string"},'"HEY WHATS CRACKALACKIN"'),e("\n"),a("span",{class:"token string"},'"HEY WATSCRKLIN"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),rr=a("h2",{id:"输入与输出"},[a("a",{class:"header-anchor",href:"#输入与输出","aria-hidden":"true"},"#"),e(" 输入与输出")],-1),ur=a("h3",{id:"io-action"},[a("a",{class:"header-anchor",href:"#io-action","aria-hidden":"true"},"#"),e(" IO action")],-1),ir=a("p",null,[a("code",null,"name <- IO action"),e(": 将 action 绑定至名字上,IO String -> String")],-1),kr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getLine"),e("\n"),a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token constant"},"String"),e("\n\n"),a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token constant"},"String"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),br=a("p",null,[e("在一个"),a("code",null,"do block"),e("中,最后一个"),a("code",null,"action"),e("不能绑定任何名字,它会被绑定成为"),a("code",null,"do block"),e("的结果值.")],-1),mr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"foo"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token string"},'"Hello, what\'s your name?"'),e("\n "),a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getLine"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Hey "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"name"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'", you rock!"'),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),hr=a("h4",{id:"return"},[a("a",{class:"header-anchor",href:"#return","aria-hidden":"true"},"#"),e(" return")],-1),dr=a("ul",null,[a("li",null,"return 功能:将 pure value 包成 I/O actions,不会终止函数/程序"),a("li",null,[e("return 作用: "),a("ul",null,[a("li",null,"if condition then I/O action else I/O action"),a("li",null,[e("改变"),a("code",null,"do block形成的I/O action"),e("的结果值: otherIOAction -> return pureValue")])])])],-1),vr=a("blockquote",null,[a("p",null,'e.g return "haha" - String -> IO String')],-1),gr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"line"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getLine"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token builtin"},"null"),e(),a("span",{class:"token hvariable"},"line"),e("\n "),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token hvariable"},"reverseWords"),e(),a("span",{class:"token hvariable"},"line"),e("\n "),a("span",{class:"token hvariable"},"main"),e("\n\n"),a("span",{class:"token hvariable"},"reverseWords"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"reverseWords"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"unwords"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"reverse"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"words"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),yr=a("h3",{id:"command-line"},[a("a",{class:"header-anchor",href:"#command-line","aria-hidden":"true"},"#"),e(" Command Line")],-1),fr=a("blockquote",null,[a("p",null,"System.Environment")],-1),wr=a("ul",null,[a("li",null,[e("getArgs: "),a("code",null,"getArgs :: IO [String]")]),a("li",null,[e("getProgName: "),a("code",null,"getProgName :: IO String")])],-1),xr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.Environment")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"args"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"getArgs"),e("\n "),a("span",{class:"token hvariable"},"progName"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"getProgName"),e("\n "),a("span",{class:"token builtin"},"mapM"),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token hvariable"},"args"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token hvariable"},"progName"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),Sr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.Environment")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.Directory")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.List")]),e("\n\n"),a("span",{class:"token hvariable"},"dispatch"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"dispatch"),e(),a("span",{class:"token operator"},"="),e(" "),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"add"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"add"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"view"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"view"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"remove"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"remove"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"]"),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"command"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"args"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"getArgs"),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token hvariable"},"action"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"lookup"),e(),a("span",{class:"token hvariable"},"command"),e(),a("span",{class:"token hvariable"},"dispatch"),e("\n "),a("span",{class:"token hvariable"},"action"),e(),a("span",{class:"token hvariable"},"args"),e("\n\n"),a("span",{class:"token hvariable"},"add"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"add"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"fileName"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"todoItem"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"appendFile"),e(),a("span",{class:"token hvariable"},"fileName"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"todoItem"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'"\\n"'),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"view"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"view"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"fileName"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"readFile"),e(),a("span",{class:"token hvariable"},"fileName"),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"todoTasks"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token hvariable"},"contents"),e("\n "),a("span",{class:"token hvariable"},"numberedTasks"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"zipWith"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token hvariable"},"line"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"n"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" - "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"line"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token operator"},".."),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token hvariable"},"todoTasks"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"unlines"),e(),a("span",{class:"token hvariable"},"numberedTasks"),e("\n\n"),a("span",{class:"token hvariable"},"remove"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"remove"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token hvariable"},"fileName"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"numberString"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"openFile"),e(),a("span",{class:"token hvariable"},"fileName"),e(),a("span",{class:"token constant"},"ReadMode"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"tempName"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"tempHandle"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"openTempFile"),e(),a("span",{class:"token string"},'"."'),e(),a("span",{class:"token string"},'"temp"'),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"hGetContents"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"number"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"read"),e(),a("span",{class:"token hvariable"},"numberString"),e("\n "),a("span",{class:"token hvariable"},"todoTasks"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token hvariable"},"contents"),e("\n "),a("span",{class:"token hvariable"},"newTodoItems"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"delete"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"todoTasks"),e(),a("span",{class:"token operator"},"!!"),e(),a("span",{class:"token hvariable"},"number"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"todoTasks"),e("\n "),a("span",{class:"token hvariable"},"hPutStr"),e(),a("span",{class:"token hvariable"},"tempHandle"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"unlines"),e(),a("span",{class:"token hvariable"},"newTodoItems"),e("\n "),a("span",{class:"token hvariable"},"hClose"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token hvariable"},"hClose"),e(),a("span",{class:"token hvariable"},"tempHandle"),e("\n "),a("span",{class:"token hvariable"},"removeFile"),e(),a("span",{class:"token hvariable"},"fileName"),e("\n "),a("span",{class:"token hvariable"},"renameFile"),e(),a("span",{class:"token hvariable"},"tempName"),e(),a("span",{class:"token hvariable"},"fileName"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br")])],-1),Lr=a("h3",{id:"bytestring"},[a("a",{class:"header-anchor",href:"#bytestring","aria-hidden":"true"},"#"),e(" ByteString")],-1),Mr=a("ul",null,[a("li",null,[a("code",null,"Data.ByteString")]),a("li",null,[a("code",null,"Data.ByteString.Lazy")])],-1),Fr=a("p",null,"lazy byteStrings 像装了一堆大小为 64K 的 strict byteStrings 的 list",-1),Tr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.ByteString.Lazy "),a("span",{class:"token keyword"},"as"),e(" B")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(),a("span",{class:"token keyword"},"qualified"),e(" Data.ByteString "),a("span",{class:"token keyword"},"as"),e(" S")]),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Ir=a("ul",null,[a("li",null,"pack/unpack"),a("li",null,"fromChunks/toChunks"),a("li",null,"cons/empty/head/tail/init/null/length/map/reverse/foldl/foldr/concat/takeWhile/filter")],-1),Ar=a("h3",{id:"常用输入输出函数"},[a("a",{class:"header-anchor",href:"#常用输入输出函数","aria-hidden":"true"},"#"),e(" 常用输入输出函数")],-1),Cr=a("h4",{id:"输出"},[a("a",{class:"header-anchor",href:"#输出","aria-hidden":"true"},"#"),e(" 输出")],-1),Or=a("h5",{id:"output-string"},[a("a",{class:"header-anchor",href:"#output-string","aria-hidden":"true"},"#"),e(" Output String")],-1),Nr=a("p",null,[a("code",null,"putChar"),e("/"),a("code",null,"putStr"),e("/"),a("code",null,"putStrLn"),e(":")],-1),Br=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putChar"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Wr=a("h5",{id:"print"},[a("a",{class:"header-anchor",href:"#print","aria-hidden":"true"},"#"),e(" print")],-1),Er=a("p",null,"print = putStrLn . show",-1),$r=a("h5",{id:"file-system-io"},[a("a",{class:"header-anchor",href:"#file-system-io","aria-hidden":"true"},"#"),e(" File System IO")],-1),Pr=a("ul",null,[a("li",null,[a("code",null,"hPutStr")]),a("li",null,[a("code",null,"hPutStrLn")])],-1),qr=a("h6",{id:"write-file"},[a("a",{class:"header-anchor",href:"#write-file","aria-hidden":"true"},"#"),e(" Write File")],-1),Rr=a("p",null,[a("code",null,"writefile :: FilePath -> String -> IO ()"),e(" - WriteMode, not AppendMode")],-1),zr=a("h6",{id:"append-file"},[a("a",{class:"header-anchor",href:"#append-file","aria-hidden":"true"},"#"),e(" Append File")],-1),Dr=a("h4",{id:"输入"},[a("a",{class:"header-anchor",href:"#输入","aria-hidden":"true"},"#"),e(" 输入")],-1),Jr=a("h5",{id:"getchar-io-char"},[a("a",{class:"header-anchor",href:"#getchar-io-char","aria-hidden":"true"},"#"),e(" getChar :: IO Char")],-1),Gr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getChar"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"/="),e(),a("span",{class:"token char string"},"' '"),e("\n "),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putChar"),e(),a("span",{class:"token hvariable"},"c"),e("\n "),a("span",{class:"token hvariable"},"main"),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),_r=a("h5",{id:"get-contents"},[a("a",{class:"header-anchor",href:"#get-contents","aria-hidden":"true"},"#"),e(" Get Contents")],-1),Hr=a("p",null,"getContents :: IO String (Lazy I/O) - 内容暂存在文件,需要使用时读取至内存区",-1),Kr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.Char")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getContents"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"toUpper"),e(),a("span",{class:"token hvariable"},"contents"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Yr=a("h5",{id:"file"},[a("a",{class:"header-anchor",href:"#file","aria-hidden":"true"},"#"),e(" File")],-1),Vr=a("h6",{id:"handle"},[a("a",{class:"header-anchor",href:"#handle","aria-hidden":"true"},"#"),e(" handle")],-1),Ur=a("ul",null,[a("li",null,[a("p",null,[a("code",null,"data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode")])]),a("li",null,[a("p",null,[a("code",null,"openFile :: FilePath(String) -> IOMode -> IO Handle")])]),a("li",null,[a("p",null,[a("code",null,"hGetContents :: Handle -> IO String")])]),a("li",null,[a("p",null,[a("code",null,"hClose :: Handle -> IO ()")])]),a("li",null,[a("p",null,[a("code",null,"hGetChar")])]),a("li",null,[a("p",null,[a("code",null,"hGetLine")])])],-1),Zr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"openFile"),e(),a("span",{class:"token string"},'"girlfriend.txt"'),e(),a("span",{class:"token constant"},"ReadMode"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"hGetContents"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token hvariable"},"contents"),e("\n "),a("span",{class:"token hvariable"},"hClose"),e(),a("span",{class:"token hvariable"},"handle"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),jr=a("h6",{id:"with-file"},[a("a",{class:"header-anchor",href:"#with-file","aria-hidden":"true"},"#"),e(" With File")],-1),Qr=a("p",null,[a("code",null,"withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a")],-1),Xr=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"withFile"),e(),a("span",{class:"token string"},'"girlfriend.txt"'),e(),a("span",{class:"token constant"},"ReadMode"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"hGetContents"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token hvariable"},"contents"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),nu=a("h6",{id:"read-file"},[a("a",{class:"header-anchor",href:"#read-file","aria-hidden":"true"},"#"),e(" Read File")],-1),su=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"readFile"),e(),a("span",{class:"token string"},'"girlfriend.txt"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),au=a("h4",{id:"action"},[a("a",{class:"header-anchor",href:"#action","aria-hidden":"true"},"#"),e(" Action")],-1),eu=a("h5",{id:"when"},[a("a",{class:"header-anchor",href:"#when","aria-hidden":"true"},"#"),e(" When")],-1),tu=a("p",null,"Control.Monad.when :: (Applicative f) => Bool -> f () -> f ()",-1),lu=a("p",null,[a("code",null,"when bool表达式 I/O-Action"),e(" - 真时返回 Action,假时"),a("code",null,"return ()")],-1),ou=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Control.Monad")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getChar"),e("\n "),a("span",{class:"token hvariable"},"when"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"c"),e(),a("span",{class:"token operator"},"/="),e(),a("span",{class:"token char string"},"' '"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putChar"),e(),a("span",{class:"token hvariable"},"c"),e("\n "),a("span",{class:"token hvariable"},"main"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),pu=a("h5",{id:"sequence"},[a("a",{class:"header-anchor",href:"#sequence","aria-hidden":"true"},"#"),e(" sequence")],-1),cu=a("p",null,[e("sequence :: "),a("code",null,"[IO a]"),e(" -> IO "),a("code",null,"[a]")],-1),ru=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"rs"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"sequence"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token builtin"},"getLine"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token builtin"},"getLine"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token builtin"},"getLine"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token builtin"},"print"),e(),a("span",{class:"token hvariable"},"rs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),uu=a("h5",{id:"map"},[a("a",{class:"header-anchor",href:"#map","aria-hidden":"true"},"#"),e(" Map")],-1),iu=a("p",null,"mapM, Control.Monad.forM:",-1),ku=a("p",null,"= sequence . map",-1),bu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"mapM"),e(),a("span",{class:"token builtin"},"print"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"1"),e("\n"),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token number"},"3"),e("\n"),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token builtin"},"mapM_"),e(),a("span",{class:"token builtin"},"print"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e("\n"),a("span",{class:"token number"},"1"),e("\n"),a("span",{class:"token number"},"2"),e("\n"),a("span",{class:"token number"},"3"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),mu=a("h5",{id:"control-monad-forever"},[a("a",{class:"header-anchor",href:"#control-monad-forever","aria-hidden":"true"},"#"),e(" Control Monad forever")],-1),hu=a("p",null,"接受一个 I/O action 并回传一个永远作同一件事的 I/O action",-1),du=a("p",null,"以下代码实现了循环结构:",-1),vu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Control.Monad")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Data.Char")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"forever"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token string"},'"Give me some input: "'),e("\n "),a("span",{class:"token hvariable"},"l"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"getLine"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token builtin"},"toUpper"),e(),a("span",{class:"token hvariable"},"l"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),gu=a("h5",{id:"hsetbuffering"},[a("a",{class:"header-anchor",href:"#hsetbuffering","aria-hidden":"true"},"#"),e(" hSetBuffering")],-1),yu=a("p",null,[a("code",null,"data BufferMode = NoBuffering | LineBuffering | BlockBuffering (Maybe Int)"),a("code",null,"hSetBuffering :: Handle -> BufferMode -> IO ()")],-1),fu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"withFile"),e(),a("span",{class:"token string"},'"something.txt"'),e(),a("span",{class:"token constant"},"ReadMode"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"hSetBuffering"),e(),a("span",{class:"token hvariable"},"handle"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token constant"},"BlockBuffering"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Just"),e(),a("span",{class:"token number"},"2048"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"hGetContents"),e(),a("span",{class:"token hvariable"},"handle"),e("\n "),a("span",{class:"token builtin"},"putStr"),e(),a("span",{class:"token hvariable"},"contents"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),wu=a("h5",{id:"hflush"},[a("a",{class:"header-anchor",href:"#hflush","aria-hidden":"true"},"#"),e(" hFlush")],-1),xu=a("h4",{id:"other"},[a("a",{class:"header-anchor",href:"#other","aria-hidden":"true"},"#"),e(" Other")],-1),Su=a("h5",{id:"lines"},[a("a",{class:"header-anchor",href:"#lines","aria-hidden":"true"},"#"),e(" lines")],-1),Lu=a("p",null,[a("code",null,"lines :: String -> [String]"),e(" - 按换行符将段落切割成句子")],-1),Mu=a("h5",{id:"interact"},[a("a",{class:"header-anchor",href:"#interact","aria-hidden":"true"},"#"),e(" interact")],-1),Fu=a("ul",null,[a("li",null,"arguments: String -> String 的函数"),a("li",null,"return: 一个 I/O action"),a("li",null,"function: I/O action 会读取输入,调用提供的函数,然后把函数的结果打印出来")],-1),Tu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"interact"),e(),a("span",{class:"token hvariable"},"shortLinesOnly"),e("\n\n"),a("span",{class:"token hvariable"},"shortLinesOnly"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token hvariable"},"shortLinesOnly"),e(),a("span",{class:"token hvariable"},"input"),e(),a("span",{class:"token operator"},"="),e("\n "),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token hvariable"},"allLines"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token hvariable"},"input"),e("\n "),a("span",{class:"token hvariable"},"shortLines"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"line"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"line"),e(),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"allLines"),e("\n "),a("span",{class:"token hvariable"},"result"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"unlines"),e(),a("span",{class:"token hvariable"},"shortLines"),e("\n "),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token hvariable"},"result"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),Iu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"interact"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token builtin"},"unlines"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"filter"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"<"),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"length"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"lines"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Au=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"respondPalindromes"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"unlines"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"map"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"->"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token hvariable"},"isPalindrome"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token keyword"},"then"),e(),a("span",{class:"token string"},'"palindrome"'),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token string"},'"not a palindrome"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"}," . "),a("span",{class:"token builtin"},"lines"),e("\n "),a("span",{class:"token keyword"},"where"),e(),a("span",{class:"token hvariable"},"isPalindrome"),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token builtin"},"reverse"),e(),a("span",{class:"token hvariable"},"xs"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Cu=a("h5",{id:"system-directory"},[a("a",{class:"header-anchor",href:"#system-directory","aria-hidden":"true"},"#"),e(" System Directory")],-1),Ou=a("ul",null,[a("li",null,"removeFile"),a("li",null,"renameFile")],-1),Nu=a("h2",{id:"异常"},[a("a",{class:"header-anchor",href:"#异常","aria-hidden":"true"},"#"),e(" 异常")],-1),Bu=a("p",null,"System.IO.Error",-1),Wu=a("h3",{id:"catch"},[a("a",{class:"header-anchor",href:"#catch","aria-hidden":"true"},"#"),e(" catch")],-1),Eu=a("p",null,"catch :: IO a -> (IOError -> IO a) -> IO a",-1),$u=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.Environment")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO")]),e("\n"),a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" System.IO.Error")]),e("\n\n"),a("span",{class:"token hvariable"},"main"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"toTry"),e(" `"),a("span",{class:"token builtin"},"catch"),e("` "),a("span",{class:"token hvariable"},"handler"),e("\n\n"),a("span",{class:"token hvariable"},"toTry"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"toTry"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"fileName"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"_"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"getArgs"),e("\n "),a("span",{class:"token hvariable"},"contents"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token builtin"},"readFile"),e(),a("span",{class:"token hvariable"},"fileName"),e("\n "),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token string"},'"The file has "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"lines"),e(),a("span",{class:"token hvariable"},"contents"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token string"},'" lines!"'),e("\n\n"),a("span",{class:"token hvariable"},"handler"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"IOError"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"IO"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"handler"),e(),a("span",{class:"token hvariable"},"e"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"isDoesNotExistError"),e(),a("span",{class:"token hvariable"},"e"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"putStrLn"),e(),a("span",{class:"token string"},'"The file doesn\'t exist!"'),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"isFullError"),e(),a("span",{class:"token hvariable"},"e"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"freeSomeSpace"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token hvariable"},"isIllegalOperation"),e(),a("span",{class:"token hvariable"},"e"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"notifyCops"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token builtin"},"otherwise"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token builtin"},"ioError"),e(),a("span",{class:"token hvariable"},"e"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),Pu=a("h2",{id:"advanced-monad"},[a("a",{class:"header-anchor",href:"#advanced-monad","aria-hidden":"true"},"#"),e(" Advanced Monad")],-1),qu=a("h3",{id:"writer-monad"},[a("a",{class:"header-anchor",href:"#writer-monad","aria-hidden":"true"},"#"),e(" Writer Monad")],-1),Ru=a("p",null,"Writer 可以让我们在计算的同时搜集所有 log 纪录,并汇集成一个 log 并附加在结果上",-1),zu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"applyLog"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},","),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"applyLog"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token builtin"},"log"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"newLog"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),a("span",{class:"token builtin"},"log"),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token hvariable"},"newLog"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Du=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"30"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"A freaking platoon."'),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`applyLog`"),e(),a("span",{class:"token hvariable"},"isBigGang"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"True"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"A freaking platoon.Compared gang size to 9"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"BathCat"'),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"Got outlaw name."'),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"`applyLog`"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token builtin"},"length"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token string"},'"Applied length"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"Got outlaw name.Applied length"'),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Ju=a("h4",{id:"control-monad-writer"},[a("a",{class:"header-anchor",href:"#control-monad-writer","aria-hidden":"true"},"#"),e(" Control Monad Writer")],-1),Gu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Monoid"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"mempty"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"v"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"v"),e("'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token keyword"},"in"),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"y"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"v"),e(),a("span",{class:"token operator"},"`mappend`"),e(),a("span",{class:"token hvariable"},"v"),e("'"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),_u=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Control.Monad.Writer")]),e("\n\n"),a("span",{class:"token hvariable"},"logNumber"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"logNumber"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},'"Got number: "'),e(),a("span",{class:"token operator"},"++"),e(),a("span",{class:"token builtin"},"show"),e(),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"multiWithLog"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Writer"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"String"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"multiWithLog"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"logNumber"),e(),a("span",{class:"token number"},"3"),e("\n "),a("span",{class:"token hvariable"},"b"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"logNumber"),e(),a("span",{class:"token number"},"5"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token operator"},"*"),a("span",{class:"token hvariable"},"b"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),Hu=a("h3",{id:"reader-monad"},[a("a",{class:"header-anchor",href:"#reader-monad","aria-hidden":"true"},"#"),e(" Reader Monad")],-1),Ku=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"->"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"r"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"_"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"w"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token hvariable"},"w"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token hvariable"},"w"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Yu=a("h3",{id:"state-monad"},[a("a",{class:"header-anchor",href:"#state-monad","aria-hidden":"true"},"#"),e(" State Monad")],-1),Vu=a("h4",{id:"control-monad-state"},[a("a",{class:"header-anchor",href:"#control-monad-state","aria-hidden":"true"},"#"),e(" Control Monad State")],-1),Uu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"newtype"),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token hvariable"},"runState"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token hvariable"},"h"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token keyword"},"let"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token hvariable"},"newState"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"h"),e(),a("span",{class:"token hvariable"},"s"),e("\n "),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token hvariable"},"g"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"a"),e("\n "),a("span",{class:"token keyword"},"in"),e(" "),a("span",{class:"token hvariable"},"g"),e(),a("span",{class:"token hvariable"},"newState"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Zu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"get"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"s"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"put"),e(),a("span",{class:"token hvariable"},"newState"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"s"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"newState"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ju=a("h4",{id:"state-monad-case"},[a("a",{class:"header-anchor",href:"#state-monad-case","aria-hidden":"true"},"#"),e(" State Monad Case")],-1),Qu=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token import-statement"},[a("span",{class:"token keyword"},"import"),e(" Control.Monad.State")]),e("\n\n"),a("span",{class:"token hvariable"},"pop"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"pop"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"x"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"Int"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token operator"},"$"),e(),a("span",{class:"token operator"},"\\"),a("span",{class:"token hvariable"},"xs"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),a("span",{class:"token hvariable"},"a"),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"xs"),a("span",{class:"token punctuation"},")"),e("\n\n"),a("span",{class:"token hvariable"},"stackManipulation"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"State"),e(),a("span",{class:"token constant"},"Stack"),e(),a("span",{class:"token constant"},"Int"),e("\n"),a("span",{class:"token hvariable"},"stackManipulation"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"do"),e("\n "),a("span",{class:"token hvariable"},"push"),e(),a("span",{class:"token number"},"3"),e("\n "),a("span",{class:"token hvariable"},"a"),e(),a("span",{class:"token operator"},"<-"),e(),a("span",{class:"token hvariable"},"pop"),e("\n "),a("span",{class:"token hvariable"},"pop"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br")])],-1),Xu=a("h3",{id:"error-monad"},[a("a",{class:"header-anchor",href:"#error-monad","aria-hidden":"true"},"#"),e(" Error Monad")],-1),ni=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"instance"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Error"),e(),a("span",{class:"token hvariable"},"e"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"Monad"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Either"),e(),a("span",{class:"token hvariable"},"e"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"where"),e("\n "),a("span",{class:"token builtin"},"return"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Right"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token constant"},"Right"),e(),a("span",{class:"token hvariable"},"x"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token hvariable"},"x"),e("\n "),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token hvariable"},"err"),e(),a("span",{class:"token operator"},">>="),e(),a("span",{class:"token hvariable"},"f"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token hvariable"},"err"),e("\n "),a("span",{class:"token builtin"},"fail"),e(),a("span",{class:"token hvariable"},"msg"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"Left"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token hvariable"},"strMsg"),e(),a("span",{class:"token hvariable"},"msg"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),si=a("div",{class:"language-haskell line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token operator"},":"),a("span",{class:"token hvariable"},"t"),e(),a("span",{class:"token hvariable"},"strMsg"),e("\n"),a("span",{class:"token hvariable"},"strMsg"),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token constant"},"Error"),e(),a("span",{class:"token hvariable"},"a"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"=>"),e(),a("span",{class:"token constant"},"String"),e(),a("span",{class:"token operator"},"->"),e(),a("span",{class:"token hvariable"},"a"),e("\n"),a("span",{class:"token hvariable"},"ghci"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token hvariable"},"strMsg"),e(),a("span",{class:"token string"},'"boom!"'),e(),a("span",{class:"token operator"},"::"),e(),a("span",{class:"token constant"},"String"),e("\n"),a("span",{class:"token string"},'"boom!"'),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ai=a("h4",{id:"liftm"},[a("a",{class:"header-anchor",href:"#liftm","aria-hidden":"true"},"#"),e(" liftM")],-1),ei=a("h4",{id:"join"},[a("a",{class:"header-anchor",href:"#join","aria-hidden":"true"},"#"),e(" join")],-1),ti=a("h4",{id:"filterm"},[a("a",{class:"header-anchor",href:"#filterm","aria-hidden":"true"},"#"),e(" filterM")],-1),li=a("h4",{id:"foldm"},[a("a",{class:"header-anchor",href:"#foldm","aria-hidden":"true"},"#"),e(" foldM")],-1),oi=a("h4",{id:"组合函数"},[a("a",{class:"header-anchor",href:"#组合函数","aria-hidden":"true"},"#"),e(),a("code",null,"<=<"),e("(组合函数)")],-1),pi=a("h3",{id:"self-defined-monad"},[a("a",{class:"header-anchor",href:"#self-defined-monad","aria-hidden":"true"},"#"),e(" Self-Defined Monad")],-1),ci=a("h2",{id:"注释"},[a("a",{class:"header-anchor",href:"#注释","aria-hidden":"true"},"#"),e(" 注释")],-1),ri=a("h3",{id:"符号-1"},[a("a",{class:"header-anchor",href:"#符号-1","aria-hidden":"true"},"#"),e(" 符号")],-1),ui=a("ul",null,[a("li",null,[a("code",null,"--"),e(" 单行注释")]),a("li",null,[a("code",null,"{- -}"),e(" 块注释")]),a("li",null,[a("code",null,"{-# #-}"),e(" (文件头部)编译器参数")])],-1),ii=a("h2",{id:"reference"},[a("a",{class:"header-anchor",href:"#reference","aria-hidden":"true"},"#"),e(" Reference")],-1),ki=a("ul",null,[a("li",null,[a("a",{href:"https://github.com/MnO2/learnyouahaskell-zh",target:"_blank",rel:"noopener noreferrer"},"Haskell Book")])],-1);l.render=function(a,e,t,l,bi,mi){return n(),s("div",null,[o,p,c,r,u,i,k,b,m,h,d,v,g,y,f,w,x,S,L,M,F,T,I,A,C,O,N,B,W,E,$,P,q,R,z,D,J,G,_,H,K,Y,V,U,Z,j,Q,X,nn,sn,an,en,tn,ln,on,pn,cn,rn,un,kn,bn,mn,hn,dn,vn,gn,yn,fn,wn,xn,Sn,Ln,Mn,Fn,Tn,In,An,Cn,On,Nn,Bn,Wn,En,$n,Pn,qn,Rn,zn,Dn,Jn,Gn,_n,Hn,Kn,Yn,Vn,Un,Zn,jn,Qn,Xn,ns,ss,as,es,ts,ls,os,ps,cs,rs,us,is,ks,bs,ms,hs,ds,vs,gs,ys,fs,ws,xs,Ss,Ls,Ms,Fs,Ts,Is,As,Cs,Os,Ns,Bs,Ws,Es,$s,Ps,qs,Rs,zs,Ds,Js,Gs,_s,Hs,Ks,Ys,Vs,Us,Zs,js,Qs,Xs,na,sa,aa,ea,ta,la,oa,pa,ca,ra,ua,ia,ka,ba,ma,ha,da,va,ga,ya,fa,wa,xa,Sa,La,Ma,Fa,Ta,Ia,Aa,Ca,Oa,Na,Ba,Wa,Ea,$a,Pa,qa,Ra,za,Da,Ja,Ga,_a,Ha,Ka,Ya,Va,Ua,Za,ja,Qa,Xa,ne,se,ae,ee,te,le,oe,pe,ce,re,ue,ie,ke,be,me,he,de,ve,ge,ye,fe,we,xe,Se,Le,Me,Fe,Te,Ie,Ae,Ce,Oe,Ne,Be,We,Ee,$e,Pe,qe,Re,ze,De,Je,Ge,_e,He,Ke,Ye,Ve,Ue,Ze,je,Qe,Xe,nt,st,at,et,tt,lt,ot,pt,ct,rt,ut,it,kt,bt,mt,ht,dt,vt,gt,yt,ft,wt,xt,St,Lt,Mt,Ft,Tt,It,At,Ct,Ot,Nt,Bt,Wt,Et,$t,Pt,qt,Rt,zt,Dt,Jt,Gt,_t,Ht,Kt,Yt,Vt,Ut,Zt,jt,Qt,Xt,nl,sl,al,el,tl,ll,ol,pl,cl,rl,ul,il,kl,bl,ml,hl,dl,vl,gl,yl,fl,wl,xl,Sl,Ll,Ml,Fl,Tl,Il,Al,Cl,Ol,Nl,Bl,Wl,El,$l,Pl,ql,Rl,zl,Dl,Jl,Gl,_l,Hl,Kl,Yl,Vl,Ul,Zl,jl,Ql,Xl,no,so,ao,eo,to,lo,oo,po,co,ro,uo,io,ko,bo,mo,ho,vo,go,yo,fo,wo,xo,So,Lo,Mo,Fo,To,Io,Ao,Co,Oo,No,Bo,Wo,Eo,$o,Po,qo,Ro,zo,Do,Jo,Go,_o,Ho,Ko,Yo,Vo,Uo,Zo,jo,Qo,Xo,np,sp,ap,ep,tp,lp,op,pp,cp,rp,up,ip,kp,bp,mp,hp,dp,vp,gp,yp,fp,wp,xp,Sp,Lp,Mp,Fp,Tp,Ip,Ap,Cp,Op,Np,Bp,Wp,Ep,$p,Pp,qp,Rp,zp,Dp,Jp,Gp,_p,Hp,Kp,Yp,Vp,Up,Zp,jp,Qp,Xp,nc,sc,ac,ec,tc,lc,oc,pc,cc,rc,uc,ic,kc,bc,mc,hc,dc,vc,gc,yc,fc,wc,xc,Sc,Lc,Mc,Fc,Tc,Ic,Ac,Cc,Oc,Nc,Bc,Wc,Ec,$c,Pc,qc,Rc,zc,Dc,Jc,Gc,_c,Hc,Kc,Yc,Vc,Uc,Zc,jc,Qc,Xc,nr,sr,ar,er,tr,lr,or,pr,cr,rr,ur,ir,kr,br,mr,hr,dr,vr,gr,yr,fr,wr,xr,Sr,Lr,Mr,Fr,Tr,Ir,Ar,Cr,Or,Nr,Br,Wr,Er,$r,Pr,qr,Rr,zr,Dr,Jr,Gr,_r,Hr,Kr,Yr,Vr,Ur,Zr,jr,Qr,Xr,nu,su,au,eu,tu,lu,ou,pu,cu,ru,uu,iu,ku,bu,mu,hu,du,vu,gu,yu,fu,wu,xu,Su,Lu,Mu,Fu,Tu,Iu,Au,Cu,Ou,Nu,Bu,Wu,Eu,$u,Pu,qu,Ru,zu,Du,Ju,Gu,_u,Hu,Ku,Yu,Vu,Uu,Zu,ju,Qu,Xu,ni,si,ai,ei,ti,li,oi,pi,ci,ri,ui,ii,ki])};export default l;export{t as __pageData}; diff --git a/assets/language_java_javaBasicNotes.md.4f729496.js b/assets/language_java_javaBasicNotes.md.4f729496.js new file mode 100644 index 00000000000..0e8579ac4bd --- /dev/null +++ b/assets/language_java_javaBasicNotes.md.4f729496.js @@ -0,0 +1 @@ +import{o as a,c as n,a as s}from"./app.6dd2a1b7.js";const e='{"title":"Java Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"CLI Tools","slug":"cli-tools"},{"level":3,"title":"编译","slug":"编译"},{"level":3,"title":"运行","slug":"运行"},{"level":3,"title":"反汇编","slug":"反汇编"},{"level":3,"title":"打包","slug":"打包"},{"level":2,"title":"Gradle Tool","slug":"gradle-tool"},{"level":3,"title":"Build Gradle","slug":"build-gradle"},{"level":2,"title":"基本结构","slug":"基本结构"},{"level":3,"title":"数据类型","slug":"数据类型"},{"level":3,"title":"常量","slug":"常量"},{"level":3,"title":"运算符","slug":"运算符"},{"level":3,"title":"字符串","slug":"字符串"},{"level":3,"title":"输入输出","slug":"输入输出"},{"level":3,"title":"表达式","slug":"表达式"},{"level":3,"title":"控制流程","slug":"控制流程"},{"level":3,"title":"大数值","slug":"大数值"},{"level":3,"title":"数组","slug":"数组"},{"level":2,"title":"对象与类","slug":"对象与类"},{"level":3,"title":"面向对象的三大特征","slug":"面向对象的三大特征"},{"level":3,"title":"完整的 Java 源文件","slug":"完整的-java-源文件"},{"level":3,"title":"Behavior State and Identity","slug":"behavior-state-and-identity"},{"level":3,"title":"预定义类","slug":"预定义类"},{"level":3,"title":"用户自定义类","slug":"用户自定义类"},{"level":3,"title":"方法 Method","slug":"方法-method"},{"level":3,"title":"对象构造","slug":"对象构造"},{"level":3,"title":"包","slug":"包"},{"level":3,"title":"类路径","slug":"类路径"},{"level":3,"title":"文档注释","slug":"文档注释"},{"level":3,"title":"类设计技巧","slug":"类设计技巧"},{"level":2,"title":"继承","slug":"继承"},{"level":3,"title":"父类与子类(superclass and subclass)","slug":"父类与子类-superclass-and-subclass"},{"level":3,"title":"Object 类","slug":"object-类"},{"level":3,"title":"泛型数组列表","slug":"泛型数组列表"},{"level":3,"title":"对象包装器与自动装箱","slug":"对象包装器与自动装箱"},{"level":3,"title":"可变参数方法","slug":"可变参数方法"},{"level":3,"title":"枚举类","slug":"枚举类"},{"level":3,"title":"访问控制符","slug":"访问控制符"},{"level":3,"title":"非访问控制符","slug":"非访问控制符"},{"level":3,"title":"反射(Reflection)","slug":"反射-reflection"},{"level":2,"title":"接口与内部类","slug":"接口与内部类"},{"level":3,"title":"接口","slug":"接口"},{"level":3,"title":"对象克隆","slug":"对象克隆"},{"level":3,"title":"回调","slug":"回调"},{"level":3,"title":"内部类","slug":"内部类"},{"level":3,"title":"代理类","slug":"代理类"},{"level":2,"title":"图形程序设计","slug":"图形程序设计"},{"level":3,"title":"Swing 组件继承层次","slug":"swing-组件继承层次"},{"level":3,"title":"Swing 组件功能层次","slug":"swing-组件功能层次"},{"level":3,"title":"JFrame","slug":"jframe"},{"level":3,"title":"框架基础","slug":"框架基础"},{"level":3,"title":"自定义绘制组件","slug":"自定义绘制组件"},{"level":3,"title":"图形颜色","slug":"图形颜色"},{"level":2,"title":"事件处理","slug":"事件处理"},{"level":3,"title":"定义与基础","slug":"定义与基础"},{"level":3,"title":"动作","slug":"动作"},{"level":3,"title":"鼠标事件","slug":"鼠标事件"},{"level":3,"title":"AWT 事件继承层次","slug":"awt-事件继承层次"},{"level":2,"title":"Swing 组件","slug":"swing-组件"},{"level":3,"title":"Awt 和 Swing 设计中的设计模式","slug":"awt-和-swing-设计中的设计模式"},{"level":3,"title":"简单布局管理器","slug":"简单布局管理器"},{"level":3,"title":"复杂布局管理器","slug":"复杂布局管理器"},{"level":3,"title":"文本组件","slug":"文本组件"},{"level":3,"title":"选择组件","slug":"选择组件"},{"level":3,"title":"菜单组件","slug":"菜单组件"},{"level":3,"title":"对话框(顶级框架)","slug":"对话框-顶级框架"},{"level":2,"title":"异常、断言、日志、调试","slug":"异常、断言、日志、调试"},{"level":3,"title":"处理错误","slug":"处理错误"},{"level":3,"title":"捕获异常","slug":"捕获异常"},{"level":3,"title":"记录日志","slug":"记录日志"},{"level":3,"title":"调试技巧","slug":"调试技巧"},{"level":2,"title":"泛型","slug":"泛型"},{"level":3,"title":"泛型类","slug":"泛型类"},{"level":3,"title":"泛型方法","slug":"泛型方法"},{"level":3,"title":"泛型变量 T","slug":"泛型变量-t"},{"level":3,"title":"泛型代码","slug":"泛型代码"},{"level":3,"title":"使用限制","slug":"使用限制"},{"level":3,"title":"通配符类型","slug":"通配符类型"},{"level":3,"title":"反射与泛型","slug":"反射与泛型"},{"level":2,"title":"集合","slug":"集合"},{"level":3,"title":"集合接口","slug":"集合接口"},{"level":3,"title":"具体集合","slug":"具体集合"},{"level":3,"title":"集合框架","slug":"集合框架"},{"level":3,"title":"集合算法","slug":"集合算法"},{"level":3,"title":"早期集合","slug":"早期集合"},{"level":2,"title":"线程","slug":"线程"},{"level":3,"title":"线程基础","slug":"线程基础"},{"level":3,"title":"线程状态","slug":"线程状态"},{"level":3,"title":"线程属性","slug":"线程属性"},{"level":3,"title":"中断线程","slug":"中断线程"},{"level":3,"title":"线程同步","slug":"线程同步"},{"level":3,"title":"线程安全","slug":"线程安全"},{"level":3,"title":"异步计算","slug":"异步计算"},{"level":3,"title":"执行器","slug":"执行器"},{"level":3,"title":"同步器","slug":"同步器"},{"level":3,"title":"线程与 Swing:事件分配线程","slug":"线程与-swing-事件分配线程"},{"level":2,"title":"Self-Defined Class","slug":"self-defined-class"},{"level":2,"title":"常用工具类","slug":"常用工具类"},{"level":2,"title":"输入与输出","slug":"输入与输出"},{"level":3,"title":"输入","slug":"输入"},{"level":3,"title":"输出","slug":"输出"},{"level":2,"title":"正则表达式","slug":"正则表达式"},{"level":3,"title":"基本写法","slug":"基本写法"},{"level":3,"title":"Group RegExp","slug":"group-regexp"},{"level":2,"title":"Garbage Collection","slug":"garbage-collection"},{"level":3,"title":"Garbage Collection Optimization","slug":"garbage-collection-optimization"},{"level":3,"title":"GC Tools","slug":"gc-tools"},{"level":2,"title":"Java Naming Conventions","slug":"java-naming-conventions"},{"level":3,"title":"使用前注意事项","slug":"使用前注意事项"},{"level":3,"title":"包名的书写规范 (Package)","slug":"包名的书写规范-(package)"},{"level":3,"title":"类名的书写规范 (Class)","slug":"类名的书写规范-class"},{"level":3,"title":"变量的命名","slug":"变量的命名"},{"level":3,"title":"方法的命名","slug":"方法的命名"}],"relativePath":"language/java/javaBasicNotes.md","lastUpdated":1627196319000}',l={},t=s('

Java Basic Notes

CLI Tools

编译

javac name.java

运行

java name(无后缀类名)

反汇编

javap –c name(无后缀类名)

打包

jar {c t x u f} [v m e 0 M i][-c 目录] 文件名

解释:

  • c:创建 jar 包
  • t:显示 jar 包内容
  • x:解压 jar 包
  • u:添加文件到 jar 包
  • f:命名 jar 包
  • v:显示详细执行过程报告
  • m:指定 manifest.mf 文件(对 jar 包做相关设置)
  • 0:打包 jar 包是不压缩
  • M:不产生 manifest.mf 文件,覆盖 m 参数的设置
  • i:创建索引文件
  • C:进入某目录后再执行 jar 命令
  • 生成 API 文档:java doc –d [ ] 类名/包名

可选参数:

  • -author/-version:文档中显示作者和版本信息(默认不显示)
  • -link superlink 标准类名:为标准类添加超链接
  • -linksourse:方法/类名转化为超链接,指向生成的 html 格式的源文件

Gradle Tool

sudo apt install gradle\n\ngradle help\ngradle tasks\n\ngradle assemble\ngradle build\ngradle clean\ngradle test\ngradle jar\ngradle javadoc\n
1
2
3
4
5
6
7
8
9
10
11

Build Gradle

apply plugin: 'java'\n\njar {\n    manifest {\n        attributes 'Main-Class': 'com.sabertazimi.tao.Tao'\n    }\n}\n
1
2
3
4
5
6
7

基本结构

数据类型

整型

类型
  • byte 型:1 字节 -128~127
  • short 型:2 字节 -2^15~2^15 -1
  • int 型:4 字节 -2^31~2^31 -1(20 亿)
  • long 型:8 字节 -2^63~2^63 -1
前/后缀
  • 二进制前缀 0b/0B
  • 八进制前缀 0
  • 十六进制前缀 0x/0X
  • 长整型后缀 l/L

浮点型

float 型

后缀 F:4 字节 -3403E38~3.403E38(有效位数 6~7 位)

float f = 1.2;    //ERROR\nfloat f = 1.2F;   //OK\n
1
2
double 型

后缀 D(默认型):8 字节 -1798E308~1.798E308(有效位数 15 位)

科学计数法:指数均为十进制,符号为 e(尾数为十进制)/p(尾数为十六进制)

Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,Double.isNaN(检数)

千分位下划线分隔法 e.g 123_456.789_000

字符型

  • 1 个 char 型占 2 个字节(Unicode 编码)

  • ’ ’(定界符)内可用八/十六进制转义字符表示字符

  • 八:\\xxx

  • 十六:\\uxxxx(\\u 转义序列可出现在任何地方) e.g ‘\\u0061’

改进的 Unicode 字符集:

  • 代码点(前缀 U+)分为 17 个代码级别(code plane)
  • 第一个级别为经典 Unicode 代码,
  • 其余附加级别 U+10000 至 U+10FFFF。

布尔型

只允许赋值为 true、false(不可取 0 或非 0 值),不可与整型相互转换

引用类型

数组(Array)

字符串 String:不可变性(需要修改变量名所引用值,必须重新创建一个 String 对象例)

旧的对象实例若被判定为垃圾便会被系统自动回收

类(Class)
接口(Interface)

常量

定义(随使用随定义)

运算符

短路与、短路或

  • &&:第一个操作数为假不再向后运算
  • ||:第一个操作数为真不再向后运算

移位符

有符号右移>>:最高位补符号位 左操作数 int/long,右操作数 mod32/mod64

e.g int a >> 33 即 int a >> 1

无符号右移>>>:最高位补 0

字符串连接运算符

+: 左右操作数 String 字符串/其他类型(自动被转化为 String)

判断运算符

==:

  • 基本类型值相等,引用类型引用相等
  • 基本类型:转换后比较;boolean 型不与 int 型比较
  • 引用类型:指向两个不同对象实例的变量不相等;若判断两个对象实例内容是否相等,必须调用 equals()方法

强制类型转换运算符

()

  • 只能在继承层次内进行强制类型转换(同一家族)
  • 将父类转换为子类前,应使用 instanceof 运算符进行检查

字符串

子串

stringTest.substring(a, b)方法返回长度为 b-a 的子串,a(包括)、b(不包括)为起止位置

相等

  • 内容相等:stringOne.equals(stringTwo)方法返回值 true/false
  • 不区分大小写 equalsIgnoreCase

代码点<代码单元(单字符)

常用方法

构建字符串

StringBuilder 类:单线程,功能同 StringBuffer 类(多线程)

输入输出

Scanner 类

监视器,功能最强大的输入类,new Scanner(System.in)

格式化输出方法

System.out.printf(): 静态创建格式化字符串

文件的输入与输出

输入(至内存)

用 File 对象构造一个 Scanner 对象:new Scanner(Paths.get(“FileName”))(反斜杠需双写)

输出(至文件)

用文件名构造一个 PrintWriter 对象: new PrintWriter(“FileName”), 之后调用System.PrintWriter.print方法.

表达式

  • 整数提升:算术运算前 byte,short,char 提升为 int
  • 没有”,”运算符和表达式语句

控制流程

合法语句:赋值语句和方法调用语句

块作用域

  • 不允许内层块与外层块变量同名(无 C 语言中的可见性)
  • 块内声明变量不允许在块外使用:if 块、for 块、while 块、普通 block

switch 语句

case 标签变量类型:字符型(包装类)、三整型(包装类)、枚举常量、字符串类

loop 语句

循环五要素
  • 初始化部分(init_statement);
  • 循环条件部分(test_explanation);
  • 循环体部分(body_statement);
  • 迭代部分(alter_statement):自加/自减运算符改变条件变量
  • 结束后处理
for each 循环
//只读遍历;变量age只作用于for循环体\nfor (int age : arrayName) {}\n
1
2

Break and Continue Statement

Break Statement
label:\n{\nbreak label;\n} //可跳出任意语句块:跳至标签块末尾\n
1
2
3
4
Continue Statement
  label:\n  {\ncontinue label;\n} //跳至循环块首部\n
1
2
3
4

大数值

  • java.math.BigInteger
  • java.math.BigDecimal

数组

定义+引用

  • 类型(包括类/对象) + [] + 标识符

  • 类型(包括类/对象) + 标识符 + []

  • int a[N] 非法 数组是引用类型

  • 不可对数组名进行自增操作(尽管[]与*具有类似作用)

初始化

  • 默认初始化数值类型为 0/false,引用类型为 null
  • 使用 new 分配内存单元

两种不同初始化方式:

  • 声明时:MyDate[] dates = {new MyDate(),……};
  • 赋值时:MyDate[] dates = new MyDate[] (匿名数组){new MyDate(),…..};

方法

  • 属性:arrayName.length
  • 打印:arrays.toString 方法
  • 复制:arrayName.Copy( source, 0, destination, 0, source.length);
Arrays.copyOf(ArrayName, Array.length); //可用来增长数组长度\n
1
  • 比较:Arrays.equals(type[] a,type[] b);

命令行参数

字符串数组 String[] args, args 数组不包括程序名.

排序

Arrays.sort(ArrayName)快速排序方法

多维数组

  • 多维数组的声明和初始化应按从高维到低维的顺序进行
    • 声明&初始化 int[][] t = new int[3][];
    • 子数组交换 temp = a[i];a[i]=a[i+1];a[i+1]=temp;
  • 子数组长度可不一致:可先构造一维长度,再使用循环构造子数组各自长度

对象与类

面向对象的三大特征

封装性

  • 模块化+信息隐蔽(隐藏细节(private),只提供受保护的访问接口(public))
  • 只可通过方法改变对象实例的状态(State)(Instance Field)

继承性

父类和子类共享数据和方法(提高代码重用率和可维护性)

  • 可继承成员:公有域、公有方法
  • 不可继承成员:私有域、静态域、实例域初始化块、私有方法、静态方法、构造器方法

多态性

不同的对象收到同一个消息(method 调用)可产生不同效果(由对象内封装的细节决定)

  • 编译时多态:重载(Overload)

  • 运行时多态:覆盖(Override)

  • 根据对象实例的类型进行动态绑定(Dynamic Binding)

  • 虚方法调用(Virtual Method Invoking) :

    • 在调用方法时,程序会正确地调用子类对象的方法
    • 无法进行虚方法调用情况
  • static 的方法:以声明类型(标识符前的类型)为准,无关实例类型

  • final/private 方法:子类无法覆盖/继承父类同名方法,不存在虚化问题

完整的 Java 源文件

package packageName;              //指定文件中的类所在的包,0个或1个\nimport packageName.[className|*];   //指定引入的类/API,0个或多个\npublic classDefinition                 //属性为public的类定义,0个或1个\ninterfaceDefinition and classDefinition  //接口或类定义,0个或多个\n
1
2
3
4

Behavior State and Identity

预定义类

Constructor

Georgia Calendar

用户自定义类

完整的类定义

[public] [abstract|final] class className[extends superclassName]\n[implements interfaceNameList] {\n\n//成员变量声明,可为多个:\n//实例域一般添加private修饰符,保持封装性\n[public | protected | private] [static] [final] [transient] [volatile]type variableName;\n\n//方法(包括构造方法)定义及实现,可为多个\n[public | protected | private] [static] [final | abstract] [native] [synchronized]\nreturnType methodName( [paramList] ) [throws exceptionList]{\n    statements;\n  }\n}\n\nclass Class_Name{\n   int/String characteristic;   //状态(State):变量(实例域 instance field)\n   constructor(){};             //标识(Identity):构造器(constructor)\n   void function(){};           //行为(Behavior):函数(方法method)\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

访问器方法(Accessor Method)

只访问实例域不进行修改的方法

returnType get() {\n    //保持封装性\n    //返回引用类型对象时,必须返回克隆体对象引用而不是实例域对象引用\n  return instanceFieldVariable.clone();\n}\n
1
2
3
4
5

更改器方法(Mutator Method)

修改实例域的方法

e.g add、set

方法 Method

方法参数

Java 所有普通方法的参数传递方式:值传递

  • 基本类型:传递值的拷贝

方法中无法更改原变量的值

  • 引用类型:传递引用的拷贝

方法中无法更改原变量的引用(指针指向),只可更改引用对象的状态(State)

方法标签(Signature)

  • 方法名称
  • 方法参数

方法返回值类型不是方法标签(不存在同名同参而不同返回值的方法)

对象构造

构造器方法完整定义:

ClassName (ClassPropertiesVariableTable) {\n    //进行输入参数合法化检查\n    Property n = Variable;\n    Statement Block;\n}\n
1
2
3
4
5

构造器方法的定义

  • 未定义任何构造器方法 系统会自动产生一个构造器方法,称为无参空默认构造器方法(default constructor) 并将所有实例域初始化为默认值
  • 注意事项
    • 不应在构造器中定义实例域同名变量
    • 在构造器中尽量只调用 final/private 方法(不会(继承后)被子类方法覆盖)
    • this()可调用此类的其他构造方法,this()必须位于构造器方法首行

构造器方法的执行

  • 构造器方法前语句/语句块将先于构造器执行
  • 编译器会在编译时在每个构造器方法内首行自动添加 super();(若父类没有无参构造器方法编译出错)/人工显式调用父类构造器方法,直至实现 Object 类(超类)的构造器方法,再执行构造器方法语句

导入 import

  • 包的导入
import java.包路径.*   //导入指定包的所有类\n
1
  • 静态导入
import static java.包路径.类名.*  //导入指令类的所有静态域和静态方法\n
1

类路径

package 语句

文档注释

Java 除了可以采用我们常见的注释方式(//、/* */(二型注释不可嵌套))之外,Java 语言规范还定义了一种特殊的 Javadoc 注释:

/**\n*@author    对类的说明              标明开发该类模块的作者\n*@version   对类的说明              标明该类模块的版本\n*@see      对类、属性、方法的说明  参考转向,也就是相关主题\n*@param    对方法的说明            对方法中某参数的说明\n*@return    对方法的说明             对方法返回值的说明\n*@exception 对方法的说明            对方法可能抛出的异常进行说明\n*@Override   对方法覆盖的说明\n*@FunctionalInterface 函数式接口:包含不超过一个abstract方法(可以有其他非abstract方法)\n*/\n
1
2
3
4
5
6
7
8
9
10

类注释:位于类定义前

/**\n*整体性描述\n*\n* @author 作者\n* @version 1.0, 05/22/07\n* @since version1.0\n*/\n
1
2
3
4
5
6
7

方法注释:位于方法定义前

/**\n*整体性描述\n*\n*@param             对方法的说明            对方法中某参数的说明\n*@return            对方法的说明             对方法返回值的说明\n*@exception/throws 对方法的说明            对方法可能抛出的异常进行说明\n*/\n
1
2
3
4
5
6
7

域注释:位于实例域定义前

/**\n * 备注信息\n */\n
1
2
3

通用注释

可在文档中看到引用(文本、类/方法/变量、超链接)

  • @see/@link package.class#feature label
  • @see/@link <a href = “……”> label </a>
  • @see/@link “text”

区别: link 注释可在任意位置,see 注释需在类/方法/变量定义前

包注释

新建 package-info.java 源文件:

package 包路径\n/**\n * 包注释\n */\n
1
2
3
4

类设计技巧

数据私有化

保持封装性

数据初始化

不依赖实例域默认值,且应进行数据合法化检查

实例域封装化

过多相关的基本类型实例域可封装成一个轻型类

构造器多样化

最低标准为显式无参构造器+一个带参数(包括合法化检查)构造器

职责明晰化

一个类的方法应与类本身紧密相关,且不过少也不过多

继承

父类与子类(superclass and subclass)

属性的继承

  • 可继承:公有域
  • 不可继承:私有域、静态域、实例域初始化块
  • 若要访问(修改)父类私有域,必须使用调用公有的 super.get()方法得到父类私有域
  • 初始化父类私有域:super(ParamList); 调用父类构造方法进行初始化

方法的继承

  • 可继承:公有方法
  • 不可继承:私有方法、静态方法、构造器方法

方法的覆盖

标签相同(不可降低父类方法可见性)

方法的重载

标签不同,属于新加方法

super 关键字

不是隐式参数,是提示编译器执行的特殊关键字

父类与子类的转换(Is-A 关系)

Subclass must be superclass,superclass may not be subclass.

  • 可将子类引用赋值给父类变量(Upcasting)或替换父类作参数
Person p = new Student();  //p仍是父类变量\n
1

注:若父类变量和子类变量引用同一个子类对象实例时,父类某些操作可能引起内存混乱 例如:先调用父类构造器方法构造子类,再调用子类扩展方法访问子类扩展私有域(根本未分配内存空间)

  • 不可将父类引用赋值给子类变量
Student s = new Person();  //ERROR:e.g. s.getStuID();\n
1

Object 类

Equals Method

  • 子类中覆盖(@override)父类 equals 方法:super.equals(superClassName other);
  • 显式参数名:Object otherObject
  • if (this == otherObject) return true;
  • if (otherObject == null) return false;
  • if (getClass != otherObject.getClass()) return false;
  • ClassName other = (ClassName) otherObject;
  • return field == other.field

Hash Code Method

  • String 类 hashCode:ΣStringChar[i]^(n-i-1), (i:0~(n-1))
  • Object 对象 hashCode:内部 ID(存储地址)
  • 若重写 equals 方法,也应重写 hashCode 方法,使对象实例 hashCode 分布散列化
    • return 各域 hashCode 值加权和:31x+y rule
      • 引用类型、包装类:hashCode()
      • 数组:Arrays.deepHashCode()
    • return Objects.hash(field1,..,fieldN); //自动组合各域 hashCode
    • hash 值:return (key.hashCode() & 0x7fffffff) % M;

To String Method

  • 设计 return getClass().getName() + ”[“ + field + ”]”;
  • Object 类 toString 方法:用于输出 ClassName 和 hashCode
  • 数组直接继承 Object 类 toString 方法,输出数组需调用 Arrays.(deep)toString();
  • 自动调用:+ 字符串连接符、println(obj)

泛型数组列表

ArrayList<ClassName> VariableName = new ArrayList<>();

  • toArray(ArrayName)转化为同类型普通数组存至 ArrayName:可以创建一个 ArrayList,再转化为数组,方便使用[]下标操作
  • 插入、删除操作时间复杂度高(数据元素左/右移)

对象包装器与自动装箱

Void,Boolean,Byte,Character,Short,Integer, Long, Float, Double 类:包装器(Wrapper)类

父类:Number

final 类(强不可变类):无子类,不可改变数

编译器特性

  • 自动装箱(auto-boxing)
Integer I = 10;\nmyArrayList.add(10);\n
1
2
  • 自动拆箱(auto-unboxing)
int i= I;\n
1
  • 自动装箱+拆箱
Integer I = 0;I++;\n\n// 实际译为:\nInteger I = Integer.valueOf(10);\nmyArrayList.add(Integer.valueOf(10));\nInt I =  I.intValue();\n
1
2
3
4
5
6

API 说明

可变参数方法

枚举类

定义

扩展

访问控制符

public>protected(包作用域+)>默认(包作用域)>private(类作用域)

最大限度:不同包的非子类(所有类)、同一个包/不同包的子类、同一个包、同一个类

非访问控制符

abstract

abstract 方法

语法形式:abstract + MethodName(ParamList);

abstract 类
  • 定义
    • 若一个类包含 abstract 方法,则必须是 abstract 类
    • 一个 abstract 类可以不包含 abstract 方法
    • 一个 abstract 类可以包含具体数据和具体方法
  • 构造器方法
    • 需要实现构造器方法
    • 构造器方法可以被非 abstract 子类调用
    • 不能使用 new 构造 abstract 类对象实例

final

final 局部变量(方法内的变量)

只读常量,必须进行一次初始化

final 实例域
  • 必须进行一次(类定义/构造器)初始化
  • final 引用类型实例域的引用不可更改,但所引用对象的状态(State)可被更改
final 方法

可继承,不可重写

final 类
  • 不可继承
  • 实例域可以不是 final 实例域,方法自动成为 final 方法

static

static 实例域(类域)
  • 属于类整体,保存在类的内存区域的公共存储单元(不可继承)
  • 可通过类名/对象实例名访问该属性
static 方法
  • 类方法,属于类整体(不可继承)
  • 没有隐式参数(this/super)
  • 只能处理该类中的 static 实例域/调用该类中的 static 方法
  • 既可通过类名调用该方法(推荐),又可通过对象实例名调用 static 方法
  • Tips:工厂模式:利用 static 方法可模拟构造器方法:可自定义构造器名和返回对象类型

static final 实例域

  • 默认初始化(数值型为 0,布尔型为 false,引用型为 null)
  • 常结合 public 将变量设置为全局静态常量 e.g. e、PI

反射(Reflection)

反射机制

  • 运行中分析 类的能力
  • 运行中查看对象
  • 实现通用的数组操作代码
  • 利用 Method 对象

Class 类

分析类的能力

分析对象

泛型数组实现

调用任意方法(函数指针)

接口与内部类

接口

基本定义

interface 类型是引用类型

常量 + 方法

 [修饰符(public)]interface 接口名 [extends 父接口名列表(多个接口)] {\n//常量声明,可为多个\n[public] [static] [final] type constantName= Value;\n//方法声明,可为多个\n[public] [abstract] returnType methodName( [paramList] );\n}\n
1
2
3
4
5
6

接口方法

自动默认:public abstract 方法(声明时无需关键字)

实现接口

在类中必须实现(override)接口中的所有方法

接口作为方法参数

  • 设计此类方法时:
    • 可调用接口中公有静态常量/公有方法(interfaceVariableName.invokeMethod)
    • 可将接口参数传递给其他接口作参数的方法,调用其他方法
  • 实际调用方法时:将实现了该接口的类作为参数传递给方法,可提高方法的灵活性

例子:Comparable 接口和 sort 静态方法

接口变量

  • 不可创建 interface 实例(即不可用 new 关键字)
  • 可声明一个 interface 变量,并使其引用实现了该接口的对象实例
//TimePrinter:实现了ActionListener接口的类\nActionListener myListener = new TimePrinter();\n
1
2

对象克隆

Cloneable Interface

标签接口(tagging interface)之一:无方法

Object Clone Method

浅拷贝: 无法克隆目标对象实例域中的对象成员

Custom Clone Method

鉴于默认 clone 方法的局限性,需自定义用户需要的 clone 方法

  • 实现接口:implements Cloneable
  • 改变可见性&抛出异常: public <returnType> clone() throws CloneNotSupportedException {};
  • 自定义克隆体:克隆子对象

回调

回调设计模式

某个特定事件发生时采取某个特定动作

Action Listener Interface

内部类

内部类调用格式

  • 声明与构造(实例化)

OuterClass.InnerClass variableName = OuterClass/this.new InnerClass();

  • 定义:应使用 private 关键字

  • 访问实例域/方法

内部类:this.fieldName/methodName 外部类:OuterClass.this.fieldName/methodName

局部内部类

  • 定义在方法中的内部类
  • 修饰符:无/abstract/final
  • 可访问外围类成员/final 局部变量

匿名内部类

在实际构造父类/实现接口时定义在构造器方法中的内部类

  • 外围为父类:内部类需要扩展该父类
new SuperType (construction parameters)\n{\n    inner class methods and data{};  //直接定义实例域和方法,构造器参数传递给父类构造器\n}\n
1
2
3
4
  • 外围为接口:内部类需要实现该接口
new InterfaceType () //一对括号\n{\n    Inner class methods and data{};  //需实现接口中的所有抽象方法\n}\n
1
2
3
4
  • 例子:积分、线程、回调函数(HookFunction)、事件处理(ActionListener)

静态内部类

  • 只可访问外围类的静态域/静态方法(可视作不可访问外围类)
  • 实例化:new OuterClass.InnerClass()

代理类

图形程序设计

Swing 组件继承层次

Swing 组件功能层次

JFrame

框架基础

位置和大小

  • 方法概览
//继承于:Component类\n//功能:设置JFrame的位置和大小\n//坐标:相对于电脑屏幕\nsetLocation(x, y);\nsetBounds(x, y, width, height);\nsetLocationByPlatform(true);\n
1
2
3
4
5
6
  • 动态设置
//得到屏幕大小\nDimension screenSize = Toolkit.getDefaultToolKit().getScreenSize();\nint screenWidth = screensSize.width;\nint screenHeight = screenSize.height;\n//根据屏幕大小设置框架大小\nsetSize(screenWidth / 2, screenHeight / 2);\nsetLocationByPlatform(true);\n
1
2
3
4
5
6
7

Property Getter and Setter

  • 标题
//继承于:Frame类\n//功能:标题属性\npublic String getTitle();\npublic void setTitle (String title);\n
1
2
3
4
  • 定位
//继承于:Window类\n//功能:定位属性\npublic boolean isLocationByPlatform();\npublic void setLocationByPlatform (boolean b);\n
1
2
3
4
  • 标题图标
Image img = new ImageIcon(“icon.gif”).getImage();\nsetIconImage(img);\n
1
2
  • 其他属性

自定义绘制组件

所有绘制都使用 Graphics 类作参数(包含绘制图案、图像、文本等的方法)

JComponent Extends

class MyComponent extends JComponent\n{   //@override\n    public void paintComponent (Graphics g)\n    {\n        g.drawString(text, x, y);\n    }\n    //@override\n    public Dimension getPreferredSize()\n    {\n        return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12

JPanel Extends

class MyJPanel extends JPanel\n{\n    public void paintComponent(Graphics g)\n    {\n        super.paintComponent(g);\n        code for drawing;\n    }\n}\n
1
2
3
4
5
6
7
8

Shape Interface

图形类继承关系图

图形颜色

  • Color 类预定义颜色(Color.XX)
  • SystemColor 类预定义颜色(SystemColor.XX)

事件处理

定义与基础

事件源与事件监听器

实现 Action Listener 接口的事件监听器

适用情况
  • 按钮点击事件(click button)
  • 鼠标双击选择列表框某一选项
  • 选择某一菜单项
  • 文本域键入回车键
  • 组件达到指定时间间隔
事件监听器对象执行其他对象事件源产生事件影响

此时,可使用匿名内部类或 Lambda 表达式简化代码

.addActionListener(new ActionListener()\n{\npublic void actionPerformed(ActionEvent event)\n{\n//code for procedure\n}\n});\n
1
2
3
4
5
6
7

.addActionListener( (e) -> {\n//code for procedure\n});\n
1
2
3

Event Handler Class

Adapter Class

此类实现相应接口所有方法,但每个方法方法体皆空;简化 Listener 实现,只需重写所需方法

.addWindowLister(new WindowAdapter()\n{\n    //重写所需方法\n});\n
1
2
3
4

动作

Action and Abstract Action

Action 接口可连接多个事件源,AbstractAction 类是实现该接口的类(可继承此类编写动作)

基本方法

  • actionPerformed:继承于 ActionListener 接口
  • enabled:启用/禁用动作
  • value:动作对象中的任意名/值 e.g.Action.NAME,“按键名称”
  • key 为动作属性的名称,value 为对应具体属性(如 String、ImageIcon)
  • propertyChangeListener:将动作对象属性变化传给监听器,再传给其他组件对象

动作与点击按钮

  • 将自定义动作与按钮关联
  • JButton 构造器:JButton(Action act);

动作与按键映射

  • 每个组件有三个输入映射和一个动作映射
  • 建立输入映射(三种方式得到(次)顶层组件的输入映射)
InputMap imap = panel.getInputMap(JComponent.InputMapFlag);\n
1
  • 关联输入映射和动作映射:put 方法含有两个参数
imap.put(  KeyStroke.getKeyStroke(“ctrl Y)  ,  Object actionMapKeyName );\n
1
  • 建立动作映射
ActionMap actionMap = panel.getActionMap();\n
1
  • 关联动作映射和动作:put 方法含有两个参数
actionMap.put(Object actionMapKeyName  ,  Action act);\n
1

注:将 Object 设为 String ”none” 时 表示 空动作(按键无效)

鼠标事件

AWT 事件继承层次

事件类

  • 语义事件类(semantic):

ActionEvent、AdjustmentEvent、ItemEvent

  • 低级事件类(low-level)

FocusEvent、MouseEvent、MouseWheelEvent、KeyEvent、WindowEvent

事件处理 监听器接口

事件处理 适配器类

  • FocusAdapter
  • MouseAdapter
  • MouseMotionAdapter
  • KeyAdapter
  • WindowAdapter

Swing 组件

Awt 和 Swing 设计中的设计模式

Composite Pattern

将对象组合成树形结构以表示”部分-整体“的层次结构,使得用户对单个对象和组合对象的使用具有一致性

容器与组件(具有共同接口) - 容器与组件具有层次结构,用户可对容器与组件实施同样的操作(点击、删除、输入等)

Decorator Pattern

保持接口,增强性能

以对客户透明的方式动态地给一个对象附加上更多的责任(对象前后本体未变化)

  • 抽象组件(Component)角色: 给出一个抽象接口,以规范准备接收附加责任的对象(透明不变性)
  • 具体组件(ConcreteComponent)角色: 定义一个将要接收附加责任的类(透明不变性)
  • 装饰(Decorator)角色:
    • 持有一个组件对象的实例
    • 定义一个与抽象构件组件接口一致的接口
    • 添加额外功能

具体装饰(ConcreteDecorator)角色: 负责给组件对象“贴上”附加的责任

带滚动条的面板 Component:面板,Decorator:滚动条

Strategy Pattern

动态改变行为

定义了不同的算法族,并且之间可以互相替换

  • 环境对象:实现了抽象策略接口或者引用了具体策略类
  • 抽象策略接口/类:接口/抽象类,环境对象中变化部分
  • 具体策略类:实现了抽象策略接口,可有多个不同的具体策略类(动态变化)

设计原则: 把一个类中经常改变或者将来可能改变的部分提取出来,作为一个接口

布局管理器 环境对象:面板,抽象策略:抽象布局管理接口,具体策略:具体布局管理类

Model View Controller Pattern

  • 组件三要素:外观、内容(状态)、行为
  • 模型类:存储内容(状态) state getter/setter
  • 视图类:显示内容(状态)对应外观 一个模型可以有多个视图(模型的不同部分/形式)
  • 控制器类:处理用户输入事件(点击鼠标、敲击键盘),改变模型类/视图类状态,调用模型类/视图类中方法

一个模型类可以适用多个组件(如 DefaultButtonModel 是多种 Button 包装器类的模型对象):

几乎所有组件都有一个带后缀 UI 的视图对象;不是所有组件都有一个专门的控制器对象

简单布局管理器

Flow Layout Class

逐行填充组件,可设置对齐方式和组件间距

Border Layout Class

改变面板大小时,只有中央组件大小变化

技巧:可在五个区块内添加子面板

frame.add(panel, BorderLayout.CENTER/NORTH/SOUTH/WEST/EAST)\n
1

Grid Layout Class

组件按行列排列,且大小一致(只能逐行逐列添加组件)

复杂布局管理器

Grid Bag Layout Class

没有行列大小和个数限制的网格布局:行列大小可不一致,可合并相邻行/列(合并单元格)

gridBagPanel.add(component, GridBagConstraints);给每一个组件都构造一个 GridBagConstraints 类

基本步骤
  • 画框图
  • 设置 gridXX 四值
  • 设置 fill/anchor 值、
  • 设置增量 weightx/weight
    • 全部 100:行列可随容器大小变化
    • 设置某一行/列所有 weightx/weighty 为 0:此行/列保持默认大小

e. gridBagPanel.add(component, GridBagConstraints);

Group Layout Class

通过(嵌套)组的水平与垂直计算(间距与组件),实现灵活的布局:

  • GroupLayout 类
  • Group 类
  • ParallelGroup 类
  • SequentialGroup 类

Null Layout

Custom Layout

实现 LayoutManager 接口,自定义 setSizes(Container parent)

在 layoutContainer 方法中调用每个组件的 setBounds 方法,摆放组件

文本组件

JTextComponent 抽象类

文本域

  • JTextField 组件

标签

  • JLabel 组件: 标签无法响应用户输入事件,只提供标识功能

密码域

  • JPassWordField 组件

文本区

  • JTextArea 组件

滚动窗格

  • JScrollPane: 将文本区组件/其他组件插入到滚动窗格中, 可实现文本区/其他组件的滚动.

选择组件

复选框

  • JCheckBox 组件, 勾取选项(可自定义选项标签):勾选/不勾选状态
.addActionListener( if ( .isSelected() ) {……});\n
1

多个复选框可共用一个监听器,通过 isSelected 方法检测不同复选框的状态

单选按钮

  • ButtonGroup 对象负责调度任务:保证只有一个 JRadioButton 对象被按下
  • JRadioButton 对象:add 至 JPanel 对象,add 至 ButtonGroup 对象,addActionListener 注册监听器

边框

在面板/子面板(TextPanel、ButtonPanel 功能区等) 调用 setBorder 方法填充边框

组合框

JComboBox 组件,泛型类, 下拉选框(N 个选项 Item),.addActionListener(getItemAt(getSelectedIndex()))

滑动条

JSlider 组件, 可连续选择数值:

.addChangeListener(new ChangeListener()\n{\n    public void stateChanged(ChangeEvent event)\n    {\n        JSlider source = (JSlider) event.getSource();\n        int Value = source.getValue();\n        code for operating;   //with Value\n    }\n});\n
1
2
3
4
5
6
7
8
9

菜单组件

创建菜单

JMenuBar –> JMenu -> JMenuItem

利用 Action 创建 MenuItem:(调用 insert 方法)

Action exitAction = new AbstractAction (Exit) //MenuItems text goes here\n{\n    public void actionPerformed (ActionEvent event)\n    {\n        code for action;\n    }\n};\n
1
2
3
4
5
6
7

图标菜单项

  • JMenuItem 类扩展 AbstractButton 类
  • 同样地,JMenuItem 类构造器/Action.putValue(Action.SMALL_ICON,……);

单选按钮/复选框菜单项

将 MenuBar 视作 JPanel 即可

弹出菜单

JPopupMenu 组件, 不依附于 MenuBar 的浮动菜单(Window 系统下右键弹出菜单)

快捷键

  • 同样地,两个方式:MenuItem 类构造器/Action.putValue(Action.MNEMONIC_KEY,……);
  • 加速器:不打开菜单项即可起作用的快捷键(如 Ctrl+……),关联 JMenuItem

启用/禁用菜单项

工具栏

  • 同样地,JToolBar 构造器/Action.putValue(Action.SHORT_DESCRIPTION);

对话框(顶级框架)

  • 模式对话框:弹出时不允许用户作其他交互
  • 无模式对话框:弹出时允许用户作其他交互(如工具栏)

选项对话框

  • JOptionPane 组件

自定义对话框

  • 在构造器中调用超类 JDialog 构造器方法
  • 添加对话框的用户界面组件
  • 添加事件处理器
  • 设置对话框大小

文件对话框

  • JFileChooser 组件插入对话框
  • 文件选择器
  • 文件过滤器:可添加文件视图类

颜色对话框

  • JColorChooser 组件插入对话框

异常、断言、日志、调试

处理错误

异常分类

  • 已检查异常:需要抛出声明(非 RuntimeException)
  • 未检查异常:不可控(Error)/可避免(RuntimeException:下标越界等)
  • 子类覆盖父类方法(该方法)抛出必检异常数必须少于父类方法

捕获异常

原则:捕获知道如何处理的异常,传递不知如何处理的异常

捕获多个异常

  • 普通形式
try\n{\n    语句组(可能抛出异常的语句:抛出异常后,try语句块直接终止)\n}\ncatch (匹配Exception ex)\n{\n    子子异常类名 异常处理语句组;\n}\ncatch (匹配Exception ex)\n{\n    子异常类名 异常处理语句组;\n}\ncatch (匹配Exception ex)\n{\n    父异常类名 异常处理语句组;\n}\n[ finally\n{\n    必定(即使try return)会被执行异常处理语句组:用于释放资源,不能捕获异常\n}]\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

catch:0~n, finally:可选项(无论是否有 break/return,都会被执行多遍)

  • 更好的形式
try\n{\n    try\n    {\n        code that might throw exceptions{};\n    }\n    finally\n    {\n        in.close();\n    }\n}\ncatch (IOException e)\n{\n    show error message{};\n    throw exception{};\n    throw new exception{};\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • 常用 catch 处理语句
System.out.println(Error+ e);\nSystem.out,println(Cause+ e.getCause());\nthrow e;\nthrow new Exception(String)/(String, e);\n
1
2
3
4

重抛异常

  • 将当前捕获的异常再次抛出:throw e;
  • 重新生成并抛出一个新异常(沿调用堆栈传递)
    • throw new Exception("some message");
    • throw new Exception("some message",e);
    • 将原始异常作为新异常的 initCause
catch (SQLException e)\n{\n    Throwable se = new ServletException(“database error”);\n    se.initCause(e);\n    throw se;\n}\n
1
2
3
4
5
6

上级便可通过 se.getCause()方法得到原始异常

关闭异常:关闭频率低/不可能发生的异常

catch 语句体为空,便可关闭捕获的异常

记录日志

调试技巧

使用断言

assert 表达式:字符串信息

JUnit 框架单元测试

import static org.JUnit.Assert.*

为每个类构置一个 main 方法进行单元测试

日志代理

Logger.getGlobal().info(" ……" + targetVariableName);

Stack Trace

  • Throwable.PintStackTrace

GUI 调试

泛型

泛型类

自定义类型 Class Person<T> {}

泛型方法

自定义方法 [修饰符] <T> T PeronMethod(Param) {}

泛型变量 T

<T extends SuperClass & Interface>\n
1

类型变量用“,”分隔,限定类型用“&”分隔(一个父类+多个接口)

<T extends Comparable & Serializable>\n
1

泛型代码

翻译关键:擦除类型参数,用 Object/限定类型代换

  • 虚拟机中没有泛型
  • 多态性:合成桥方法
  • 类型安全性:必要时插入强制类型转换

使用限制

  • T 类型变量:不可为基本类型,必须为引用类型
  • 类型查询(getClass()/instanceof):返回原始类型(如 Person),不返回 T 类型
  • 不可创建参数化类型的数组(如 Person<T>[])
  • 不可实例化类型变量(如 new T()、new T[])
  • 不可在静态域/方法中引用类型变量(如 private static T variableName)
  • 泛型类不可扩展 Throwable 及其子类(因此泛型类不可被抛出/捕获)

通配符类型

<? extends SuperClass>\n<? super SubClass>\n
1
2

反射与泛型

集合

集合接口

Collection Interface

Abstract Collection

将 size 和 iterator 抽象化,将 contains、toString 具体化

迭代器
  • 工作位置: 迭代器位于两元素间
  • 工作机制
    • previous 方法:迭代器越过前一元素,并将该元素的引用作为返回值
    • next 方法:迭代器越过下一元素,并将该元素的引用作为返回值
  • 工作方法
    • add 方法:将新元素的引用插入至迭代器当前位置
    • remove 方法:与 previous/next 相互依赖,删除前一次越过元素(不可连续调用 remove 方法)
    • next->( remove )->next->remove
  • 使用原则
    • 给容器附加多个只读迭代器,单独附加唯一一个读写迭代器
    • 并发修改检测: 只检测结构性改变(改变长度),不检测数值性改变(如 set 方法)
Iterable Interface

任何实现了 Iterable 接口及其子接口(如 Collection 接口)的对象都可使用 for each 循环

public interface Iterable<E>\n{\n    Iterator<E> iterator();\n}\n
1
2
3
4

Queue Interface and Deque Interface

Deque extends Queue extends Collection)

具体集合

数组列表

ArrayList implements List:

  • 随机访问:利用索引值访问元素
  • get/set(int Index)方法:效率高

链表

LinkedList implements List,Deque:

  • 非随机存取:必须从头/尾开始遍历链表访问元素
  • E get(int Index)方法:效率极低

ListIterator:

  • add 方法:依赖于迭代器位置
  • remove 方法:依赖于迭代器状态
  • previousIndex 方法:返回迭代器前一元素的整数索引
  • nextIndex 方法:返回迭代器后一元素的整数索引

散列集

HashSet implements Set:无序无重复元素集,基于 HashTable

  • 散列表:列表/链表数组(多个列表/链表),每个列表/链表成为桶(bucket)
    • 元素的桶的整数索引:元素散列码 mod 桶的总数(余数)
    • 桶数:预计元素个数的 75%~150% e.g 标准类库桶数为 2 的 n 次方,默认值 16
    • 散列冲突(hash collision):目标桶已满
    • 再散列(rehashed):创建大通数(如双倍桶数)的新散列表,丢弃原散列表
    • 装填因子(load factor):再散列时机决定因素,一般为 0.75(75%桶中含有至少一个元素)
    • hash 值:return (key.hashCode() & 0x7fffffff) % M;
  • 方法
    • add:先查找对象存在性,不存在则添加元素
    • contains:重写方法
  • 散列集迭代器:依次访问所有桶

树集

TreeSet implements SortedSet,NavigableSet:有序集

比较接口

Comparable Interface
// A<B负值,A==B零,A>B正值(B代表other)\npublic interface Comparable<T>\n{\n    int compareTo(T other);\n}\n
1
2
3
4
5
Comparator Interface
// A<B负值,A==B零,A>B正值\npublic interface Comparator<T>\n{\n    int compare(T a, T b);\n}\n\nSortedSet<Item> setSortedByName = new TreeSet<>(Comparator comp)//实现比较器的类\n
1
2
3
4
5
6
7

队列

  • 双端队列: ArrayDeque 类与 LinkedList 类
  • 优先级队列: PriorityQueue 类, 未对所有元素进行排序
    • add 和 remove 方法: 引用最小元素

映射表

实现 Map 接口,未实现 Collection 接口

  • 散列/比较函数只作用于键 key
  • 添加/删除/检索对象时,必须提供键 key
  • 键 key 具有唯一性

集合框架

RandomAccess 接口:标记接口,无任何方法,标记一个集合是否支持高效的随机访问

集合算法

早期集合

线程

线程基础

线程:分享 CPU、共享内存(多个线程访问同一数据/对象)

线程一般用于需长时间执行的任务:循环、下载、浏览图片

线程状态

Create Thread

创建线程的方法:

  • 父类:继承 Thread 类,重写 run 方法
  • 接口:new Thread(Runnable Task)实现 Runnable 接口的类:实名类/匿名类/Lambda 表达式

Runnable Thread

thread.start() 线程处于可运行状态:可能在运行,可能不在运行,不必始终保持运行

Block Thread

  • blocked
  • waiting
  • timed waiting
blocked

获取锁不得时,进入阻塞状态

waiting

等待调度器时,进入等待状态

timed waiting

调用含超时参数的方法时,进入计时等待状态保持到超时或通知

Object.wait、Thread.sleep、Thread.join、Lock.tryLock、Condition.await

terminated

run 方法正常退出或抛出未捕获异常时,进入(自然/意外)死亡状态

线程属性

优先级

  • 默认情况下继承父线程的优先级
  • 需防止低优先级线程被饿死(因此不要依赖优先级进行编程)
  • MIN_PRIORITY(1)~NORM_PRIORITY(5)~MAX_PRIORITY(10)
  • 每当线程调度器选择新线程时,首选具有较高优先级的线程

Daemon Thread

  • Thread.setDaemon(true)
  • 设置为后台线程:随时可能中断
  • 虚拟机会在只有后台线程时退出,后台线程不可访问固有资源(文件、数据库等)

未捕获异常的处理器

实现 Thread.UncaughtExceptionHandler 接口

不安装默认处理器时,默认处理器为空

中断线程

Interrupt Method

对一个线程调用此方法时,线程将进入中断状态

Interrupted Exception

对一个阻塞线程(调用 sleep/wait 方法等)调用 interrupt 方法时,抛出此异常

线程同步

两个线程都有多个语句,无法保证一个线程所有语句全部执行完再调用另一个线程,必然会出现交错调用不同线程中的语句现象,导致调用混乱现象

锁对象 (实例域)

可重复(持有计数),可共用(共用锁对象的方法可互相调用)

条件对象 (实例域)

管理有锁却不能正常工作的线程

一个锁对象可以有多个相关的条件对象

内部锁

  • synchronized 关键字
  • 每个对象都有一个内部锁,可将静态方法声明为 synchronized
  • 等价于 wait/notifyAll
  • 等价于 await/signalAll

截获内部锁

读写锁

监视器

volatile 关键字:修饰实例域

声明一个域可并发更新,通知编译器和虚拟器注意此特性

死锁

所有线程处于等待或阻塞状态

e.g. 两个线程互相等待状态

Thread Local Helper

线程安全

java.util.concurrent 并发 API:线程安全(同时只有一个线程调用某对象)

原子整数

AtomicInteger.getAndIncrement();     //cnt++;\nAtomicInteger.getAndDecrement();    //cnt—\nAtomicBooleanAtomicLongAtomicReference类\n
1
2
3

线程安全集合

  • 阻塞队列
  • 高效映射表、队列
  • 写数组列表和写数组集的拷贝 - CopyOnWriteArrayList 类、CopyOnWriteArraySet 类
  • 同步包装器(synchronization wrapper) - 任何集合类通过同步包装器变成线程安全集合类

异步计算

执行器

线程池:创建大量生命周期短的线程

  • CachedThreadPool:提交任务多,创建新线程
  • FixedThreadPool:提交任务多,等待当前任务完成再运行其他任务
  • SingleThreadExecutor:逐一执行提交任务
  • ScheduledExecutorService 接口:预定执行/重复执行任务

以上工厂方法返回:实现 ExecutorService 接口的 ThreadPoolExecutor 对象

  • 创建
ExecutorService pool = Executors.newCachedThreadPool();\n
1
  • 方法
pool.execute(myTask);\npool.shutdown();\n
1
2
  • Future 对象用于查询任务完成情况

同步器

线程与 Swing:事件分配线程

事件分配线程不应进行 input/output/sleep 调用(可能使线程阻塞)

  • Timer 类(亦是线程):每隔一段时间重复执行 MyTask
  • 更新图形化界面:SwingUtilities.invokeLater(Runnable MyTask);
EventQueue.invokeLater(new Runnable()\n{\npublic void run()\n{\n    Statements;\n}\n});\n
1
2
3
4
5
6
7
  • 指定布局(Layout)

以下可合成一个方法:

  • 创建组件(Component)
  • 添加组件(getContentPane.add(Component) //得到 Container 类)
  • 响应事件(Event)
  • 设置属性(size、location、bounds、locationByPlatform、title、iconImage、visible、resizable、undecorated、extendedState)
// 取得点击按钮的名字\nString itemName = ((JRadioButton) e.getSource()).getText();\n//Source:事件源(点击按钮事件)\n
1
2
3

Self-Defined Class

@override:

  • equals
  • hashCode
  • toString
  • Comparable(compareTo)
  • Cloneable(clone)

常用工具类

  • java.lang Java 语言的核心类库
  • java.util 实用工具
  • java.io 标准输入/输出类库
  • java.awt/javax.swing 图形用户界面(GUI)的类库
  • java.net 网络功能的类库
  • java.sql 数据库访问的类库
  • java.io 输入输出流

输入与输出

输入

  • InputStream 类:read(字节)
  • Reader 类:read(char)

输出

  • OutputStream 类:write、flush、close
  • Writer 类:write(char,String)、flush、close

正则表达式

基本写法

([字符]){数量}\\位置

Group RegExp

  • matcher.group(int)
  • 0 表示整个正则表达式,i 表示第 i 个圆括号

Garbage Collection

Garbage Collection Optimization

GC 优化的核心思路: 尽可能让对象在新生代中分配和回收, 尽量避免过多对象进入老年代, 导致对老年代频繁进行垃圾回收, 同时给系统足够的内存减少新生代垃圾回收次数.

分析系统的运行状况:

  • 系统每秒请求数, 每个请求创建多少对象, 占用多少内存
  • Young GC 触发频率, 对象进入老年代的速率
  • 老年代占用内存, Full GC 触发频率, Full GC 触发的原因, 长时间 Full GC 的原因

GC Tools

  • 监控告警系统:Zabbix、Prometheus、Open-Falcon
  • jdk 自动实时内存监控工具:VisualVM
  • 堆外内存监控: Java VisualVM 安装 Buffer Pools 插件, google perf 工具, Java NMT (Native Memory Tracking) 工具
  • GC 日志分析:GCViewer、gceasy
# jstat:JVM 自带命令行工具, 可用于统计内存分配速率、GC 次数,GC 耗时\njstat -gc <pid> <统计间隔时间>  <统计次数>\n\n# jmap:JVM 自带命令行工具, 可用于了解系统运行时的对象分布\njmap -histo <pid>\njmap -dump:live,format=b,file=dump.prof <pid>\n\n# 用来查看正在运行的 Java 应用程序的扩展参数\n# 包括 Java System 属性和 JVM 命令行参数\njinfo <pid>\n
1
2
3
4
5
6
7
8
9
10

Java Naming Conventions

使用前注意事项

  • 由于 Java 面向对象编程的特性, 在命名时应尽量选择名词
  • 驼峰命名法(Camel-Case)
    • 当变量名或函式名是由一个或多个单字连结在一起,而构成的唯一识别字时,首字母以小写开头,每个单词首字母大写(第一个单词除外)。

包名的书写规范 (Package)

推荐使用公司或机构的顶级域名为包名的前缀,目的是保证各公司/机构内所使用的包名的唯一性。包名全部为小写字母,且具有实际的区分意义。

Package 一般要求

  • 选择有意义的名字,能快速地传达该类的用途。
  • 所有包的命名必须采用小写英文字母。

Package 实际应用

应用系统中经常应用分层,Dao 层(数据库访问)、Service 层(业务处理)、Web 层(页面控制 action 类)

  • 包名的前几个为固定名称, 如果是网站的话,采用网站的域名的反写,如果域名还没有确定的话,采用公司固定的几个名称。如:net.vschool
  • 在包名的接下来一个单词为模块的名称。如:用户模块,包名为 net.vschool.user
  • 关于模块的访问操作,采用分层形式,一般分为:

Dao 层操作:一般定义在 net.vschool.xxx.dao 中,其中 xxx 为模块名称。
Service 层操作:一般定义在 net.vschool.xxx.service 中。
web 层操作:一般定义在 net.vschool.xxx.action 中。

net.vschool.user
net.vschool.user.dao
net.vschool.user.action
net.vschool.user.service

类名的书写规范 (Class)

类名必须使用名词,如果一个类名内含多个单词,那么各个单词第一个字母大写,后续字母小写,起伏呈驼峰状,人称驼峰式命名。给类名命名时,必须保证准确、简洁且容易理解。尽量使用完整单词,避免使用缩写词(除了大家公认的) 类

Class 一般要求

  • 选择有意义的名字,能快速地传达该类的用途。
  • 参照 java 驼峰命名法,类名的首字母必须采用大写的形式,如果类名为多词组合而成的话,那么每个词的首字母必须采用大写。如:StudentAnswer.java
  • 当要区别接口类和实现类的时候,可以在类的后面加上“Impl”。
  • 推荐实体类没有后缀名。
  • public class 应与.java 文件同名

Class 实际应用

应用系统中经常应用分层,Dao 层(数据库访问)、Service 层(业务处理)、Web 层(页面控制 action 类),每一层的类的名称尽量带上该层后缀。

  • Dao 层

    • 接口类:采用 JavaBean+Interface+Dao 的形式来定义,即,实体对象+Interface+Dao
    • 实现类:采用 JavaBean+Interface+Impl+Dao 的形式来定义,即,实体对象+Interface+Impl+Dao
  • Service 层

    • 接口类:采用 Xxx+Interface+Service 的形式来定义,即,模块+Interface+Service
    • 实现类:采用 Xxx+Interface+Impl+Service 的形式来定义,即,模块+Interface+Impl+Service
  • Web 层(action 类)

    • 实现类:采用县 Xxx+Operator+Action 的形式来定义,即,模块+操作+Action

变量的命名

普通变量

Var 一般要求
  • 选择有意义的名字,能快速地传达该变量的用途。
  • 参照 java 驼峰命名法,首字母以小写开头,每个单词首字母大写(第一个单词除外)。
Var 实际应用
  • 变量命名采用基本结构为 typeVariableName,使用 3 字符前缀来表示数据类型。
    • 做数组用时,再加前缀-a,如字符串数组:astr,
    • 自定义类型的变量可以采用本身的名称,把首字母改为小写。
    • 采用名称要能代表在方法中的意义。如果员工列表:employeeList
原词缩写
bytebyt
charchr
floatflt
booleanbln
Integer/intint
shortsht
Long/longlng
Double/doubledbl
stringstr
变量使用技巧
  • 在一段函数中不使用同一个变量表示前后意义不同的两个数值。
  • 除非是在循环中,否则一般不推荐使用单个字母作为变量名,i、j、k 等只作为小型循环的循环索引变量。
  • 避免用 Flag 来命名状态变量。
  • 用 Is 来命名逻辑变量,如:blnFileIsFound。通过这种给布尔变量肯定形式的命名方式,使得其它开发人员能够更为清楚的理解布尔变量所代表的意义。
  • 如果需要对变量名进行缩写时,一定要注意整个代码中缩写规则的一致性。 例如,如果在代码的某些区域中使用 intCnt,而在另一些区域中又使用 intCount,就会给代码增加不必要的复杂性. 建议变量名中尽量不要出现缩写.

静态变量

  • 选择有意义的名字,能快速地传达该变量的用途。
  • 参照 java 驼峰命名法,采用全部大写的形式来书写,对于采用多词合成的变量采用-来连接各单词。如:USER_LIST

方法的命名

Method 一般要求

  • 选择有意义的名字,能快速地传达该方法的用途。
  • 参照 java 驼峰命名法,首字母以小写开头,每个单词首字母大写(第一个单词除外)。

Method 实际应用

  • 方法表示一种行为,它代表一种动作,最好是一个动词或者动词词组或者第一个单词为一个动词。
  • 属性方法:以 get/set 开头,其后跟字段名称,字段名称首字母大写。如:getUserName()
  • 数据层方法:只能以 insert(插入),delete(删除),update(更新),select(查找),count(统计)开头 他层方法避免以这个 5 个单词开头,以免造成误解。
  • 服务层方法,根据方法的行为命名,只描述方法的意义,而不采用方法的目的命名. 比如系统的添加新用户,用户可以前台注册,也可以管理员后台添加,方法会被重用,所以最好不要用使用 register,采用 add 会更好写. 避免使用与 web 层相关的方法.
  • Web 层方法最好是贴近 web 的语言,如 register,login,logout 等方法。
',712);l.render=function(s,e,l,p,i,r){return a(),n("div",null,[t])};export default l;export{e as __pageData}; diff --git a/assets/language_java_javaBasicNotes.md.4f729496.lean.js b/assets/language_java_javaBasicNotes.md.4f729496.lean.js new file mode 100644 index 00000000000..776ce6dcf02 --- /dev/null +++ b/assets/language_java_javaBasicNotes.md.4f729496.lean.js @@ -0,0 +1 @@ +import{o as a,c as n,a as s}from"./app.6dd2a1b7.js";const e='{"title":"Java Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"CLI Tools","slug":"cli-tools"},{"level":3,"title":"编译","slug":"编译"},{"level":3,"title":"运行","slug":"运行"},{"level":3,"title":"反汇编","slug":"反汇编"},{"level":3,"title":"打包","slug":"打包"},{"level":2,"title":"Gradle Tool","slug":"gradle-tool"},{"level":3,"title":"Build Gradle","slug":"build-gradle"},{"level":2,"title":"基本结构","slug":"基本结构"},{"level":3,"title":"数据类型","slug":"数据类型"},{"level":3,"title":"常量","slug":"常量"},{"level":3,"title":"运算符","slug":"运算符"},{"level":3,"title":"字符串","slug":"字符串"},{"level":3,"title":"输入输出","slug":"输入输出"},{"level":3,"title":"表达式","slug":"表达式"},{"level":3,"title":"控制流程","slug":"控制流程"},{"level":3,"title":"大数值","slug":"大数值"},{"level":3,"title":"数组","slug":"数组"},{"level":2,"title":"对象与类","slug":"对象与类"},{"level":3,"title":"面向对象的三大特征","slug":"面向对象的三大特征"},{"level":3,"title":"完整的 Java 源文件","slug":"完整的-java-源文件"},{"level":3,"title":"Behavior State and Identity","slug":"behavior-state-and-identity"},{"level":3,"title":"预定义类","slug":"预定义类"},{"level":3,"title":"用户自定义类","slug":"用户自定义类"},{"level":3,"title":"方法 Method","slug":"方法-method"},{"level":3,"title":"对象构造","slug":"对象构造"},{"level":3,"title":"包","slug":"包"},{"level":3,"title":"类路径","slug":"类路径"},{"level":3,"title":"文档注释","slug":"文档注释"},{"level":3,"title":"类设计技巧","slug":"类设计技巧"},{"level":2,"title":"继承","slug":"继承"},{"level":3,"title":"父类与子类(superclass and subclass)","slug":"父类与子类-superclass-and-subclass"},{"level":3,"title":"Object 类","slug":"object-类"},{"level":3,"title":"泛型数组列表","slug":"泛型数组列表"},{"level":3,"title":"对象包装器与自动装箱","slug":"对象包装器与自动装箱"},{"level":3,"title":"可变参数方法","slug":"可变参数方法"},{"level":3,"title":"枚举类","slug":"枚举类"},{"level":3,"title":"访问控制符","slug":"访问控制符"},{"level":3,"title":"非访问控制符","slug":"非访问控制符"},{"level":3,"title":"反射(Reflection)","slug":"反射-reflection"},{"level":2,"title":"接口与内部类","slug":"接口与内部类"},{"level":3,"title":"接口","slug":"接口"},{"level":3,"title":"对象克隆","slug":"对象克隆"},{"level":3,"title":"回调","slug":"回调"},{"level":3,"title":"内部类","slug":"内部类"},{"level":3,"title":"代理类","slug":"代理类"},{"level":2,"title":"图形程序设计","slug":"图形程序设计"},{"level":3,"title":"Swing 组件继承层次","slug":"swing-组件继承层次"},{"level":3,"title":"Swing 组件功能层次","slug":"swing-组件功能层次"},{"level":3,"title":"JFrame","slug":"jframe"},{"level":3,"title":"框架基础","slug":"框架基础"},{"level":3,"title":"自定义绘制组件","slug":"自定义绘制组件"},{"level":3,"title":"图形颜色","slug":"图形颜色"},{"level":2,"title":"事件处理","slug":"事件处理"},{"level":3,"title":"定义与基础","slug":"定义与基础"},{"level":3,"title":"动作","slug":"动作"},{"level":3,"title":"鼠标事件","slug":"鼠标事件"},{"level":3,"title":"AWT 事件继承层次","slug":"awt-事件继承层次"},{"level":2,"title":"Swing 组件","slug":"swing-组件"},{"level":3,"title":"Awt 和 Swing 设计中的设计模式","slug":"awt-和-swing-设计中的设计模式"},{"level":3,"title":"简单布局管理器","slug":"简单布局管理器"},{"level":3,"title":"复杂布局管理器","slug":"复杂布局管理器"},{"level":3,"title":"文本组件","slug":"文本组件"},{"level":3,"title":"选择组件","slug":"选择组件"},{"level":3,"title":"菜单组件","slug":"菜单组件"},{"level":3,"title":"对话框(顶级框架)","slug":"对话框-顶级框架"},{"level":2,"title":"异常、断言、日志、调试","slug":"异常、断言、日志、调试"},{"level":3,"title":"处理错误","slug":"处理错误"},{"level":3,"title":"捕获异常","slug":"捕获异常"},{"level":3,"title":"记录日志","slug":"记录日志"},{"level":3,"title":"调试技巧","slug":"调试技巧"},{"level":2,"title":"泛型","slug":"泛型"},{"level":3,"title":"泛型类","slug":"泛型类"},{"level":3,"title":"泛型方法","slug":"泛型方法"},{"level":3,"title":"泛型变量 T","slug":"泛型变量-t"},{"level":3,"title":"泛型代码","slug":"泛型代码"},{"level":3,"title":"使用限制","slug":"使用限制"},{"level":3,"title":"通配符类型","slug":"通配符类型"},{"level":3,"title":"反射与泛型","slug":"反射与泛型"},{"level":2,"title":"集合","slug":"集合"},{"level":3,"title":"集合接口","slug":"集合接口"},{"level":3,"title":"具体集合","slug":"具体集合"},{"level":3,"title":"集合框架","slug":"集合框架"},{"level":3,"title":"集合算法","slug":"集合算法"},{"level":3,"title":"早期集合","slug":"早期集合"},{"level":2,"title":"线程","slug":"线程"},{"level":3,"title":"线程基础","slug":"线程基础"},{"level":3,"title":"线程状态","slug":"线程状态"},{"level":3,"title":"线程属性","slug":"线程属性"},{"level":3,"title":"中断线程","slug":"中断线程"},{"level":3,"title":"线程同步","slug":"线程同步"},{"level":3,"title":"线程安全","slug":"线程安全"},{"level":3,"title":"异步计算","slug":"异步计算"},{"level":3,"title":"执行器","slug":"执行器"},{"level":3,"title":"同步器","slug":"同步器"},{"level":3,"title":"线程与 Swing:事件分配线程","slug":"线程与-swing-事件分配线程"},{"level":2,"title":"Self-Defined Class","slug":"self-defined-class"},{"level":2,"title":"常用工具类","slug":"常用工具类"},{"level":2,"title":"输入与输出","slug":"输入与输出"},{"level":3,"title":"输入","slug":"输入"},{"level":3,"title":"输出","slug":"输出"},{"level":2,"title":"正则表达式","slug":"正则表达式"},{"level":3,"title":"基本写法","slug":"基本写法"},{"level":3,"title":"Group RegExp","slug":"group-regexp"},{"level":2,"title":"Garbage Collection","slug":"garbage-collection"},{"level":3,"title":"Garbage Collection Optimization","slug":"garbage-collection-optimization"},{"level":3,"title":"GC Tools","slug":"gc-tools"},{"level":2,"title":"Java Naming Conventions","slug":"java-naming-conventions"},{"level":3,"title":"使用前注意事项","slug":"使用前注意事项"},{"level":3,"title":"包名的书写规范 (Package)","slug":"包名的书写规范-(package)"},{"level":3,"title":"类名的书写规范 (Class)","slug":"类名的书写规范-class"},{"level":3,"title":"变量的命名","slug":"变量的命名"},{"level":3,"title":"方法的命名","slug":"方法的命名"}],"relativePath":"language/java/javaBasicNotes.md","lastUpdated":1627196319000}',l={},t=s('',712);l.render=function(s,e,l,p,i,r){return a(),n("div",null,[t])};export default l;export{e as __pageData}; diff --git a/assets/language_python_pythonBasicNotes.md.f5d1a476.js b/assets/language_python_pythonBasicNotes.md.f5d1a476.js new file mode 100644 index 00000000000..7f52a58a8bc --- /dev/null +++ b/assets/language_python_pythonBasicNotes.md.f5d1a476.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"Python Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Python Basics","slug":"python-basics"},{"level":3,"title":"Basic Types","slug":"basic-types"},{"level":3,"title":"Basic Control Statement","slug":"basic-control-statement"},{"level":3,"title":"Functions","slug":"functions"},{"level":3,"title":"Classes","slug":"classes"},{"level":2,"title":"NumPy Basics","slug":"numpy-basics"},{"level":3,"title":"Numpy Array Creation","slug":"numpy-array-creation"},{"level":3,"title":"Numpy Indexing","slug":"numpy-indexing"},{"level":3,"title":"Numpy Matrix Operations","slug":"numpy-matrix-operations"},{"level":2,"title":"CLI Application","slug":"cli-application"},{"level":3,"title":"Basic CLI","slug":"basic-cli"},{"level":3,"title":"Progress Bar","slug":"progress-bar"},{"level":2,"title":"Process Data Files","slug":"process-data-files"},{"level":3,"title":"CSV File","slug":"csv-file"},{"level":3,"title":"JSON File","slug":"json-file"},{"level":3,"title":"XML File","slug":"xml-file"},{"level":3,"title":"Plain Text File","slug":"plain-text-file"},{"level":3,"title":"Converter","slug":"converter"},{"level":2,"title":"Matplotlib Usage","slug":"matplotlib-usage"},{"level":3,"title":"Plot Type","slug":"plot-type"},{"level":3,"title":"Basic Usage","slug":"basic-usage"},{"level":2,"title":"Plot Style","slug":"plot-style"},{"level":2,"title":"Plot Axis Tick","slug":"plot-axis-tick"},{"level":2,"title":"Plot Legend","slug":"plot-legend"},{"level":2,"title":"Subplot","slug":"subplot"},{"level":3,"title":"Double y-axis","slug":"double-y-axis"},{"level":2,"title":"3D Plot","slug":"_3d-plot"},{"level":2,"title":"Paper Figures Config","slug":"paper-figures-config"},{"level":2,"title":"Paper Standard","slug":"paper-standard"},{"level":2,"title":"Awesome Library","slug":"awesome-library"},{"level":3,"title":"Debugging and Testing","slug":"debugging-and-testing"}],"relativePath":"language/python/pythonBasicNotes.md","lastUpdated":1627196319000}',t={},e=a('

Python Basic Notes

Python Basics

Basic Types

Tuples

tuples in python are immutable

Basic Control Statement

For Loop

for i in range(3): # goes from i = 0 to i = 2\n  x += 1\n\nfor i in [0, 1, 2]:\n  x += 1\n
1
2
3
4
5

While Loop

while x > 0:\n  x -= 1\n
1
2

Functions

def join_name(first_name, last_name):\n  joined_name = first_name + " " + last_name\n  return joined_name\n
1
2
3

Classes

class Pet(object):\n  def __init__(self, species, color, name):\n    self.species = species\n    self.color = color\n    self.name = name\n\n  def __str__(self):\n    return "{0} {1} named {2}.".format(self.color, self.species, self.name)\n\n  def change_name(self, new_name):\n    self.name = new_name\n\nmy_dog = Pet(species="dog", color="orange", name="Guises")\nprint(my_dog)\nprint(my_dog.name)\n# => output:\n# orange dog named Guises.\n# Guises\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

NumPy Basics

import numpy as np\n\nnp.random.seed(seed=1234)\n
1
2
3

Numpy Array Creation

x = np.array(6)\nx.ndim\nx.shape\nx.size\nx.dtype\n
1
2
3
4
5
np.zeros((2, 2))\nnp.ones((2, 2))\nnp.eye((2))\nnp.random.random.((2, 2))\n
1
2
3
4

Numpy Indexing

x = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\nx[:, 1]    # [2, 6, 10]\nx[0, :]    # [1, 2, 3, 4]\nx[:3, 1:3] # [[2, 3], [6, 7], [10, 11]]\nx[[0, 1, 2], [0, 2, 1]] # [1, 7, 10]\n
1
2
3
4
5
# Boolean array indexing\nx = np.array([[1,2], [3, 4], [5, 6]])\nprint ("x:\\n", x)\nprint ("x > 2:\\n", x > 2)\nprint ("x[x > 2]:\\n", x[x > 2])\n# x:\n#  [[1 2]\n#  [3 4]\n#  [5 6]]\n# x > 2:\n#  [[False False]\n#  [ True  True]\n#  [ True  True]]\n# x[x > 2]:\n#  [3 4 5 6]\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Numpy Matrix Operations

  • math: x+y/x-y/x*y np.add/subtract/multiply
  • dot product: a.dot(b)
  • sum: np.sum(x)
  • column sum: np.sum(x, axis=0)
  • row sum: np.sum(x, axis=1)
  • transposing: x.T
  • reshape: np.reshape(x, (2, 3))

CLI Application

Basic CLI

import click\n\nfrom caesar_encryption import encrypt\n\n@click.command()\n@click.option(\n    '--input_file',\n    type=click.File('r'),\n    help='File in which there is the text you want to encrypt/decrypt.'\n         'If not provided, a prompt will allow you to type the input text.',\n)\n@click.option(\n    '--output_file',\n    type=click.File('w'),\n    help='File in which the encrypted / decrypted text will be written.'\n         'If not provided, the output text will just be printed.',\n)\n@click.option(\n    '--decrypt/--encrypt',\n    '-d/-e',\n    help='Whether you want to encrypt the input text or decrypt it.'\n)\n@click.option(\n    '--key',\n    '-k',\n    default=1,\n    help='The numeric key to use for the caesar encryption / decryption.'\n)\ndef caesar(input_file, output_file, decrypt, key):\n    if input_file:\n        text = input_file.read()\n    else:\n        text = click.prompt('Enter a text', hide_input=not decrypt)\n    if decrypt:\n        key = -key\n    cypherText = encrypt(text, key)\n    if output_file:\n        output_file.write(cypherText)\n    else:\n        click.echo(cypherText)\n\nif __name__ == '__main__':\n    caesar()\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

Progress Bar

import click\nimport enchant\n\nfrom tqdm import tqdm\n\nfrom caesar_encryption import encrypt\n\n@click.command()\n@click.option(\n    '--input_file',\n    type=click.File('r'),\n    required=True,\n)\n@click.option(\n    '--output_file',\n    type=click.File('w'),\n    required=True,\n)\ndef caesar_breaker(input_file, output_file):\n    cypherText = input_file.read()\n    english_dictionary = enchant.Dict("en_US")\n    best_number_of_english_words = 0\n    for key in tqdm(range(26)):\n        plaintext = encrypt(cypherText, -key)\n        number_of_english_words = 0\n        for word in plaintext.split(' '):\n            if word and english_dictionary.check(word):\n                number_of_english_words += 1\n        if number_of_english_words > best_number_of_english_words:\n            best_number_of_english_words = number_of_english_words\n            best_plaintext = plaintext\n            best_key = key\n    click.echo(f'The most likely encryption key is {best_key}. It gives the\n    following plaintext:\\n\\n{best_plaintext[:1000]}...')\n    output_file.write(best_plaintext)\n\nif __name__ == '__main__':\n    caesar_breaker()\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

Process Data Files

CSV File

import csv\n\nfilename = "my_data.csv"\n\nfields = []\nrows = []\n\n# Reading csv file\nwith open(filename, 'r') as csvFile:\n    # Creating a csv reader object\n    csvReader = csv.reader(csvFile)\n\n    # Extracting field names in the first row\n    fields = csvReader.next()\n\n    # Extracting each data row one by one\n    for row in csvReader:\n        rows.append(row)\n\n# Printing out the first 5 rows\nfor row in rows[:5]:\n    print(row)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import csv\n\n# Field names\nfields = ['Name', 'Goals', 'Assists', 'Shots']\n\n# Rows of data in the csv file\nrows = [ ['Emily', '12', '18', '112'],\n         ['Katie', '8', '24', '96'],\n         ['John', '16', '9', '101'],\n         ['Mike', '3', '14', '82']]\n\nfilename = "soccer.csv"\n\n# Writing to csv file\nwith open(filename, 'w+') as csvFile:\n    # Creating a csv writer object\n    csvWriter = csv.writer(csvFile)\n\n    # Writing the fields\n    csvWriter.writerow(fields)\n\n    # Writing the data rows\n    csvWriter.writerows(rows)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import pandas as pd\n\nfilename = "my_data.csv"\n\n# Read in the data\ndata = pd.read_csv(filename)\n\n# Print the first 5 rows\nprint(data.head(5))\n\n# Write the data to file\ndata.to_csv("new_data.csv", sep=",", index=False)\n
1
2
3
4
5
6
7
8
9
10
11
12

JSON File

import json\nimport pandas as pd\n\n# Read the data from file\n# We now have a Python dictionary\nwith open('data.json') as f:\n    data_listOfDict = json.load(f)\n\n# We can do the same thing with pandas\ndata_df = pd.read_json('data.json', orient='records')\n\n# We can write a dictionary to JSON like so\n# Use 'indent' and 'sort_keys' to make the JSON\n# file look nice\nwith open('new_data.json', 'w+') as json_file:\n    json.dump(data_listOfDict, json_file, indent=4, sort_keys=True)\n\n# And again the same thing with pandas\nexport = data_df.to_json('new_data.json', orient='records')\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

XML File

import xml.etree.ElementTree as ET\nimport xmltodict\nimport json\n\ntree = ET.parse('output.xml')\nxml_data = tree.getroot()\n\nxmlStr = ET.tostring(xml_data, encoding='utf8', method='xml')\n\n\ndata_dict = dict(xmltodict.parse(xmlStr))\n\nprint(data_dict)\n\nwith open('new_data_2.json', 'w+') as json_file:\n    json.dump(data_dict, json_file, indent=4, sort_keys=True)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Plain Text File

import numpy as np\nx, y = np.loadtxt('input.dat', delimiter=',', unpack=True)\n
1
2

Converter

import pandas as pd\nfrom dicttoxml import dicttoxml\nimport json\n\n# Building our dataframe\ndata = {'Name': ['Emily', 'Katie', 'John', 'Mike'],\n        'Goals': [12, 8, 16, 3],\n        'Assists': [18, 24, 9, 14],\n        'Shots': [112, 96, 101, 82]\n        }\n\ndf = pd.DataFrame(data, columns=data.keys())\n\n# Converting the dataframe to a dictionary\n# Then save it to file\ndata_dict = df.to_dict(orient="records")\nwith open('output.json', "w+") as f:\n    json.dump(data_dict, f, indent=4)\n\n# Converting the dataframe to XML\n# Then save it to file\nxml_data = dicttoxml(data_dict).decode()\nwith open("output.xml", "w+") as f:\n    f.write(xml_data)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import json\nimport pandas as pd\nimport csv\n\n# Read the data from file\n# We now have a Python dictionary\nwith open('data.json') as f:\n    data_listOfDict = json.load(f)\n\n# Writing a list of dicts to CSV\nkeys = data_listOfDict[0].keys()\nwith open('saved_data.csv', 'wb') as output_file:\n    dict_writer = csv.DictWriter(output_file, keys)\n    dict_writer.writeheader()\n    dict_writer.writerows(data_listOfDict)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Matplotlib Usage

pip3 install matplotlib\npip3 install ggplot\n
1
2

Plot Type

  • bar plot
  • line plot
  • scatter plot
  • pie plot
  • stack plot
  • histogram plot
  • interval plot
  • box plot
  • KDE plot
  • candlestick_ohlc plot

Basic Usage

import matplotlib.pyplot as plt\n\nfig = plt.figure()\nax = plt.subplot2grid((1, 1), (0, 0))\n\nax.fill_between(x, y, y[0],where=(y > y[0]), facecolor='g', alpha=0.5)\n\nax.grid(True)\nax.xaxis.label.set_color('c') ax.yaxis.label.set_color('r') ax.set_yticks([0,25,50,75])\nax.xaxis.get_ticklabels()\n\nax.spines['left'].set_color('c') ax.spines['right'].set_visible(False)\nax.spines['top'].set_visible(False) ax.spines['left'].set_linewidth(5)\nax.tick_params(axis='x', colors='#f06215')\nax.axhline(20, color='k', linewidth=5)\n\nax.text(x, y, 'text')\nax.annotate(\n  'Bad News!',\n  xytext=(0.8, 0.9),\n  textcoords='axes fraction',\n  arrowprops = dict(facecolor='grey', color='grey')\n)\n\nplt.plot(x, y, label='label', color='red', linewidth=5)\nplt.bar(x, y, label='label', color='#111')\nplt.scatter(x, y, label='label', marker='*', size=100)\nplt.stackplot(x, y1, y2, ..., yn, colors=[])\nplt.pie(slices, labels=[], colors=[], startangle=90, explode=(), autopct='%1.1f%%')\nplt.hist(data, bins=10, histtype='bar', rwidth=0.8)\n\nplt.xlabel('xlabel')\nplt.ylabel('ylabel')\nplt.title('title')\nplt.legend()\nplt.subplots_adjust()\nplt.show()\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

Plot Style

from matplotlib import style\nprint(plt.style.available)\nprint(plt.__file__)\nstyle.use('ggplot')\nstyle.use('fivethirtyeight')\nplt.style.use('mystylesheet.mplrc')\n
1
2
3
4
5
6

Plot Axis Tick

ax3.xaxis.set_major_formatter(mDates.DateFormatter('%Y-%m-%d'))\nax3.xaxis.set_major_locator(mTicker.MaxNLocator(10))\nax1.yaxis.set_major_locator(mTicker.MaxNLocator(nbins=5, prune='lower'))\n\nfor label in ax3.xaxis.get_ticklabels():\n    label.set_rotation(45)\n\nplt.setp(ax1.get_xticklabels(), visible=False)\nplt.setp(ax2.get_xticklabels(), visible=False)\n
1
2
3
4
5
6
7
8
9

Plot Legend

up middle legend

ax.legend()\nleg = ax.legend(loc=9, ncol=2,prop={'size':11})\nleg.get_frame().set_alpha(0.4)\n
1
2
3

Subplot

tall and width for grid template

fig = plt.figure()\nax1 = fig.add_subplot(221)\nax2 = fig.add_subplot(222)\nax3 = fig.add_subplot(212)\n\nax1 = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)\nax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1)\nax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1)\n
1
2
3
4
5
6
7
8

Double y-axis

ax2v = ax2.twinx()\n
1

3D Plot

from mpl_toolkits.mplot3d import axes3d\nimport matplotlib.pyplot as plt\nfrom matplotlib import style\n\nstyle.use('ggplot')\n\nfig = plt.figure()\nax1 = fig.add_subplot(111, projection='3d')\n\nx = [1,2,3,4,5,6,7,8,9,10]\ny = [5,6,7,8,2,5,6,3,7,2]\nz = [1,2,6,3,2,7,3,3,7,2]\n\nx2 = [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]\ny2 = [-5,-6,-7,-8,-2,-5,-6,-3,-7,-2]\nz2 = [1,2,6,3,2,7,3,3,7,2]\n\nax1.scatter(x, y, z, c='g', marker='o')\nax1.scatter(x2, y2, z2, c ='r', marker='o')\n\nax1.set_xlabel('x axis')\nax1.set_ylabel('y axis')\nax1.set_zlabel('z axis')\n\nplt.show()\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from mpl_toolkits.mplot3d import axes3d\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib import style\nstyle.use('ggplot')\n\nfig = plt.figure()\nax1 = fig.add_subplot(111, projection='3d')\n\nx3 = [1,2,3,4,5,6,7,8,9,10]\ny3 = [5,6,7,8,2,5,6,3,7,2]\nz3 = np.zeros(10)\n\ndx = np.ones(10)\ndy = np.ones(10)\ndz = [1,2,3,4,5,6,7,8,9,10]\n\nax1.bar3d(x3, y3, z3, dx, dy, dz)\n\n\nax1.set_xlabel('x axis')\nax1.set_ylabel('y axis')\nax1.set_zlabel('z axis')\n\nplt.show()\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Paper Figures Config

import matplotlib.pyplot as plt\nimport matplotlib\n\ndef latexify(fig_width=None, fig_height=None, columns=1):\n    '''Set up matplotlib RC params for LaTeX plotting.\n    Call this before plotting a figure.\n\n    Parameters\n    ----------\n    fig_width : float, optional, inches\n    fig_height : float,  optional, inches\n    columns : {1, 2}\n    '''\n\n    # code adapted from http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples\n\n    # Width and max height in inches for IEEE journals taken from\n    # computer.org/cms/Computer.org/Journal%20templates/transactions_art_guide.pdf\n\n    assert(columns in [1,2])\n\n    if fig_width is None:\n        fig_width = 3.39 if columns==1 else 6.9 # width in inches\n\n    if fig_height is None:\n        golden_mean = (sqrt(5)-1.0)/2.0    # Aesthetic ratio\n        fig_height = fig_width*golden_mean # height in inches\n\n    MAX_HEIGHT_INCHES = 8.0\n    if fig_height > MAX_HEIGHT_INCHES:\n        print("WARNING: fig_height too large:" + fig_height +\n              "so will reduce to" + MAX_HEIGHT_INCHES + "inches.")\n        fig_height = MAX_HEIGHT_INCHES\n\n    params = {'backend': 'ps',\n              'text.latex.preamble': ['\\usepackage{gensymb}'],\n              'axes.labelsize': 8, # fontsize for x and y labels (was 10)\n              'axes.titlesize': 8,\n              'text.fontsize': 8, # was 10\n              'legend.fontsize': 8, # was 10\n              'xtick.labelsize': 8,\n              'ytick.labelsize': 8,\n              'text.usetex': True,\n              'figure.figsize': [fig_width,fig_height],\n              'font.family': 'serif'\n    }\n\n    matplotlib.rcParams.update(params)\n\n\ndef format_axes(ax):\n\n    for spine in ['top', 'right']:\n        ax.spines[spine].set_visible(False)\n\n    for spine in ['left', 'bottom']:\n        ax.spines[spine].set_color(SPINE_COLOR)\n        ax.spines[spine].set_linewidth(0.5)\n\n    ax.xaxis.set_ticks_position('bottom')\n    ax.yaxis.set_ticks_position('left')\n\n    for axis in [ax.xaxis, ax.yaxis]:\n        axis.set_tick_params(direction='out', color=SPINE_COLOR)\n\n    return ax\n\nlatexify()\nax.set_xlabel("X label")\nax.set_ylabel("Y label")\nax.set_title("Title")\nplt.tight_layout()\nformat_axes(ax)\nplt.savefig("figure.pdf")\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import numpy as np\nimport matplotlib as mpl\nmpl.use('pdf')\nimport matplotlib.pyplot as plt\n\nplt.rc('font', family='serif', serif='Times')\nplt.rc('text', usetex=True)\nplt.rc('xtick', labelsize=8)\nplt.rc('ytick', labelsize=8)\nplt.rc('axes', labelsize=8)\n\n# width as measured in ink scape\nwidth = 3.487\nheight = width / 1.618\n\nfig, ax = plt.subplots()\nfig.subplots_adjust(left=.15, bottom=.16, right=.99, top=.97)\n\nx = np.arange(0.0, 3*np.pi , 0.1)\nplt.plot(x, np.sin(x))\n\nax.set_ylabel('Some Metric (in unit)')\nax.set_xlabel('Something (in unit)')\nax.set_xlim(0, 3*np.pi)\n\nfig.set_size_inches(width, height)\nfig.savefig('plot.pdf')\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Paper Standard

Awesome Library

Debugging and Testing

',73);t.render=function(a,p,t,o,c,l){return n(),s("div",null,[e])};export default t;export{p as __pageData}; diff --git a/assets/language_python_pythonBasicNotes.md.f5d1a476.lean.js b/assets/language_python_pythonBasicNotes.md.f5d1a476.lean.js new file mode 100644 index 00000000000..ff0e2ac9bb4 --- /dev/null +++ b/assets/language_python_pythonBasicNotes.md.f5d1a476.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"Python Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Python Basics","slug":"python-basics"},{"level":3,"title":"Basic Types","slug":"basic-types"},{"level":3,"title":"Basic Control Statement","slug":"basic-control-statement"},{"level":3,"title":"Functions","slug":"functions"},{"level":3,"title":"Classes","slug":"classes"},{"level":2,"title":"NumPy Basics","slug":"numpy-basics"},{"level":3,"title":"Numpy Array Creation","slug":"numpy-array-creation"},{"level":3,"title":"Numpy Indexing","slug":"numpy-indexing"},{"level":3,"title":"Numpy Matrix Operations","slug":"numpy-matrix-operations"},{"level":2,"title":"CLI Application","slug":"cli-application"},{"level":3,"title":"Basic CLI","slug":"basic-cli"},{"level":3,"title":"Progress Bar","slug":"progress-bar"},{"level":2,"title":"Process Data Files","slug":"process-data-files"},{"level":3,"title":"CSV File","slug":"csv-file"},{"level":3,"title":"JSON File","slug":"json-file"},{"level":3,"title":"XML File","slug":"xml-file"},{"level":3,"title":"Plain Text File","slug":"plain-text-file"},{"level":3,"title":"Converter","slug":"converter"},{"level":2,"title":"Matplotlib Usage","slug":"matplotlib-usage"},{"level":3,"title":"Plot Type","slug":"plot-type"},{"level":3,"title":"Basic Usage","slug":"basic-usage"},{"level":2,"title":"Plot Style","slug":"plot-style"},{"level":2,"title":"Plot Axis Tick","slug":"plot-axis-tick"},{"level":2,"title":"Plot Legend","slug":"plot-legend"},{"level":2,"title":"Subplot","slug":"subplot"},{"level":3,"title":"Double y-axis","slug":"double-y-axis"},{"level":2,"title":"3D Plot","slug":"_3d-plot"},{"level":2,"title":"Paper Figures Config","slug":"paper-figures-config"},{"level":2,"title":"Paper Standard","slug":"paper-standard"},{"level":2,"title":"Awesome Library","slug":"awesome-library"},{"level":3,"title":"Debugging and Testing","slug":"debugging-and-testing"}],"relativePath":"language/python/pythonBasicNotes.md","lastUpdated":1627196319000}',t={},e=a('',73);t.render=function(a,p,t,o,c,l){return n(),s("div",null,[e])};export default t;export{p as __pageData}; diff --git a/assets/language_verilog_verilogBasicNotes.md.8503596e.js b/assets/language_verilog_verilogBasicNotes.md.8503596e.js new file mode 100644 index 00000000000..8218d79aecf --- /dev/null +++ b/assets/language_verilog_verilogBasicNotes.md.8503596e.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as e}from"./app.6dd2a1b7.js";const t='{"title":"Verilog Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"General Flow","slug":"general-flow"},{"level":3,"title":"Create Project","slug":"create-project"},{"level":3,"title":"RTL Analysis","slug":"rtl-analysis"},{"level":3,"title":"Simulate Design","slug":"simulate-design"},{"level":3,"title":"Synthesize Design","slug":"synthesize-design"},{"level":3,"title":"Implement Design","slug":"implement-design"},{"level":3,"title":"Post-Implementation Timing Simulate Design","slug":"post-implementation-timing-simulate-design"},{"level":3,"title":"Generate Bit Stream","slug":"generate-bit-stream"},{"level":3,"title":"Testing and Verification","slug":"testing-and-verification"},{"level":2,"title":"Timing in Circuits","slug":"timing-in-circuits"},{"level":3,"title":"Combination Circuit Timing","slug":"combination-circuit-timing"},{"level":3,"title":"Sequential Circuit Timing","slug":"sequential-circuit-timing"},{"level":2,"title":"Key Words","slug":"key-words"},{"level":2,"title":"Module","slug":"module"},{"level":3,"title":"外部端口","slug":"外部端口"},{"level":2,"title":"function","slug":"function"},{"level":3,"title":"task","slug":"task"},{"level":2,"title":"Data Structure","slug":"data-structure"},{"level":3,"title":"常量","slug":"常量"},{"level":3,"title":"向量","slug":"向量"},{"level":3,"title":"数字","slug":"数字"},{"level":3,"title":"Register","slug":"register"},{"level":3,"title":"Net","slug":"net"},{"level":2,"title":"Gate Level","slug":"gate-level"},{"level":3,"title":"Basic Gate","slug":"basic-gate"},{"level":3,"title":"Use Gate","slug":"use-gate"},{"level":3,"title":"Self-Defined Gate(用户自定义原语)","slug":"self-defined-gate-用户自定义原语"},{"level":2,"title":"Dataflow Level","slug":"dataflow-level"},{"level":3,"title":"Operators","slug":"operators"},{"level":3,"title":"整数提升","slug":"整数提升"},{"level":2,"title":"Behavior Level","slug":"behavior-level"},{"level":3,"title":"时延控制","slug":"时延控制"},{"level":3,"title":"事件控制","slug":"事件控制"},{"level":3,"title":"语句内/间控制","slug":"语句内-间控制"},{"level":3,"title":"always","slug":"always"},{"level":3,"title":"if-else","slug":"if-else"},{"level":3,"title":"Case Statement","slug":"case-statement"},{"level":3,"title":"for","slug":"for"},{"level":3,"title":"repeat loop","slug":"repeat-loop"},{"level":3,"title":"forever loop","slug":"forever-loop"},{"level":3,"title":"Force and Release","slug":"force-and-release"},{"level":3,"title":"Blocking and Non-Blocking","slug":"blocking-and-non-blocking"},{"level":3,"title":"disable","slug":"disable"},{"level":2,"title":"结构建模","slug":"结构建模"},{"level":3,"title":"generate 语句","slug":"generate-语句"},{"level":2,"title":"Delay(时延)","slug":"delay-时延"},{"level":2,"title":"预编译指令","slug":"预编译指令"},{"level":3,"title":"define 宏","slug":"define-宏"},{"level":2,"title":"Data Path","slug":"data-path"},{"level":3,"title":"Multiplexer","slug":"multiplexer"},{"level":3,"title":"Adder","slug":"adder"},{"level":3,"title":"Register Data Path","slug":"register-data-path"},{"level":3,"title":"Memory","slug":"memory"},{"level":2,"title":"Demos","slug":"demos"},{"level":3,"title":"Binary Multiplier","slug":"binary-multiplier"},{"level":3,"title":"Multi-Hz","slug":"multi-hz"},{"level":2,"title":"Tips","slug":"tips"},{"level":3,"title":"不可综合结构","slug":"不可综合结构"},{"level":3,"title":"混合编程","slug":"混合编程"},{"level":3,"title":"上升沿/下降沿","slug":"上升沿-下降沿"},{"level":3,"title":"Parameter","slug":"parameter"},{"level":2,"title":"有限状态机(FSM)","slug":"有限状态机-fsm"},{"level":2,"title":"算术状态机(ASM)","slug":"算术状态机-asm"},{"level":2,"title":"System Verilog","slug":"system-verilog"},{"level":3,"title":"Enum","slug":"enum"},{"level":3,"title":"Struct and Union","slug":"struct-and-union"},{"level":3,"title":"Procedural Block","slug":"procedural-block"},{"level":3,"title":"Interface","slug":"interface"},{"level":3,"title":"Testing","slug":"testing"},{"level":2,"title":"U280 Platform","slug":"u280-platform"},{"level":3,"title":"Host Application","slug":"host-application"},{"level":3,"title":"U280 Tools","slug":"u280-tools"},{"level":2,"title":"AXI Protocol","slug":"axi-protocol"},{"level":3,"title":"AXI Channels","slug":"axi-channels"},{"level":3,"title":"AXI Burst","slug":"axi-burst"},{"level":2,"title":"Verilog Components","slug":"verilog-components"},{"level":3,"title":"Clock Unit","slug":"clock-unit"},{"level":3,"title":"Signal Unit","slug":"signal-unit"},{"level":3,"title":"ALU Unit","slug":"alu-unit"},{"level":3,"title":"Memory Unit","slug":"memory-unit"}],"relativePath":"language/verilog/verilogBasicNotes.md","lastUpdated":1627196319000}',o={},l=a("h1",{id:"verilog-basic-notes"},[a("a",{class:"header-anchor",href:"#verilog-basic-notes","aria-hidden":"true"},"#"),e(" Verilog Basic Notes")],-1),c=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#verilog-basic-notes"},"Verilog Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#general-flow"},"General Flow")]),a("li",null,[a("a",{href:"#timing-in-circuits"},"Timing in Circuits")]),a("li",null,[a("a",{href:"#key-words"},"Key Words")]),a("li",null,[a("a",{href:"#module"},"Module")]),a("li",null,[a("a",{href:"#function"},"function")]),a("li",null,[a("a",{href:"#data-structure"},"Data Structure")]),a("li",null,[a("a",{href:"#gate-level"},"Gate Level")]),a("li",null,[a("a",{href:"#dataflow-level"},"Dataflow Level")]),a("li",null,[a("a",{href:"#behavior-level"},"Behavior Level")]),a("li",null,[a("a",{href:"#结构建模"},"结构建模")]),a("li",null,[a("a",{href:"#delay-时延"},"Delay(时延)")]),a("li",null,[a("a",{href:"#预编译指令"},"预编译指令")]),a("li",null,[a("a",{href:"#data-path"},"Data Path")]),a("li",null,[a("a",{href:"#demos"},"Demos")]),a("li",null,[a("a",{href:"#tips"},"Tips")]),a("li",null,[a("a",{href:"#有限状态机-fsm"},"有限状态机(FSM)")]),a("li",null,[a("a",{href:"#算术状态机-asm"},"算术状态机(ASM)")]),a("li",null,[a("a",{href:"#system-verilog"},"System Verilog")]),a("li",null,[a("a",{href:"#u280-platform"},"U280 Platform")]),a("li",null,[a("a",{href:"#axi-protocol"},"AXI Protocol")]),a("li",null,[a("a",{href:"#verilog-components"},"Verilog Components")])])])])])],-1),p=a("h2",{id:"general-flow"},[a("a",{class:"header-anchor",href:"#general-flow","aria-hidden":"true"},"#"),e(" General Flow")],-1),u=a("h3",{id:"create-project"},[a("a",{class:"header-anchor",href:"#create-project","aria-hidden":"true"},"#"),e(" Create Project")],-1),r=a("ul",null,[a("li",null,[a("code",null,".v"),e(" (sources)")]),a("li",null,[a("code",null,".xdc"),e(" (constraints)")])],-1),i=a("h3",{id:"rtl-analysis"},[a("a",{class:"header-anchor",href:"#rtl-analysis","aria-hidden":"true"},"#"),e(" RTL Analysis")],-1),k=a("p",null,"Schematic",-1),b=a("h3",{id:"simulate-design"},[a("a",{class:"header-anchor",href:"#simulate-design","aria-hidden":"true"},"#"),e(" Simulate Design")],-1),m=a("ul",null,[a("li",null,[a("code",null,"_tb.v"),e(": test bench")]),a("li",null,"simulation options: 500ns"),a("li",null,"Scopes -> dut -> swt/led"),a("li",null,"Schematic")],-1),d=a("h3",{id:"synthesize-design"},[a("a",{class:"header-anchor",href:"#synthesize-design","aria-hidden":"true"},"#"),e(" Synthesize Design")],-1),g=a("p",null,"Schematic",-1),w=a("h3",{id:"implement-design"},[a("a",{class:"header-anchor",href:"#implement-design","aria-hidden":"true"},"#"),e(" Implement Design")],-1),y=a("p",null,"Schematic",-1),h=a("h3",{id:"post-implementation-timing-simulate-design"},[a("a",{class:"header-anchor",href:"#post-implementation-timing-simulate-design","aria-hidden":"true"},"#"),e(" Post-Implementation Timing Simulate Design")],-1),f=a("h3",{id:"generate-bit-stream"},[a("a",{class:"header-anchor",href:"#generate-bit-stream","aria-hidden":"true"},"#"),e(" Generate Bit Stream")],-1),v=a("h3",{id:"testing-and-verification"},[a("a",{class:"header-anchor",href:"#testing-and-verification","aria-hidden":"true"},"#"),e(" Testing and Verification")],-1),_=a("p",null,"low-level (circuit) simulation is much slower than high-level (C, HDL) simulation:",-1),A=a("ul",null,[a("li",null,"check only functionality at high level (C, HDL)"),a("li",null,"check only timing, power at low level (circuit)")],-1),D=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token function"},"testBench"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// clock and reset are internal"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" a"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},","),e(" c"),a("span",{class:"token punctuation"},","),e(" yExpected"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// values from testVectors"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" y"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// output of circuit"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" vectorNum"),a("span",{class:"token punctuation"},","),e(" errors"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// bookkeeping variables"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" testVectors"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// array of testVectors"),e("\n\n "),a("span",{class:"token comment"},"// instantiate device under test"),e("\n sillyFunction "),a("span",{class:"token function"},"dut"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"a"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"b"),a("span",{class:"token punctuation"},"("),e("b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"c"),a("span",{class:"token punctuation"},"("),e("c"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"y"),a("span",{class:"token punctuation"},"("),e("y"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// generate clock"),e("\n "),a("span",{class:"token important"},"always "),e(" "),a("span",{class:"token comment"},"// no sensitivity list, so it always executes"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token number"},"#5"),a("span",{class:"token punctuation"},";"),e(" clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token number"},"#5"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 10ns period"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token comment"},"// at start of test, load vectors and pulse reset"),e("\n "),a("span",{class:"token keyword"},"initial"),e(" "),a("span",{class:"token comment"},"// Only executes once"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token property"},"$readmemb"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"example.tv"'),a("span",{class:"token punctuation"},","),e(" testVectors"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// Read vectors: e.g 000_0 001_1 ... xxx_x"),e("\n vectorNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" errors "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// Initialize"),e("\n reset "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token number"},"#27"),a("span",{class:"token punctuation"},";"),e(" reset "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// Apply reset wait"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token comment"},"// Note: $readmemh reads testVector files written in"),e("\n "),a("span",{class:"token comment"},"// hexadecimal"),e("\n "),a("span",{class:"token comment"},"// apply test vectors on rising edge of clk"),e("\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token number"},"#1"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token operator"},"{"),e("a"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},","),e(" c"),a("span",{class:"token punctuation"},","),e(" yExpected"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"="),e(" testVectors"),a("span",{class:"token punctuation"},"["),e("vectorNum"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"negedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"~"),e("reset"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},"// don’t test during reset"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("y "),a("span",{class:"token operator"},"!=="),e(" yExpected"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Error: inputs = %b"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"{"),e("a"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},","),e(" c"),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'" outputs = %b (%b exp)"'),a("span",{class:"token punctuation"},","),e("y"),a("span",{class:"token punctuation"},","),e("yExpected"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n errors "),a("span",{class:"token operator"},"="),e(" errors "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token comment"},"// increment array index and read next testVector"),e("\n vectorNum "),a("span",{class:"token operator"},"="),e(" vectorNum "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("testVectors"),a("span",{class:"token punctuation"},"["),e("vectorNum"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"==="),e(),a("span",{class:"token number"},"4'bx"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"%d tests completed with %d errors"'),a("span",{class:"token punctuation"},","),e(" vectorNum"),a("span",{class:"token punctuation"},","),e(" errors"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// End simulation"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br")])],-1),T=a("h2",{id:"timing-in-circuits"},[a("a",{class:"header-anchor",href:"#timing-in-circuits","aria-hidden":"true"},"#"),e(" Timing in Circuits")],-1),x=a("h3",{id:"combination-circuit-timing"},[a("a",{class:"header-anchor",href:"#combination-circuit-timing","aria-hidden":"true"},"#"),e(" Combination Circuit Timing")],-1),I=a("ul",null,[a("li",null,[e("contamination delay ("),a("code",null,"t_cd"),e("): minimum path in circuits, outputs start to change")]),a("li",null,[e("propagation delay ("),a("code",null,"t_pd"),e("): maximum path in circuits, outputs complete change")]),a("li",null,[e("(delay) heavy dependence on "),a("strong",null,"voltage"),e(" and "),a("strong",null,"temperature")])],-1),B=a("h3",{id:"sequential-circuit-timing"},[a("a",{class:"header-anchor",href:"#sequential-circuit-timing","aria-hidden":"true"},"#"),e(" Sequential Circuit Timing")],-1),W=a("p",null,[e("minimize clock skew time: requires intelligent "),a("strong",null,"clock network"),e(" across a chip, making clock arrives at all locations at roughly the same time.")],-1),C=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("T_clock "),a("span",{class:"token operator"},">="),e(" T_pcq "),a("span",{class:"token operator"},"+"),e(" T_pd "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token punctuation"},"("),e("T_setup "),a("span",{class:"token operator"},"+"),e(" T_skew"),a("span",{class:"token punctuation"},")"),e("\nT_ccq "),a("span",{class:"token operator"},"+"),e(" T_cd "),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),e("T_hold "),a("span",{class:"token operator"},"+"),e(" T_skew"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),H=a("h2",{id:"key-words"},[a("a",{class:"header-anchor",href:"#key-words","aria-hidden":"true"},"#"),e(" Key Words")],-1),N=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e("\n"),a("span",{class:"token keyword"},"input"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"output"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"inout"),e("\n"),a("span",{class:"token keyword"},"wire"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"reg"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"parameter"),e("\n"),a("span",{class:"token important"},"always@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"assign"),e("\n"),a("span",{class:"token keyword"},"posedge"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"negedge"),e("\n"),a("span",{class:"token keyword"},"if"),a("span",{class:"token operator"},"-"),a("span",{class:"token keyword"},"else"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"case"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"begin"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e(),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),O=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("port"),a("span",{class:"token punctuation"},","),e(" signal\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),z=a("h2",{id:"module"},[a("a",{class:"header-anchor",href:"#module","aria-hidden":"true"},"#"),e(" Module")],-1),S=a("ul",null,[a("li",null,"Inout(Net) -> (Net)Module(Net) -> Inout(Net)"),a("li",null,"Input(Net/Register) -> (Net)Module(Net/Register) -> Output(Net)")],-1),P=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token function"},"moduleName"),a("span",{class:"token punctuation"},"("),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},","),e(" Out1"),a("span",{class:"token punctuation"},","),e(" Out2"),a("span",{class:"token punctuation"},","),e(" InOut1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"// 输入输出定义: 说明端口是输入还是输出"),e("\n "),a("span",{class:"token keyword"},"input"),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(" Out1"),a("span",{class:"token punctuation"},","),e(" Out2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"inout"),e(" InOut1"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// 数据类型定义: 说明端口数据类型 - Net/Register"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},","),e(" Out1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" InOut1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" Out2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"// Instantiation of lower level modules"),e("\n Design "),a("span",{class:"token function"},"u_2"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"("),e("端口"),a("span",{class:"token punctuation"},"("),e("信号"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"("),e("端口"),a("span",{class:"token function"},"2"),a("span",{class:"token punctuation"},"("),e("信号"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"("),e("端口"),a("span",{class:"token function"},"3"),a("span",{class:"token punctuation"},"("),e("信号"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// Functionality"),e("\n\n "),a("span",{class:"token comment"},"// 三种层次的描述: 逻辑层次(Gate Level), 数据流层次(Dataflow Level), 行为层次(Behavior Level)"),e("\n "),a("span",{class:"token comment"},"// and/or - gate level"),e("\n "),a("span",{class:"token keyword"},"and"),e(),a("span",{class:"token function"},"and1"),a("span",{class:"token punctuation"},"("),e(" Out1"),a("span",{class:"token punctuation"},","),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"// assign - dataflow level"),e("\n "),a("span",{class:"token keyword"},"assign"),e(),a("span",{class:"token number"},"#2"),e(" Out1 "),a("span",{class:"token operator"},"="),e(" In1 "),a("span",{class:"token operator"},"&"),e(" In2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"// always/initial - behavior level"),e("\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n Out2 "),a("span",{class:"token operator"},"="),e(" In1 "),a("span",{class:"token operator"},"&"),e(" In2\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token comment"},"// Timing specification"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br")])],-1),U=a("h3",{id:"外部端口"},[a("a",{class:"header-anchor",href:"#外部端口","aria-hidden":"true"},"#"),e(" 外部端口")],-1),M=a("ul",null,[a("li",null,"封装内部端口,装饰者模式"),a("li",null,"一个内部端口可与多个外部端口相连")],-1),E=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" scram_b "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),e("arb"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"control"),a("span",{class:"token punctuation"},"("),e("ctrl"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"mem_word"),a("span",{class:"token punctuation"},"("),e("mem_blk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"addr"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"byte"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(" arb"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" ctrl"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" mem_blk"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"byte"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br")])],-1),L=a("h2",{id:"function"},[a("a",{class:"header-anchor",href:"#function","aria-hidden":"true"},"#"),e(" function")],-1),R=a("ul",null,[a("li",null,"不含时间/事件控制"),a("li",null,"至少 1 个输入"),a("li",null,"至多 1 个输出"),a("li",null,"只含行为模块"),a("li",null,"只含阻塞赋值语句")],-1),F=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aligned_word"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// function declaration"),e("\n "),a("span",{class:"token keyword"},"input"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" word"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n aligned_word "),a("span",{class:"token operator"},"="),e(" word"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("aligned_word "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"while"),e(),a("span",{class:"token punctuation"},"("),e("aligned_word"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(" aligned_word "),a("span",{class:"token operator"},"="),e(" aligned_word "),a("span",{class:"token operator"},"<<"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),$=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" arithmetic_unit "),a("span",{class:"token punctuation"},"("),e("result_1"),a("span",{class:"token punctuation"},","),e(" result_2"),a("span",{class:"token punctuation"},","),e(" operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" result_1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" result_2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" result_1 "),a("span",{class:"token operator"},"="),e(" sum_of_operands "),a("span",{class:"token punctuation"},"("),e("operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" result_2 "),a("span",{class:"token operator"},"="),e(" larger_operand "),a("span",{class:"token punctuation"},"("),e("operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token function"},"sum_of_operands"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n sum_of_operands "),a("span",{class:"token operator"},"="),e(" operand_1 "),a("span",{class:"token operator"},"+"),e(" operand_2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token function"},"larger_operand"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n larger_operand "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("operand_1 "),a("span",{class:"token operator"},">="),e(" operand_2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" operand_1 "),a("span",{class:"token punctuation"},":"),e(" operand_2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br")])],-1),q=a("h3",{id:"task"},[a("a",{class:"header-anchor",href:"#task","aria-hidden":"true"},"#"),e(" task")],-1),V=a("p",null,"将测试流程分为多个任务:",-1),G=a("ul",null,[a("li",null,"初始化任务"),a("li",null,"模拟生成任务"),a("li",null,"自我检查任务")],-1),X=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" adder_task "),a("span",{class:"token punctuation"},"("),e("c_out"),a("span",{class:"token punctuation"},","),e(" sum"),a("span",{class:"token punctuation"},","),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset"),a("span",{class:"token punctuation"},","),e(" c_in"),a("span",{class:"token punctuation"},","),e(" data_a"),a("span",{class:"token punctuation"},","),e(" data_b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" sum"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(" c_out"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" data_a"),a("span",{class:"token punctuation"},","),e(" data_b"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset"),a("span",{class:"token punctuation"},","),e(" c_in"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk "),a("span",{class:"token keyword"},"or"),e(),a("span",{class:"token keyword"},"posedge"),e(" reset"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("reset"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"{"),e("c_out"),a("span",{class:"token punctuation"},","),e(" sum"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"else"),e(" add_values "),a("span",{class:"token punctuation"},"("),e("sum"),a("span",{class:"token punctuation"},","),e(" c_out"),a("span",{class:"token punctuation"},","),e(" data_a"),a("span",{class:"token punctuation"},","),e(" data_b"),a("span",{class:"token punctuation"},","),e(" c_in"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// invoke task"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"task"),e(" add_values"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// task declaration"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" SUM"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(" C_OUT"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" DATA_A"),a("span",{class:"token punctuation"},","),e(" DATA_B"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" C_IN"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token operator"},"{"),e("C_OUT"),a("span",{class:"token punctuation"},","),e(" SUM"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"="),e(" DATA_A "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token punctuation"},"("),e("DATA_B "),a("span",{class:"token operator"},"+"),e(" C_IN"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br")])],-1),K=a("p",null,'常用的 task 有: $display("fmt", ...), $monitor("fmt", ...), $time, $finish',-1),j=a("h2",{id:"data-structure"},[a("a",{class:"header-anchor",href:"#data-structure","aria-hidden":"true"},"#"),e(" Data Structure")],-1),Z=a("h3",{id:"常量"},[a("a",{class:"header-anchor",href:"#常量","aria-hidden":"true"},"#"),e(" 常量")],-1),Y=a("ul",null,[a("li",null,"0: 逻辑 0"),a("li",null,"1: 逻辑 1"),a("li",null,"x/X: Unknown/Floating"),a("li",null,"z/Z: 高阻抗状态(High Impedance)"),a("li",null,"parameter: #define")],-1),Q=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"localparam"),e(" idle "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"2'b00"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"parameter"),e(" Bit "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),e(" cnt_up "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1'b1"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("Bit "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" A"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("Bit "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" A"),a("span",{class:"token punctuation"},";"),e("\n\nA "),a("span",{class:"token operator"},"="),e(" A "),a("span",{class:"token operator"},"+"),e(" cnt_up"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),J=a("h3",{id:"向量"},[a("a",{class:"header-anchor",href:"#向量","aria-hidden":"true"},"#"),e(" 向量")],-1),nn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"["),e("MSB"),a("span",{class:"token punctuation"},":"),e(" LSB"),a("span",{class:"token punctuation"},"]"),e(" 或 "),a("span",{class:"token punctuation"},"["),e("LSB"),a("span",{class:"token punctuation"},":"),e(" MSB"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),sn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" A"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 4 bit"),e("\n"),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 4 bit"),e("\n"),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"63"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" D"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 64 bit"),e("\n"),a("span",{class:"token keyword"},"wire"),e(" E"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 1 bit"),e("\n\nA"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(" B"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" C"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A[2] = 0 & C[1], A[1] = B[0] & C[2]"),e("\nB"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\nC "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'b1011"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// C[0] = 1, C[1] = 0, C[2] = 1, C[3] = 1"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),an=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"integer"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" A "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 4元素数组. 每个元素为16bit元"),e("\n"),a("span",{class:"token keyword"},"reg"),e(" B "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// arr[4][16]数组, 每个元素为1bit元"),e("\n"),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" C "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// arr[4][16]数组, 每个元素为8bit元"),e("\n\nA"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 置零A中第三个元素(8 bit)"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 置零A"),e("\nB"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// B[1][0](1 bit)置1"),e("\nC"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'b0010"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// C[0][0] 低4bit为0010"),e("\nC"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// C[2][8] 第5bit为1"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),en=a("h4",{id:"部分位选"},[a("a",{class:"header-anchor",href:"#部分位选","aria-hidden":"true"},"#"),e(" 部分位选")],-1),tn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("vector"),a("span",{class:"token punctuation"},"["),e("base_expr"),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},":"),e(" const_width"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\nvector"),a("span",{class:"token punctuation"},"["),e("base_expr"),a("span",{class:"token operator"},"-"),a("span",{class:"token punctuation"},":"),e(" const_width"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\ninst_mode"),a("span",{class:"token punctuation"},"["),e("mark"),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// => mark,mark+1"),e("\ngpio_mode"),a("span",{class:"token punctuation"},"["),e("mark"),a("span",{class:"token operator"},"-"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// => mark,mark-1,mark-2,mark-3"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),on=a("h3",{id:"数字"},[a("a",{class:"header-anchor",href:"#数字","aria-hidden":"true"},"#"),e(" 数字")],-1),ln=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// size ' signed base value"),e("\n"),a("span",{class:"token operator"},"<"),e("Bits长度"),a("span",{class:"token operator"},">"),e("'"),a("span",{class:"token punctuation"},"["),a("span",{class:"token keyword"},"signed"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token operator"},"<"),e("进制"),a("span",{class:"token operator"},"><"),e("数值"),a("span",{class:"token operator"},">"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),cn=a("ul",null,[a("li",null,"位长不能用表达式表示,只可用固定的 parameter")],-1),pn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("Num "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"5'b01101"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 二进制"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 十进制"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"12'b0000_1111_0000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 可读性"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'hf"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 十六进制(1111)"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'bxxx1"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 前三位未知"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'bz01"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 前两位为z, 后两位为01"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),un=a("h4",{id:"有符号数"},[a("a",{class:"header-anchor",href:"#有符号数","aria-hidden":"true"},"#"),e(" 有符号数")],-1),rn=a("ul",null,[a("li",null,"signed reg"),a("li",null,"signed wire"),a("li",null,"integer"),a("li",null,"'sxx")],-1),kn=a("h4",{id:"无符号数"},[a("a",{class:"header-anchor",href:"#无符号数","aria-hidden":"true"},"#"),e(" 无符号数")],-1),bn=a("ul",null,[a("li",null,"reg"),a("li",null,"wire"),a("li",null,"'xx")],-1),mn=a("h3",{id:"register"},[a("a",{class:"header-anchor",href:"#register","aria-hidden":"true"},"#"),e(" Register")],-1),dn=a("ul",null,[a("li",null,"reg/integer/time/real/realtime"),a("li",null,"有记忆性"),a("li",null,"默认值: x")],-1),gn=a("h4",{id:"integer"},[a("a",{class:"header-anchor",href:"#integer","aria-hidden":"true"},"#"),e(" integer")],-1),wn=a("p",null,"长度为 32 Bit, 补码表示, 常用于计数器",-1),yn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" CLK"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"integer"),e(" i"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("i "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"="),e(" i "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(" tmp"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(" In"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),hn=a("h4",{id:"real"},[a("a",{class:"header-anchor",href:"#real","aria-hidden":"true"},"#"),e(" real")],-1),fn=a("ul",null,[a("li",null,"real 默认值为 0,不可为 x/z"),a("li",null,"不可声明位宽")],-1),vn=a("h3",{id:"net"},[a("a",{class:"header-anchor",href:"#net","aria-hidden":"true"},"#"),e(" Net")],-1),_n=a("ul",null,[a("li",null,"wire/wand/wor"),a("li",null,"局部变量, 没有记忆性"),a("li",null,"默认值: z"),a("li",null,"wire 间不可直接相连, wand/wor 间课直接相连"),a("li",null,"可用 wire 定义局部变量")],-1),An=a("h2",{id:"gate-level"},[a("a",{class:"header-anchor",href:"#gate-level","aria-hidden":"true"},"#"),e(" Gate Level")],-1),Dn=a("h3",{id:"basic-gate"},[a("a",{class:"header-anchor",href:"#basic-gate","aria-hidden":"true"},"#"),e(" Basic Gate")],-1),Tn=a("ul",null,[a("li",null,"and"),a("li",null,"nand(与非)"),a("li",null,"or"),a("li",null,"nor(或非)"),a("li",null,"xor(异或)"),a("li",null,"xnor(同或)")],-1),xn=a("h3",{id:"use-gate"},[a("a",{class:"header-anchor",href:"#use-gate","aria-hidden":"true"},"#"),e(" Use Gate")],-1),In=a("ul",null,[a("li",null,"同一模块中, 实例名不能与线网名相同")],-1),Bn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"and"),e(),a("span",{class:"token punctuation"},"("),e("w1"),a("span",{class:"token punctuation"},","),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// w1 = Int and In2"),e("\n"),a("span",{class:"token keyword"},"or"),e(),a("span",{class:"token function"},"or1"),a("span",{class:"token punctuation"},"("),e("w2"),a("span",{class:"token punctuation"},","),e(" w1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// w2 = w1 or In2"),e("\n"),a("span",{class:"token keyword"},"xor"),e(),a("span",{class:"token function"},"xor"),a("span",{class:"token punctuation"},"("),e("Out"),a("span",{class:"token punctuation"},","),e(" w1"),a("span",{class:"token punctuation"},","),e(" w2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// Out = w1 xor w2"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Wn=a("ul",null,[a("li",null,"实例数组")],-1),Cn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" irq"),a("span",{class:"token punctuation"},","),e(" ctrl"),a("span",{class:"token punctuation"},","),e(" sense"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"/*\n * =>\n * nand\n * u8nand3 (irq[3], ctrl[3], sense[3]);\n * u8nand2 (irq[2], ctrl[2], sense[2]);\n * u8nand1 (irq[1], ctrl[1], sense[1]);\n * u8nand0 (irq[0], ctrl[0], sense[0]);\n */"),e("\n"),a("span",{class:"token keyword"},"nand"),e(" u8nand "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"("),e("irq"),a("span",{class:"token punctuation"},","),e(" ctrl"),a("span",{class:"token punctuation"},","),e(" sense"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Hn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"parameter"),e(" NUM_BITS "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),e("NUM_BITS "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" gated_d"),a("span",{class:"token punctuation"},","),e(" din"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"wire"),e(" bypass"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"and"),e(" #"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(" u0and "),a("span",{class:"token punctuation"},"["),e("NUM_BITS "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"("),e("gated_d"),a("span",{class:"token punctuation"},","),e(" din"),a("span",{class:"token punctuation"},","),e(" bypass"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Nn=a("h3",{id:"self-defined-gate-用户自定义原语"},[a("a",{class:"header-anchor",href:"#self-defined-gate-用户自定义原语","aria-hidden":"true"},"#"),e(" Self-Defined Gate(用户自定义原语)")],-1),On=a("ul",null,[a("li",null,"可以有一个/多个输入"),a("li",null,"只能有一个输出"),a("li",null,"第一个端口必须是输出端口"),a("li",null,[a("code",null,"-"),e(' 表示 值"无变化"')])],-1),zn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"primitive"),e(" XOR2 "),a("span",{class:"token punctuation"},"("),e("D_OUT"),a("span",{class:"token punctuation"},","),e(" X1"),a("span",{class:"token punctuation"},","),e(" X2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" X1"),a("span",{class:"token punctuation"},","),e(" X2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(" D_OUT"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"table"),e(),a("span",{class:"token comment"},"// X1 X2 : D_OUT"),e("\n "),a("span",{class:"token number"},"0"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"0"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"1"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"1"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtable"),e("\n"),a("span",{class:"token keyword"},"endprimitive"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Sn=a("h2",{id:"dataflow-level"},[a("a",{class:"header-anchor",href:"#dataflow-level","aria-hidden":"true"},"#"),e(" Dataflow Level")],-1),Pn=a("ul",null,[a("li",null,[e("assign net = net/reg: "),a("strong",null,"左式只能是 net")])],-1),Un=a("h3",{id:"operators"},[a("a",{class:"header-anchor",href:"#operators","aria-hidden":"true"},"#"),e(" Operators")],-1),Mn=a("ul",null,[a("li",null,[a("a",{href:"https://hom-wang.gitbooks.io/verilog-hdl/content/Chapter_04.html",target:"_blank",rel:"noopener noreferrer"},"Operators List")])],-1),En=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("赋值"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token operator"},"<="),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"="),e("\n"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"<"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"<="),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},">="),e("\n"),a("span",{class:"token operator"},"!="),a("span",{class:"token punctuation"},"."),e(),a("span",{class:"token operator"},"=="),e("\n"),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),e(),a("span",{class:"token operator"},"{"),e(),a("span",{class:"token operator"},"}"),e("\n"),a("span",{class:"token operator"},"<<"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},">>"),e("\n"),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"-"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"/"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"%"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Ln=a("h3",{id:"整数提升"},[a("a",{class:"header-anchor",href:"#整数提升","aria-hidden":"true"},"#"),e(" 整数提升")],-1),Rn=a("ul",null,[a("li",null,"表达式所有中间取 最大位宽(最长(左/右)操作数)")],-1),Fn=a("h4",{id:""},[a("a",{class:"header-anchor",href:"#","aria-hidden":"true"},"#"),e(" { }")],-1),$n=a("p",null,[e("可实现 haskell 中的"),a("strong",null,"模式匹配")],-1),qn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 连接运算符"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{"),e(),a("span",{class:"token number"},"1'b0"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"1'b1"),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A = 2'b01"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{"),e(" B"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(" D"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A = B[1], B[2], C[0], D[2]"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"2'b01"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A = 4'b0101"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{"),e(),a("span",{class:"token number"},"3'b101"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'b0"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A = 5'b101_00"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Vn=a("h2",{id:"behavior-level"},[a("a",{class:"header-anchor",href:"#behavior-level","aria-hidden":"true"},"#"),e(" Behavior Level")],-1),Gn=a("ul",null,[a("li",null,[e("reg = net/reg: *"),a("em",null,"左式只能是 reg")])],-1),Xn=a("h3",{id:"时延控制"},[a("a",{class:"header-anchor",href:"#时延控制","aria-hidden":"true"},"#"),e(" 时延控制")],-1),Kn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("#num\n\n"),a("span",{class:"token keyword"},"parameter"),e(" cycle "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"30"),a("span",{class:"token punctuation"},";"),e("\n\n# "),a("span",{class:"token number"},"2"),e("\n# cycle"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),jn=a("h3",{id:"事件控制"},[a("a",{class:"header-anchor",href:"#事件控制","aria-hidden":"true"},"#"),e(" 事件控制")],-1),Zn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n@"),a("span",{class:"token punctuation"},"("),e(" sel"),a("span",{class:"token punctuation"},","),e(" a"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n@"),a("span",{class:"token punctuation"},"("),e("sel "),a("span",{class:"token keyword"},"or"),e(" a "),a("span",{class:"token keyword"},"or"),e(" b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" CLK"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"negedge"),e(" CLK"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Yn=a("h3",{id:"语句内-间控制"},[a("a",{class:"header-anchor",href:"#语句内-间控制","aria-hidden":"true"},"#"),e(" 语句内/间控制")],-1),Qn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("q "),a("span",{class:"token operator"},"="),e(" @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk_iol"),a("span",{class:"token punctuation"},")"),e(" d"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// 语句内事件控制"),e("\n\n@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk_iol"),a("span",{class:"token punctuation"},")"),e(" "),a("span",{class:"token comment"},"// 语句间事件控制"),e("\n q "),a("span",{class:"token operator"},"="),e(" temp"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Jn=a("h3",{id:"always"},[a("a",{class:"header-anchor",href:"#always","aria-hidden":"true"},"#"),e(" always")],-1),ns=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("事件"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" 事件"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ss=a("h3",{id:"if-else"},[a("a",{class:"header-anchor",href:"#if-else","aria-hidden":"true"},"#"),e(" if-else")],-1),as=a("ul",null,[a("li",null,"必须添加 else")],-1),es=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("condition1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("condition2"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"else"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),ts=a("h3",{id:"case-statement"},[a("a",{class:"header-anchor",href:"#case-statement","aria-hidden":"true"},"#"),e(" Case Statement")],-1),os=a("ul",null,[a("li",null,"expr: 常量/变量/连接运算符{ }/x/z"),a("li",null,"casex: 当输入某一位为 x/z 时,忽略此位匹配(恒将此位匹配为真)"),a("li",null,"casez: 当输入某一位为 z 时,忽略此位匹配(恒将此位匹配为真)")],-1),ls=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token punctuation"},"("),e("expr"),a("span",{class:"token punctuation"},")"),e("\n item "),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n item "),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},":"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n item "),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"default"),a("span",{class:"token punctuation"},":"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endcase"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),cs=a("h3",{id:"for"},[a("a",{class:"header-anchor",href:"#for","aria-hidden":"true"},"#"),e(" for")],-1),ps=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("循环初值"),a("span",{class:"token punctuation"},";"),e(" 循环条件"),a("span",{class:"token punctuation"},";"),e(" 控制部分"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),us=a("h3",{id:"repeat-loop"},[a("a",{class:"header-anchor",href:"#repeat-loop","aria-hidden":"true"},"#"),e(" repeat loop")],-1),rs=a("ul",null,[a("li",null,[e("initial for "),a("strong",null,"test bench")]),a("li",null,[e("当需 if/else 进行断言时,注意 "),a("strong",null,"延时"),e(" 造成的错误逻辑")])],-1),is=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 重复事件控制:"),e("\n"),a("span",{class:"token comment"},"// 先计算好右值, 等待时钟 clk 上出现2个负跳变沿, 再把右值赋给 result"),e("\nresult "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"repeat"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(" @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"negedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(" hw_data "),a("span",{class:"token operator"},"+"),e(" hr_data"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"// repeat 循环语句:"),e("\n"),a("span",{class:"token keyword"},"repeat"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(" result "),a("span",{class:"token operator"},"="),e(" hw_data "),a("span",{class:"token operator"},"+"),e(" hr_data"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),ks=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n inc_DAC "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e("’b1"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token function"},"repeat"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4095"),a("span",{class:"token punctuation"},")"),e(" @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// bring DAC right up to point of rollover"),e("\n inc_DAC "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e("’b0"),a("span",{class:"token punctuation"},";"),e("\n inc_sym "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e("’b1"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token function"},"repeat"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},")"),e("@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// bring sample count up to 7"),e("\n inc_sym "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e("’b0"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token number"},"#100"),e(),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// run simulation for 100 units"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),bs=a("h3",{id:"forever-loop"},[a("a",{class:"header-anchor",href:"#forever-loop","aria-hidden":"true"},"#"),e(" forever loop")],-1),ms=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// $stop, $finish 可以终止 forever loop"),e("\n"),a("span",{class:"token keyword"},"forever"),e(),a("span",{class:"token number"},"#10"),e(" clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"~"),e(" clk"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ds=a("h3",{id:"force-and-release"},[a("a",{class:"header-anchor",href:"#force-and-release","aria-hidden":"true"},"#"),e(" Force and Release")],-1),gs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"initial"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"force"),e(" test_reset "),a("span",{class:"token operator"},"="),e(" penalty "),a("span",{class:"token operator"},"&"),e(" rtc_intr"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"#5"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"release"),e(" test_reset"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),ws=a("h3",{id:"blocking-and-non-blocking"},[a("a",{class:"header-anchor",href:"#blocking-and-non-blocking","aria-hidden":"true"},"#"),e(" Blocking and Non-Blocking")],-1),ys=a("ul",null,[a("li",null,"Blocking(=): 顺序执行"),a("li",null,"Non-Blocking(<=): 并行执行")],-1),hs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token operator"},"="),e(" input_logic"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token operator"},"<="),e(" input_logic"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),fs=a("h3",{id:"disable"},[a("a",{class:"header-anchor",href:"#disable","aria-hidden":"true"},"#"),e(" disable")],-1),vs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"begin"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"break"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<"),e(" n"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"="),e(" i"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"continue"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("a "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},'// "continue" loop'),e("\n "),a("span",{class:"token keyword"},"disable"),e(),a("span",{class:"token keyword"},"continue"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("a "),a("span",{class:"token operator"},"=="),e(" b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},'// "break" from loop'),e("\n "),a("span",{class:"token keyword"},"disable"),e(),a("span",{class:"token keyword"},"break"),a("span",{class:"token punctuation"},";"),e("\n statement1\n statement2\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),_s=a("h2",{id:"结构建模"},[a("a",{class:"header-anchor",href:"#结构建模","aria-hidden":"true"},"#"),e(" 结构建模")],-1),As=a("h3",{id:"generate-语句"},[a("a",{class:"header-anchor",href:"#generate-语句","aria-hidden":"true"},"#"),e(" generate 语句")],-1),Ds=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"generate"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("gv_i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" gv_i "),a("span",{class:"token operator"},"<"),e(" SIZE"),a("span",{class:"token punctuation"},";"),e(" gv_i "),a("span",{class:"token operator"},"="),e(" gv_i "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),a("span",{class:"token punctuation"},":"),e(" blk\n "),a("span",{class:"token keyword"},"xor"),e(" uxor "),a("span",{class:"token punctuation"},"("),e("y"),a("span",{class:"token punctuation"},"["),e("gv_i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(" a"),a("span",{class:"token punctuation"},"["),e("gv_i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},"["),e("gv_i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"endgenerate"),e("\n"),a("span",{class:"token comment"},"// =>"),e("\n"),a("span",{class:"token comment"},"// module.blk[0].uxor"),e("\n"),a("span",{class:"token comment"},"// module.blk[1].uxor"),e("\n"),a("span",{class:"token comment"},"// module.blk[2].uxor"),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Ts=a("h2",{id:"delay-时延"},[a("a",{class:"header-anchor",href:"#delay-时延","aria-hidden":"true"},"#"),e(" Delay(时延)")],-1),xs=a("ul",null,[a("li",null,"语句内时延"),a("li",null,"语句间时延"),a("li",null,"语句内时延期间:右值保持稳定不变,才可成功赋给左值")],-1),Is=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("sum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("a "),a("span",{class:"token operator"},"^"),e(" b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"^"),e(" cin"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token number"},"#4"),e(" t1 "),a("span",{class:"token operator"},"="),e(" a "),a("span",{class:"token operator"},"&"),e(" cin"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Bs=a("h2",{id:"预编译指令"},[a("a",{class:"header-anchor",href:"#预编译指令","aria-hidden":"true"},"#"),e(" 预编译指令")],-1),Ws=a("h3",{id:"define-宏"},[a("a",{class:"header-anchor",href:"#define-宏","aria-hidden":"true"},"#"),e(" define 宏")],-1),Cs=a("p",null,"将多个 define 宏,放至 _defines.v, 作为全局宏",-1),Hs=a("h2",{id:"data-path"},[a("a",{class:"header-anchor",href:"#data-path","aria-hidden":"true"},"#"),e(" Data Path")],-1),Ns=a("h3",{id:"multiplexer"},[a("a",{class:"header-anchor",href:"#multiplexer","aria-hidden":"true"},"#"),e(" Multiplexer")],-1),Os=a("h3",{id:"adder"},[a("a",{class:"header-anchor",href:"#adder","aria-hidden":"true"},"#"),e(" Adder")],-1),zs=a("h3",{id:"register-data-path"},[a("a",{class:"header-anchor",href:"#register-data-path","aria-hidden":"true"},"#"),e(" Register Data Path")],-1),Ss=a("h3",{id:"memory"},[a("a",{class:"header-anchor",href:"#memory","aria-hidden":"true"},"#"),e(" Memory")],-1),Ps=a("ul",null,[a("li",null,"其中数据文件中地址必须在系统任务中定义的范围内,系统任务中定义的地址必须在存储器定义的地址范围内"),a("li",null,"优先考虑数据文件中的地址>系统任务中定义的起始地址和结束地址>存储器定义的起始地址和结束地址")],-1),Us=a("h2",{id:"demos"},[a("a",{class:"header-anchor",href:"#demos","aria-hidden":"true"},"#"),e(" Demos")],-1),Ms=a("ul",null,[a("li",null,[a("a",{href:"https://hom-wang.gitbooks.io/verilog-hdl/content/Chapter_07.html",target:"_blank",rel:"noopener noreferrer"},"GitBook")]),a("li",null,[a("a",{href:"http://www.xilinx.com/support/university/ise/ise-teaching-material/hdl-design.html",target:"_blank",rel:"noopener noreferrer"},"Xilinx Lab")])],-1),Es=a("h3",{id:"binary-multiplier"},[a("a",{class:"header-anchor",href:"#binary-multiplier","aria-hidden":"true"},"#"),e(" Binary Multiplier")],-1),Ls=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e(" "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),e("the multiplicand"),a("span",{class:"token punctuation"},")"),e("\nx "),a("span",{class:"token number"},"1011"),e(),a("span",{class:"token punctuation"},"("),e("the multiplier"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"0000"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"initial"),e(" partial product"),a("span",{class:"token punctuation"},","),e(" start "),a("span",{class:"token keyword"},"with"),e(),a("span",{class:"token number"},"0000"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),e("st multiplier "),a("span",{class:"token keyword"},"bit"),e(" is "),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" so add the multiplicand"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),e("sum"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"01100"),e(),a("span",{class:"token punctuation"},"("),e("shift sum one position to the right"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),e("nd multiplier "),a("span",{class:"token keyword"},"bit"),e(" is "),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" so add multiplicand again"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"100100"),e(),a("span",{class:"token punctuation"},"("),e("sum"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"with"),e(" a carry generated on the left"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"100100"),e(),a("span",{class:"token punctuation"},"("),e("shift sum once to the right"),a("span",{class:"token punctuation"},","),e(" including carry"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"0100100"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),e("rd multiplier "),a("span",{class:"token keyword"},"bit"),e(" is "),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),e(" so skip add"),a("span",{class:"token punctuation"},","),e(" shift once"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),e("th multiplier "),a("span",{class:"token keyword"},"bit"),e(" is "),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" so add multiplicand again"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"10000100"),e(),a("span",{class:"token punctuation"},"("),e("sum"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"with"),e(" a carry generated on the left"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"10000100"),e(),a("span",{class:"token punctuation"},"("),e("shift sum once to the right"),a("span",{class:"token punctuation"},","),e(" including carry"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),Rs=a("h3",{id:"multi-hz"},[a("a",{class:"header-anchor",href:"#multi-hz","aria-hidden":"true"},"#"),e(" Multi-Hz")],-1),Fs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/*\n * 利用计数器实现任意分频\n */"),e("\n"),a("span",{class:"token important"},"always"),e(" @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" f_clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n\n "),a("span",{class:"token comment"},"//设定频率控制字p "),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"=="),e(" p"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n i"),a("span",{class:"token operator"},"="),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n f_out"),a("span",{class:"token operator"},"=~"),e("f_out"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n i"),a("span",{class:"token operator"},"="),e("i"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),$s=a("h2",{id:"tips"},[a("a",{class:"header-anchor",href:"#tips","aria-hidden":"true"},"#"),e(" Tips")],-1),qs=a("h3",{id:"不可综合结构"},[a("a",{class:"header-anchor",href:"#不可综合结构","aria-hidden":"true"},"#"),e(" 不可综合结构")],-1),Vs=a("ul",null,[a("li",null,"initial: 只用于 test bench"),a("li",null,"events: Events 同步测试各个组件"),a("li",null,"real: Real 数据类型不可综合"),a("li",null,"time: Time 数据类型不可综合"),a("li",null,"force/release"),a("li",null,"assign(reg)/deassign(reg)"),a("li",null,"fork join"),a("li",null,"primitive: 只有门级的原语(primitives)可综合"),a("li",null,"table: 用户自定义原语(UDP)及 table 不可综合"),a("li",null,[a("code",null,"#1"),e(" 延迟只用于仿真,综合器直接忽略延迟")])],-1),Gs=a("h3",{id:"混合编程"},[a("a",{class:"header-anchor",href:"#混合编程","aria-hidden":"true"},"#"),e(" 混合编程")],-1),Xs=a("ul",null,[a("li",null,"内部变量用 assign 赋值"),a("li",null,"输出变量通过监听 内部变量 改变输出值")],-1),Ks=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e(" "),a("span",{class:"token keyword"},"assign"),e(" DT0 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" DT1 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("DT0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n AOut "),a("span",{class:"token operator"},"<="),e(" DT0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("DT1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n BOut "),a("span",{class:"token operator"},"<="),e(" DT1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),js=a("h3",{id:"上升沿-下降沿"},[a("a",{class:"header-anchor",href:"#上升沿-下降沿","aria-hidden":"true"},"#"),e(" 上升沿/下降沿")],-1),Zs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e(" "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" A "),a("span",{class:"token keyword"},"or"),e(),a("span",{class:"token keyword"},"negedge"),e(" B"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),e("B"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Ys=a("h3",{id:"parameter"},[a("a",{class:"header-anchor",href:"#parameter","aria-hidden":"true"},"#"),e(" Parameter")],-1),Qs=a("ul",null,[a("li",null,"只在定义的模块内部起作用")],-1),Js=a("h4",{id:"overload-method"},[a("a",{class:"header-anchor",href:"#overload-method","aria-hidden":"true"},"#"),e(" Overload Method")],-1),na=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" data_path\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" A"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" B"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" C\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),sa=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" data_path_tb\n"),a("span",{class:"token punctuation"},"("),e("\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n data_path #"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"DATA_WIDTH"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(" DUT "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"A"),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"B"),a("span",{class:"token punctuation"},"("),e("B"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"C"),a("span",{class:"token punctuation"},"("),e("C"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),aa=a("h4",{id:"constant-variable"},[a("a",{class:"header-anchor",href:"#constant-variable","aria-hidden":"true"},"#"),e(" Constant Variable")],-1),ea=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("reset_value "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{{"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'b0"),a("span",{class:"token operator"},"}}"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"{"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'b1"),a("span",{class:"token operator"},"}}}"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),ta=a("h4",{id:"test-bench"},[a("a",{class:"header-anchor",href:"#test-bench","aria-hidden":"true"},"#"),e(" Test Bench")],-1),oa=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token important"},"always "),a("span",{class:"token keyword"},"begin"),e("\n clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"forever"),e(" #DELAY clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"~"),e("clk"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),la=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"reg"),e(" clock"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"integer"),e(" no_of_clocks"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"parameter"),e(" CLOCK_PERIOD "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"parameter"),e(" TIME "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"50000"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"initial"),e(" no_of_clocks "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"initial"),e(" clock "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1'b0"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token important"},"always "),e("#"),a("span",{class:"token punctuation"},"("),e("CLOCK_PERIOD"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2.0"),a("span",{class:"token punctuation"},")"),e(" clock "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"~"),e("clock"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clock"),a("span",{class:"token punctuation"},")"),e("\n no_of_clocks "),a("span",{class:"token operator"},"="),e(" no_of_clocks "),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n #TIME"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),e('"End of simulation '),a("span",{class:"token keyword"},"time"),e(" is "),a("span",{class:"token operator"},"%"),e("d "),a("span",{class:"token punctuation"},","),e("\n total number of clocks seen is "),a("span",{class:"token operator"},"%"),e("d expected is "),a("span",{class:"token operator"},"%"),e('d"'),a("span",{class:"token punctuation"},","),a("span",{class:"token property"},"$time"),a("span",{class:"token punctuation"},","),e("no_of_clocks"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token property"},"$time"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),ca=a("h2",{id:"有限状态机-fsm"},[a("a",{class:"header-anchor",href:"#有限状态机-fsm","aria-hidden":"true"},"#"),e(" 有限状态机(FSM)")],-1),pa=a("ul",null,[a("li",null,"reset: initial state"),a("li",null,"default: illegal/unreachable state")],-1),ua=a("h2",{id:"算术状态机-asm"},[a("a",{class:"header-anchor",href:"#算术状态机-asm","aria-hidden":"true"},"#"),e(" 算术状态机(ASM)")],-1),ra=a("ul",null,[a("li",null,"state box: moore fsm"),a("li",null,"conditional box: mealy fsm"),a("li",null,[e("decision box: "),a("code",null,"x_input"),e(" = 0/1")])],-1),ia=a("h2",{id:"system-verilog"},[a("a",{class:"header-anchor",href:"#system-verilog","aria-hidden":"true"},"#"),e(" System Verilog")],-1),ka=a("h3",{id:"enum"},[a("a",{class:"header-anchor",href:"#enum","aria-hidden":"true"},"#"),e(" Enum")],-1),ba=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"typedef"),e(),a("span",{class:"token keyword"},"enum"),e(),a("span",{class:"token keyword"},"logic"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"{"),e("\n RED"),a("span",{class:"token punctuation"},","),e(" GREEN"),a("span",{class:"token punctuation"},","),e(" BLUE"),a("span",{class:"token punctuation"},","),e(" CYAN"),a("span",{class:"token punctuation"},","),e(" MAGENTA"),a("span",{class:"token punctuation"},","),e(" YELLOW\n"),a("span",{class:"token operator"},"}"),e(" color_t"),a("span",{class:"token punctuation"},";"),e("\n\ncolor_t my_color "),a("span",{class:"token operator"},"="),e(" GREEN"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"The color is %s"'),a("span",{class:"token punctuation"},","),e(" my_color"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"name"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),ma=a("h3",{id:"struct-and-union"},[a("a",{class:"header-anchor",href:"#struct-and-union","aria-hidden":"true"},"#"),e(" Struct and Union")],-1),da=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"typedef"),e(),a("span",{class:"token keyword"},"struct"),e(),a("span",{class:"token keyword"},"packed"),e(),a("span",{class:"token operator"},"{"),e("\n "),a("span",{class:"token keyword"},"bit"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" expo"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"bit"),e(" sign"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"bit"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"51"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" man"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token operator"},"}"),e(" FP"),a("span",{class:"token punctuation"},";"),e("\n\nFP zero "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"64'b0"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),ga=a("h3",{id:"procedural-block"},[a("a",{class:"header-anchor",href:"#procedural-block","aria-hidden":"true"},"#"),e(" Procedural Block")],-1),wa=a("ul",null,[a("li",null,"always_comb: 用于组合逻辑电路(相当于 Verilog 中对所有输入变量电平敏感的 always,但 always_comb 无需手动列出所有输入变量,系统会自动识别)"),a("li",null,"always_ff: 用于触发器及相关的时序逻辑电路(相当于 Verilog 中对某个或某几个信号有效跳变沿敏感、并带有信号储存特性的 always)"),a("li",null,"always_latch: 用于锁存器级相关的时序逻辑电路(相当于 Verilog 中对某个或某几个信号电平敏感、并带有信号储存特性的的 always)")],-1),ya=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token important"},"always_comb "),a("span",{class:"token keyword"},"begin"),e("\n tmp "),a("span",{class:"token operator"},"="),e(" b "),a("span",{class:"token operator"},"*"),e(" b "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"*"),e(" a "),a("span",{class:"token operator"},"*"),e(" c"),a("span",{class:"token punctuation"},";"),e("\n no_root "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("tmp "),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token important"},"always_ff @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n\n\n"),a("span",{class:"token important"},"always_latch"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("en"),a("span",{class:"token punctuation"},")"),e(" q "),a("span",{class:"token operator"},"<="),e(" d"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),ha=a("h3",{id:"interface"},[a("a",{class:"header-anchor",href:"#interface","aria-hidden":"true"},"#"),e(" Interface")],-1),fa=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"interface"),e(" interfaceName"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"logic"),e(" a"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"logic"),e(" b"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"modport"),e(" in "),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"input"),e(" a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"output"),e(" b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"modport"),e(" out "),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"input"),e(" b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"output"),e(" a"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endinterface"),e("\n\n"),a("span",{class:"token keyword"},"module"),e(" top"),a("span",{class:"token punctuation"},";"),e("\n interfaceName i "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n u_a m1 "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"i1"),a("span",{class:"token punctuation"},"("),e("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n u_b m2 "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"i2"),a("span",{class:"token punctuation"},"("),e("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n\n"),a("span",{class:"token keyword"},"module"),e(" u_a "),a("span",{class:"token punctuation"},"("),e("interfaceName"),a("span",{class:"token punctuation"},"."),e("in i1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n\n"),a("span",{class:"token keyword"},"module"),e(" u_b "),a("span",{class:"token punctuation"},"("),e("interfaceName"),a("span",{class:"token punctuation"},"."),e("out i2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br")])],-1),va=a("h3",{id:"testing"},[a("a",{class:"header-anchor",href:"#testing","aria-hidden":"true"},"#"),e(" Testing")],-1),_a=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" top"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"integer"),e(" num_packets "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$random"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},","),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset_n"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" D"),a("span",{class:"token punctuation"},";"),e("\n register_logic "),a("span",{class:"token function"},"dut"),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},","),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset_n"),a("span",{class:"token punctuation"},","),e(" D"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// generate clock"),e("\n "),a("span",{class:"token comment"},"// ..."),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n reset_n "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"#20"),e(" reset_n "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(" reset_n "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"#1"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"repeat"),e(),a("span",{class:"token punctuation"},"("),e("num_packets"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n A "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$random"),a("span",{class:"token punctuation"},";"),e(" B "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$random"),a("span",{class:"token punctuation"},";"),e(" C "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$random"),a("span",{class:"token punctuation"},";"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},","),e(" D"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br")])],-1),Aa=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(" Packet"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"string"),e(" name"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"rand"),e(),a("span",{class:"token keyword"},"bit"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" sa"),a("span",{class:"token punctuation"},","),e(" da"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"rand"),e(),a("span",{class:"token keyword"},"reg"),e(" A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"display"),a("span",{class:"token punctuation"},"("),e("result"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},","),e(" result"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n"),a("span",{class:"token keyword"},"endclass"),a("span",{class:"token punctuation"},":"),e(" Packet\n\n"),a("span",{class:"token comment"},"// inheritance"),e("\n"),a("span",{class:"token keyword"},"class"),e(" Packet_da_3 "),a("span",{class:"token keyword"},"extends"),e(" Packet"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"constraint"),e(" da_3 "),a("span",{class:"token operator"},"{"),e("\n da "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token operator"},"}"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"display"),a("span",{class:"token punctuation"},"("),e("result"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"super"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"display"),a("span",{class:"token punctuation"},"("),e("result"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),e("sa"),a("span",{class:"token punctuation"},","),e(" da"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n"),a("span",{class:"token keyword"},"endclass"),a("span",{class:"token punctuation"},":"),e(" Packet_da_3\n\n"),a("span",{class:"token keyword"},"class"),e(" Generator"),a("span",{class:"token punctuation"},";"),e("\n Packet pkt"),a("span",{class:"token punctuation"},";"),e("\n Channel out_chan"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" num_packets"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"gen"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n pkt "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),e("\n pkt"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"randomize"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n out_chan"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),e("pkt"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"while"),e(),a("span",{class:"token punctuation"},"("),e("num_packets"),a("span",{class:"token operator"},"--"),e(),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"gen"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endclass"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(" Driver"),a("span",{class:"token punctuation"},";"),e("\n Channel in_chan"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"send"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n in_chan"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),e("pkt"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n top"),a("span",{class:"token punctuation"},"."),e("A "),a("span",{class:"token operator"},"="),e(" pkt"),a("span",{class:"token punctuation"},"."),e("A"),a("span",{class:"token punctuation"},";"),e("\n top"),a("span",{class:"token punctuation"},"."),e("B "),a("span",{class:"token operator"},"="),e(" pkt"),a("span",{class:"token punctuation"},"."),e("B"),a("span",{class:"token punctuation"},";"),e("\n top"),a("span",{class:"token punctuation"},"."),e("C "),a("span",{class:"token operator"},"="),e(" pkt"),a("span",{class:"token punctuation"},"."),e("C"),a("span",{class:"token punctuation"},";"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" top"),a("span",{class:"token punctuation"},"."),e("clk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"forever"),e(),a("span",{class:"token function"},"send"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endclass"),e("\n\n"),a("span",{class:"token keyword"},"module"),e(" top"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token function"},"build"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"build"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n Config cfg "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n Channel chan "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n Generator gen "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n Driver drv "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n gen"),a("span",{class:"token punctuation"},"."),e("out_chan "),a("span",{class:"token operator"},"="),e(" chan"),a("span",{class:"token punctuation"},";"),e("\n drv"),a("span",{class:"token punctuation"},"."),e("in_chan "),a("span",{class:"token operator"},"="),e(" chan"),a("span",{class:"token punctuation"},";"),e("\n\n cfg"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"randomize"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"with"),e(),a("span",{class:"token operator"},"{"),e(" num_packets "),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"1500"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token operator"},"}"),e("\n gen"),a("span",{class:"token punctuation"},"."),e("num_packets "),a("span",{class:"token operator"},"="),e(" cfg"),a("span",{class:"token punctuation"},"."),e("num_packets"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"fork"),e("\n gen"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n drv"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"join"),e("\n "),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br")])],-1),Da=a("h2",{id:"u280-platform"},[a("a",{class:"header-anchor",href:"#u280-platform","aria-hidden":"true"},"#"),e(" U280 Platform")],-1),Ta=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[e("-xp param "),a("span",{class:"token punctuation"},"("),e("clock frequency etc."),a("span",{class:"token punctuation"},")"),e("\n-R report level\n-slr SLR region setting\n-sp memory resources mapping\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),xa=a("p",null,"tools:",-1),Ia=a("ul",null,[a("li",null,"xbutil query"),a("li",null,"platforminfo"),a("li",null,"kernelinfo"),a("li",null,[a("strong",null,"xclbinutil")]),a("li",null,"dmesg")],-1),Ba=a("h3",{id:"host-application"},[a("a",{class:"header-anchor",href:"#host-application","aria-hidden":"true"},"#"),e(" Host Application")],-1),Wa=a("h4",{id:"basic-flow"},[a("a",{class:"header-anchor",href:"#basic-flow","aria-hidden":"true"},"#"),e(" Basic Flow")],-1),Ca=a("ul",null,[a("li",null,"set the kernel arguments before performing any enqueue operation"),a("li",null,[e("keeping the buffer size "),a("strong",null,"2 MB ~ 4 GB")]),a("li",null,[a("code",null,"posix_memalign"),e(" is used instead of malloc for the host memory space pointer")])],-1),Ha=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"uint32_t"),e(),a("span",{class:"token operator"},"*"),e("a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"*"),e("b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"*"),e("c"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"*"),e("d "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"posix_memalign"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"4096"),a("span",{class:"token punctuation"},","),e(" BUF_SIZE "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"uint32_t"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"posix_memalign"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"4096"),a("span",{class:"token punctuation"},","),e(" BUF_SIZE "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"uint32_t"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"posix_memalign"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("c"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"4096"),a("span",{class:"token punctuation"},","),e(" BUF_SIZE "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"uint32_t"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"posix_memalign"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("d"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"4096"),a("span",{class:"token punctuation"},","),e(" BUF_SIZE "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"uint32_t"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Na=a("ul",null,[a("li",null,"release resources for proper performance profile report")],-1),Oa=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token function"},"clReleaseCommandQueue"),a("span",{class:"token punctuation"},"("),e("Command_Queue"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"clReleaseContext"),a("span",{class:"token punctuation"},"("),e("Context"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"clReleaseDevice"),a("span",{class:"token punctuation"},"("),e("Target_Device_ID"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"clReleaseKernel"),a("span",{class:"token punctuation"},"("),e("Kernel"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"clReleaseProgram"),a("span",{class:"token punctuation"},"("),e("Program"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"free"),a("span",{class:"token punctuation"},"("),e("Platform_IDs"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"free"),a("span",{class:"token punctuation"},"("),e("Device_IDs"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),za=a("h4",{id:"tlp"},[a("a",{class:"header-anchor",href:"#tlp","aria-hidden":"true"},"#"),e(" TLP")],-1),Sa=a("p",null,[e("It is advisable to use the "),a("code",null,"posix_spawn()"),e(" system call to launch another process from the SDAccel environment application.")],-1),Pa=a("h3",{id:"u280-tools"},[a("a",{class:"header-anchor",href:"#u280-tools","aria-hidden":"true"},"#"),e(" U280 Tools")],-1),Ua=a("h4",{id:"gdb-based-debugging"},[a("a",{class:"header-anchor",href:"#gdb-based-debugging","aria-hidden":"true"},"#"),e(" GDB Based Debugging")],-1),Ma=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[e("xprint queue "),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"<"),e("cl_command_queue"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},"]"),e("\nxprint event "),a("span",{class:"token operator"},"<"),e("cl_event"),a("span",{class:"token operator"},">"),e("\nxprint mem "),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"<"),e("cl_mem"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},"]"),e("\nxprint kernel\nxprint all\nxstatus all\nxstatus --"),a("span",{class:"token operator"},"<"),e("ipName"),a("span",{class:"token operator"},">"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Ea=a("h4",{id:"xcl-binary-util"},[a("a",{class:"header-anchor",href:"#xcl-binary-util","aria-hidden":"true"},"#"),e(" XCL Binary Util")],-1),La=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,"xclbinutil -i binary_container_1.xclbin --info\n")]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Ra=a("h4",{id:"xocc"},[a("a",{class:"header-anchor",href:"#xocc","aria-hidden":"true"},"#"),e(" XOCC")],-1),Fa=a("p",null,"Checking out-of-bound access made by kernel interface buffers (option: address) and uninitialized memory access initiated by kernel local to kernel (option: memory).",-1),$a=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[e("xocc -l –t sw_emu --xp param:compiler.fsanitize"),a("span",{class:"token operator"},"="),e("address -o bin_kernel.xclbin\nxocc -l –t sw_emu --xp param:compiler.fsanitize"),a("span",{class:"token operator"},"="),e("memory -o bin_kernel.xclbin\nxocc -l –t sw_emu --xp param:compiler.fsanitize"),a("span",{class:"token operator"},"="),e("address,memory -o bin_kernel.xclbin\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),qa=a("h4",{id:"xbutil"},[a("a",{class:"header-anchor",href:"#xbutil","aria-hidden":"true"},"#"),e(" XBUtil")],-1),Va=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token function"},"sudo"),e(" /opt/xilinx/xrt/bin/xbutil flash -a "),a("span",{class:"token operator"},"<"),e("shell_name"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token comment"},"# flash the firmware"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Ga=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token function"},"sudo"),e(" lspci -vd 10ee:\n"),a("span",{class:"token function"},"sudo"),e(" /opt/xilinx/xrt/bin/xbutil flash scan\n"),a("span",{class:"token function"},"sudo"),e(" /opt/xilinx/xrt/bin/xbutil validate -d "),a("span",{class:"token operator"},"<"),e("card_id"),a("span",{class:"token operator"},">"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Xa=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[e("xbutil program -p "),a("span",{class:"token operator"},"<"),e("xclbin"),a("span",{class:"token operator"},">"),e("\nxbutil query "),a("span",{class:"token comment"},"# check memory banks usage"),e("\nxbutil status --lapc "),a("span",{class:"token comment"},"# check AXI violations"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Ka=a("h4",{id:"dmesg"},[a("a",{class:"header-anchor",href:"#dmesg","aria-hidden":"true"},"#"),e(" dmesg")],-1),ja=a("h4",{id:"ila-trigger"},[a("a",{class:"header-anchor",href:"#ila-trigger","aria-hidden":"true"},"#"),e(" ILA Trigger")],-1),Za=a("ul",null,[a("li",null,"debug protocol hangs"),a("li",null,"examine the burst size, pipelining and data width to locate the bottleneck")],-1),Ya=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\nstd"),a("span",{class:"token double-colon punctuation"},"::"),e("string binaryFile "),a("span",{class:"token operator"},"="),e(" xcl"),a("span",{class:"token double-colon punctuation"},"::"),a("span",{class:"token function"},"find_binary_file"),a("span",{class:"token punctuation"},"("),e("device_name"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"vAdd"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\ncl"),a("span",{class:"token double-colon punctuation"},"::"),e("Program"),a("span",{class:"token double-colon punctuation"},"::"),e("Binaries bins "),a("span",{class:"token operator"},"="),e(" xcl"),a("span",{class:"token double-colon punctuation"},"::"),a("span",{class:"token function"},"import_binary_file"),a("span",{class:"token punctuation"},"("),e("binaryFile"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\ndevices"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"resize"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\ncl"),a("span",{class:"token double-colon punctuation"},"::"),e("Program "),a("span",{class:"token function"},"program"),a("span",{class:"token punctuation"},"("),e("context"),a("span",{class:"token punctuation"},","),e(" devices"),a("span",{class:"token punctuation"},","),e(" bins"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\ncl"),a("span",{class:"token double-colon punctuation"},"::"),e("Kernel "),a("span",{class:"token function"},"kernel_vAdd"),a("span",{class:"token punctuation"},"("),e("program"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"kernel_vAdd_rtl"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},'// wait_for_enter("\\nPress ENTER to continue after setting up ILA trigger...");'),e("\nstd"),a("span",{class:"token double-colon punctuation"},"::"),e("cout "),a("span",{class:"token operator"},"<<"),e(),a("span",{class:"token string"},'"Pausing to arm ILA trigger. Hit enter here to resume host program..."'),e("\n "),a("span",{class:"token operator"},"<<"),e(" std"),a("span",{class:"token double-colon punctuation"},"::"),e("endl"),a("span",{class:"token punctuation"},";"),e("\nstd"),a("span",{class:"token double-colon punctuation"},"::"),e("cin"),a("span",{class:"token double-colon punctuation"},"::"),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"//Allocate Buffer in Global Memory"),e("\nstd"),a("span",{class:"token double-colon punctuation"},"::"),e("vector"),a("span",{class:"token operator"},"<"),e("cl"),a("span",{class:"token double-colon punctuation"},"::"),e("Memory"),a("span",{class:"token operator"},">"),e(" inBufVec"),a("span",{class:"token punctuation"},","),e(" outBufVec"),a("span",{class:"token punctuation"},";"),e("\ncl"),a("span",{class:"token double-colon punctuation"},"::"),e("Buffer "),a("span",{class:"token function"},"buffer_r1"),a("span",{class:"token punctuation"},"("),e("context"),a("span",{class:"token punctuation"},","),e("CL_MEM_USE_HOST_PTR "),a("span",{class:"token operator"},"|"),e(" CL_MEM_READ_ONLY"),a("span",{class:"token punctuation"},","),e("\nvector_size_bytes"),a("span",{class:"token punctuation"},","),e(" source_input1"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n\n"),a("span",{class:"token comment"},"//Copy input data to device global memory"),e("\nq"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"enqueueMigrateMemObjects"),a("span",{class:"token punctuation"},"("),e("inBufVec"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token comment"},"/* 0 means from host*/"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"//Set the Kernel Arguments"),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n\n"),a("span",{class:"token comment"},"//Launch the Kernel"),e("\nq"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"enqueueTask"),a("span",{class:"token punctuation"},"("),e("kernel_vAdd"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br")])],-1),Qa=a("h2",{id:"axi-protocol"},[a("a",{class:"header-anchor",href:"#axi-protocol","aria-hidden":"true"},"#"),e(" AXI Protocol")],-1),Ja=a("p",null,"Advanced eXtensible Interface Protocol:",-1),ne=a("ul",null,[a("li",null,"handshake protocol: ready-valid protocol")],-1),se=a("h3",{id:"axi-channels"},[a("a",{class:"header-anchor",href:"#axi-channels","aria-hidden":"true"},"#"),e(" AXI Channels")],-1),ae=a("h4",{id:"read-address-channel"},[a("a",{class:"header-anchor",href:"#read-address-channel","aria-hidden":"true"},"#"),e(" Read Address Channel")],-1),ee=a("ul",null,[a("li",null,"arburst: burst type"),a("li",null,"araddr: start address"),a("li",null,"arlen: (# of transfers) - 1"),a("li",null,"arsize: bytes/transfer"),a("li",null,"arready (memory to host)"),a("li",null,"arvalid")],-1),te=a("h4",{id:"read-data-channel"},[a("a",{class:"header-anchor",href:"#read-data-channel","aria-hidden":"true"},"#"),e(" Read Data Channel")],-1),oe=a("ul",null,[a("li",null,"rdata: data"),a("li",null,"rresp: response (failure check)"),a("li",null,"rlast: flag for last piece of data"),a("li",null,"rready (host to memory)"),a("li",null,"rvalid")],-1),le=a("h4",{id:"write-address-channel"},[a("a",{class:"header-anchor",href:"#write-address-channel","aria-hidden":"true"},"#"),e(" Write Address Channel")],-1),ce=a("ul",null,[a("li",null,"awburst: burst type"),a("li",null,"awaddr: start address"),a("li",null,"awlen: (# of transfers) - 1"),a("li",null,"awsize: bytes/transfer"),a("li",null,"awready (memory to host)"),a("li",null,"awvalid")],-1),pe=a("h4",{id:"write-data-channel"},[a("a",{class:"header-anchor",href:"#write-data-channel","aria-hidden":"true"},"#"),e(" Write Data Channel")],-1),ue=a("ul",null,[a("li",null,"wdata: data"),a("li",null,"wstrb: write strobe -> write mask (1 bit mask for 1 byte data)"),a("li",null,"wlast: flag for last piece of data"),a("li",null,"wready (memory to host)"),a("li",null,"wvalid")],-1),re=a("h4",{id:"write-response-channel"},[a("a",{class:"header-anchor",href:"#write-response-channel","aria-hidden":"true"},"#"),e(" Write Response Channel")],-1),ie=a("ul",null,[a("li",null,"bresp: response (failure check)"),a("li",null,"bready (host to memory)"),a("li",null,"bvalid")],-1),ke=a("h3",{id:"axi-burst"},[a("a",{class:"header-anchor",href:"#axi-burst","aria-hidden":"true"},"#"),e(" AXI Burst")],-1),be=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"right"}},"AxBURST[1:0]"),a("th",{style:{"text-align":"left"}},"Burst Type")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"right"}},"0b00"),a("td",{style:{"text-align":"left"}},"FIXED")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b01"),a("td",{style:{"text-align":"left"}},"INCR")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b10"),a("td",{style:{"text-align":"left"}},"WRAP")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b11"),a("td",{style:{"text-align":"left"}},"Reserved")])])],-1),me=a("p",null,"burst length = AxLEN[7:0] + 1 (up to 256 transfers in each burst)",-1),de=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"right"}},"AxSIZE[2:0]"),a("th",{style:{"text-align":"right"}},"Bytes in Transfer")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"right"}},"0b000"),a("td",{style:{"text-align":"right"}},"1")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b001"),a("td",{style:{"text-align":"right"}},"2")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b010"),a("td",{style:{"text-align":"right"}},"4")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b011"),a("td",{style:{"text-align":"right"}},"8")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b100"),a("td",{style:{"text-align":"right"}},"16")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b101"),a("td",{style:{"text-align":"right"}},"32")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b110"),a("td",{style:{"text-align":"right"}},"64")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b111"),a("td",{style:{"text-align":"right"}},"128")])])],-1),ge=a("h4",{id:"read-burst"},[a("a",{class:"header-anchor",href:"#read-burst","aria-hidden":"true"},"#"),e(" Read Burst")],-1),we=a("p",null,[a("img",{src:"/awesome-notes/assets/AXI_Read_Burst.0e9ed250.png",alt:"Read Burst Example"})],-1),ye=a("h4",{id:"write-burst"},[a("a",{class:"header-anchor",href:"#write-burst","aria-hidden":"true"},"#"),e(" Write Burst")],-1),he=a("p",null,[a("img",{src:"/awesome-notes/assets/AXI_Write_Burst.63cb974a.png",alt:"Write Burst Example"})],-1),fe=a("h2",{id:"verilog-components"},[a("a",{class:"header-anchor",href:"#verilog-components","aria-hidden":"true"},"#"),e(" Verilog Components")],-1),ve=a("h3",{id:"clock-unit"},[a("a",{class:"header-anchor",href:"#clock-unit","aria-hidden":"true"},"#"),e(" Clock Unit")],-1),_e=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module tick_divider\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief simple clock divider\n * @param DATA_WIDTH data width\n * @input clk_src clock signal\n * @output clk_group divided clock signals\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" tick_divider\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk_src"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" clk_group\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n clk_group "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token operator"},"{"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'b0"),a("span",{class:"token operator"},"}}"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk_src"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n clk_group "),a("span",{class:"token operator"},"<="),e(" clk_group "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br")])],-1),Ae=a("h3",{id:"signal-unit"},[a("a",{class:"header-anchor",href:"#signal-unit","aria-hidden":"true"},"#"),e(" Signal Unit")],-1),De=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module integer_to_segment\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief raw data to segment encoder\n * @param DATA_WIDTH data width\n * @input data raw decimal data (4 bit)\n * @output seg_data bit data for cNodes\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" integer_to_segment\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" int_data"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" seg_data\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("int_data"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token punctuation"},"("),e("int_data"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"4'b0000"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11000000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 0"),e("\n "),a("span",{class:"token number"},"4'b0001"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11111001"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 1"),e("\n "),a("span",{class:"token number"},"4'b0010"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10100100"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 2"),e("\n "),a("span",{class:"token number"},"4'b0011"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10110000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 3"),e("\n "),a("span",{class:"token number"},"4'b0100"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10011001"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 4"),e("\n "),a("span",{class:"token number"},"4'b0101"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10010010"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 5"),e("\n "),a("span",{class:"token number"},"4'b0110"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10000010"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 6"),e("\n "),a("span",{class:"token number"},"4'b0111"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11111000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 7"),e("\n "),a("span",{class:"token number"},"4'b1000"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10000000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 8"),e("\n "),a("span",{class:"token number"},"4'b1001"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10010000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 9"),e("\n "),a("span",{class:"token number"},"4'b1010"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10001000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// a"),e("\n "),a("span",{class:"token number"},"4'b1011"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10000011"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// b"),e("\n "),a("span",{class:"token number"},"4'b1100"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11000110"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// c"),e("\n "),a("span",{class:"token number"},"4'b1101"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10100001"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// d"),e("\n "),a("span",{class:"token number"},"4'b1110"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10000110"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// e"),e("\n "),a("span",{class:"token number"},"4'b1111"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10001110"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// f"),e("\n "),a("span",{class:"token keyword"},"default"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11111111"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// off"),e("\n "),a("span",{class:"token keyword"},"endcase"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br")])],-1),Te=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module data_to_segment\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief raw data to segment encoder\n * @param DATA_WIDTH data width\n * @input data raw decimal data\n * @output seg_data bit data for cNodes\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" data_to_segment\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" data"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" seg_data\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans1 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans2 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans3 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"23"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans4 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"24"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans5 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"19"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"39"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans6 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"23"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"47"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"40"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans7 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"27"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"24"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"55"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"48"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans8 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"28"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"63"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"56"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br")])],-1),xe=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module led_unit\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief led display module (bind to aNodes and cNodes in FPGA)\n * @param DATA_WIDTH data width\n * @input clk_src clock signal (light different led on in round turn)\n * @input led_data raw decimal data\n * @output aNodes determine which led light on at now\n * @output cNodes determine how led light on (number)\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" led_unit\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk_src"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" led_data"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aNodes"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" cNodes\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" count"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// 2^3 = 8"),e("\n "),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" seg_data"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n aNodes "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n data_to_segment #"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"DATA_WIDTH"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),e(" data_to_segment "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),e("led_data"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk_src"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"="),e(" count "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("count"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token punctuation"},"("),e("count"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"3'b000"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111110"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b001"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111101"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b010"),a("span",{class:"token punctuation"},":"),e(" "),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111011"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"23"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b011"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11110111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"24"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b100"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11101111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"39"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b101"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11011111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"47"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"40"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b110"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b10111111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"55"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"48"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b111"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b01111111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"63"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"56"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"default"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111110"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111111"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"endcase"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br")])],-1),Ie=a("h3",{id:"alu-unit"},[a("a",{class:"header-anchor",href:"#alu-unit","aria-hidden":"true"},"#"),e(" ALU Unit")],-1),Be=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module counter\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief up counter\n * @param DATA_WIDTH data width\n * @param STEP counting step\n * @input clk clock signal\n * @input rst reset signal\n * @output en enable signal\n * @output count counting value\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" counter\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" STEP "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" rst"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" en"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" count\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("rst"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("en"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// counter"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br")])],-1),We=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module latch_counter\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief latch counter (latching when reaching max value)\n * @input clk clock signal\n * @input rst reset signal\n * @output en enable signal\n * @output count counting value\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" latch_counter\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" rst"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" en"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(" count\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("rst"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("en"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("count "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// latch_counter"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br")])],-1),Ce=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module alu_flags\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief get flags after alu calculation\n * @param DATA_WIDTH data width\n * @input srcA A port data\n * @input srcB B port data\n * @input aluOP operation code\n * @output zero equal flag\n * @output of signed overflow flag\n * @output uof unsigned overflow flag\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" alu_flags\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" srcA"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" srcB"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aluOP"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" zero"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" of"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" uof\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" sum"),a("span",{class:"token punctuation"},","),e(" diff"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" carry1"),a("span",{class:"token punctuation"},","),e(" carry2"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(),a("span",{class:"token operator"},"{"),e("carry1"),a("span",{class:"token punctuation"},","),e(" sum"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"="),e(" srcA "),a("span",{class:"token operator"},"+"),e(" srcB"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// awesome tip"),e("\n "),a("span",{class:"token keyword"},"assign"),e(),a("span",{class:"token operator"},"{"),e("carry2"),a("span",{class:"token punctuation"},","),e(" diff"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"="),e(" srcA "),a("span",{class:"token operator"},"-"),e(" srcB"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// awesome tip"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" zero "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("srcA "),a("span",{class:"token operator"},"=="),e(" srcB"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" of "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("aluOP "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"4'd5"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"("),e("srcA"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" srcB"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(),a("span",{class:"token operator"},"~"),e("sum"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"~"),e("srcA"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(),a("span",{class:"token operator"},"~"),e("srcB"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" sum"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token punctuation"},"("),e("aluOP "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"4'd6"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"("),e("srcA"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(),a("span",{class:"token operator"},"~"),e("srcB"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(),a("span",{class:"token operator"},"~"),e("diff"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"~"),e("srcA"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" srcB"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" diff"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" uof "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("aluOP "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"4'd5"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token punctuation"},"("),e("carry1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token punctuation"},"("),e("aluOP "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"4'd6"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token punctuation"},"("),e("carry2"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// alu_flags"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br")])],-1),He=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module alu\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @param DATA_WIDTH data width\n * @input srcA A port data\n * @input srcB B port data\n * @input aluOP operation code\n * @output aluOut calculation result\n * @output zero equal flag\n * @output of signed overflow flag\n * @output uof unsigned overflow flag\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" alu\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" srcA"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" srcB"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aluOP"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aluOut"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" zero"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" of"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" uof\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token keyword"},"signed"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" signed_srcA"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token keyword"},"signed"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" signed_srcB"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" signed_srcA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$signed"),a("span",{class:"token punctuation"},"("),e("srcA"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" signed_srcB "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$signed"),a("span",{class:"token punctuation"},"("),e("srcB"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),e(),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token punctuation"},"("),e("aluOP"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"4'd0"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"<<"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd1"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" signed_srcA "),a("span",{class:"token operator"},">>>"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd2"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},">>"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd3"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"*"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd4"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"/"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd5"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"+"),e(" srcB"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// awesome tip"),e("\n "),a("span",{class:"token number"},"4'd6"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"-"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd7"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"&"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd8"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"|"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd9"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"^"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd10"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token operator"},"~"),a("span",{class:"token punctuation"},"("),e("srcA "),a("span",{class:"token operator"},"|"),e(" srcB"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd11"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token punctuation"},"("),e("signed_srcA "),a("span",{class:"token operator"},"<"),e(" signed_srcB"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd12"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token punctuation"},"("),e("srcA "),a("span",{class:"token operator"},"<"),e(" srcB"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"default"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endcase"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n alu_flags #"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"DATA_WIDTH"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),e(" FLAGS "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"srcA"),a("span",{class:"token punctuation"},"("),e("srcA"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"srcB"),a("span",{class:"token punctuation"},"("),e("srcB"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"aluOP"),a("span",{class:"token punctuation"},"("),e("aluOP"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"zero"),a("span",{class:"token punctuation"},"("),e("zero"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"of"),a("span",{class:"token punctuation"},"("),e("of"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"uof"),a("span",{class:"token punctuation"},"("),e("uof"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// alu"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br")])],-1),Ne=a("h3",{id:"memory-unit"},[a("a",{class:"header-anchor",href:"#memory-unit","aria-hidden":"true"},"#"),e(" Memory Unit")],-1),Oe=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module register\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief D flip flop\n * @param DATA_WIDTH data width\n * @input clk clock signal\n * @input rst reset signal\n * @input en enable signal\n * @input din data in\n * @output dout data out\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" register\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" rst"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" en"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" din"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" dout\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("rst"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n dout "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// reset"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("en"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n dout "),a("span",{class:"token operator"},"<="),e(" din"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// update"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n dout "),a("span",{class:"token operator"},"<="),e(" dout"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// hold"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// register"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br")])],-1),ze=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module regFile\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief register files for MIPS CPU, contains 32 D flip-flop registers\n * @param DATA_WIDTH data width\n * @input clk clock signal\n * @input we write enable signal\n * @input raddrA read address (No.register) for A out port\n * @input raddrB read address (No.register) for B out port\n * @input waddr write address (No.register) for wdata (in port)\n * @input wdata data to write into regFile\n * @output regA A port output\n * @output regB B port output\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" regFile\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" rst"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" we"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" raddrA"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" raddrB"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" waddr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" wdata"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" regA"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" regB"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" v0_data"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" a0_data\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token constant"},"`include"),e(),a("span",{class:"token string"},'"defines.vh"'),e("\n\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" i"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"///< three ported regFile contains 32 registers"),e("\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" regFile "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("rst"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"="),e(" i "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n regFile"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("we "),a("span",{class:"token operator"},"&&"),e(" waddr "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n regFile"),a("span",{class:"token punctuation"},"["),e("waddr"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<="),e(" wdata"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" regA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("we "),a("span",{class:"token operator"},"&&"),e(" waddr "),a("span",{class:"token operator"},"=="),e(" raddrA"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" wdata\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token punctuation"},"("),e("raddrA "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" regFile"),a("span",{class:"token punctuation"},"["),e("raddrA"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" regB "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("we "),a("span",{class:"token operator"},"&&"),e(" waddr "),a("span",{class:"token operator"},"=="),e(" raddrB"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" wdata\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token punctuation"},"("),e("raddrB "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" regFile"),a("span",{class:"token punctuation"},"["),e("raddrB"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" v0_data "),a("span",{class:"token operator"},"="),e(" regFile"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"`V0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" a0_data "),a("span",{class:"token operator"},"="),e(" regFile"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"`A0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// regFile"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br")])],-1),Se=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module imem\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief instruction cache memory (ROM)\n * @param DATA_WIDTH data width\n * @param BUS_WIDTH bus width\n * @param CODE_FILE MIPS assembly hexadecimal code file\n * @input addr memory address\n * @output rdata instruction read out from memory\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" imem\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},","),e(" BUS_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),e(" CODE_FILE"),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"mips/benchmark.hex"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("BUS_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" addr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" rdata\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" ROM "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"**"),e("BUS_WIDTH"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token property"},"$readmemh"),a("span",{class:"token punctuation"},"("),e("CODE_FILE"),a("span",{class:"token punctuation"},","),e(" ROM"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"**"),e("BUS_WIDTH"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" rdata "),a("span",{class:"token operator"},"="),e(" ROM"),a("span",{class:"token punctuation"},"["),e("addr"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// imem"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br")])],-1),Pe=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module dmem\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief data cache memory (RAM)\n * @param DATA_WIDTH data width\n * @param BUS_WIDTH bus width\n * @input clk clock signal\n * @input re read enable signal\n * @input we write enable signal\n * @input addr memory address\n * @input wdata data write into memory\n * @output rdata data read out from memory\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" dmem\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},","),e(" BUS_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" re"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" we"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("BUS_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" addr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" wdata"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" switch_addr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" rdata"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" led_data\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" RAM "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"**"),e("BUS_WIDTH"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("we"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n RAM"),a("span",{class:"token punctuation"},"["),e("addr"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<="),e(" wdata"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" rdata "),a("span",{class:"token operator"},"="),e(" re "),a("span",{class:"token operator"},"?"),e(" RAM"),a("span",{class:"token punctuation"},"["),e("addr"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token operator"},"{"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'bx"),a("span",{class:"token operator"},"}}"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" led_data "),a("span",{class:"token operator"},"="),e(" RAM"),a("span",{class:"token punctuation"},"["),e("switch_addr"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// dmem"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br")])],-1);o.render=function(a,e,t,o,Ue,Me){return n(),s("div",null,[l,c,p,u,r,i,k,b,m,d,g,w,y,h,f,v,_,A,D,T,x,I,B,W,C,H,N,O,z,S,P,U,M,E,L,R,F,$,q,V,G,X,K,j,Z,Y,Q,J,nn,sn,an,en,tn,on,ln,cn,pn,un,rn,kn,bn,mn,dn,gn,wn,yn,hn,fn,vn,_n,An,Dn,Tn,xn,In,Bn,Wn,Cn,Hn,Nn,On,zn,Sn,Pn,Un,Mn,En,Ln,Rn,Fn,$n,qn,Vn,Gn,Xn,Kn,jn,Zn,Yn,Qn,Jn,ns,ss,as,es,ts,os,ls,cs,ps,us,rs,is,ks,bs,ms,ds,gs,ws,ys,hs,fs,vs,_s,As,Ds,Ts,xs,Is,Bs,Ws,Cs,Hs,Ns,Os,zs,Ss,Ps,Us,Ms,Es,Ls,Rs,Fs,$s,qs,Vs,Gs,Xs,Ks,js,Zs,Ys,Qs,Js,na,sa,aa,ea,ta,oa,la,ca,pa,ua,ra,ia,ka,ba,ma,da,ga,wa,ya,ha,fa,va,_a,Aa,Da,Ta,xa,Ia,Ba,Wa,Ca,Ha,Na,Oa,za,Sa,Pa,Ua,Ma,Ea,La,Ra,Fa,$a,qa,Va,Ga,Xa,Ka,ja,Za,Ya,Qa,Ja,ne,se,ae,ee,te,oe,le,ce,pe,ue,re,ie,ke,be,me,de,ge,we,ye,he,fe,ve,_e,Ae,De,Te,xe,Ie,Be,We,Ce,He,Ne,Oe,ze,Se,Pe])};export default o;export{t as __pageData}; diff --git a/assets/language_verilog_verilogBasicNotes.md.8503596e.lean.js b/assets/language_verilog_verilogBasicNotes.md.8503596e.lean.js new file mode 100644 index 00000000000..8218d79aecf --- /dev/null +++ b/assets/language_verilog_verilogBasicNotes.md.8503596e.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as e}from"./app.6dd2a1b7.js";const t='{"title":"Verilog Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"General Flow","slug":"general-flow"},{"level":3,"title":"Create Project","slug":"create-project"},{"level":3,"title":"RTL Analysis","slug":"rtl-analysis"},{"level":3,"title":"Simulate Design","slug":"simulate-design"},{"level":3,"title":"Synthesize Design","slug":"synthesize-design"},{"level":3,"title":"Implement Design","slug":"implement-design"},{"level":3,"title":"Post-Implementation Timing Simulate Design","slug":"post-implementation-timing-simulate-design"},{"level":3,"title":"Generate Bit Stream","slug":"generate-bit-stream"},{"level":3,"title":"Testing and Verification","slug":"testing-and-verification"},{"level":2,"title":"Timing in Circuits","slug":"timing-in-circuits"},{"level":3,"title":"Combination Circuit Timing","slug":"combination-circuit-timing"},{"level":3,"title":"Sequential Circuit Timing","slug":"sequential-circuit-timing"},{"level":2,"title":"Key Words","slug":"key-words"},{"level":2,"title":"Module","slug":"module"},{"level":3,"title":"外部端口","slug":"外部端口"},{"level":2,"title":"function","slug":"function"},{"level":3,"title":"task","slug":"task"},{"level":2,"title":"Data Structure","slug":"data-structure"},{"level":3,"title":"常量","slug":"常量"},{"level":3,"title":"向量","slug":"向量"},{"level":3,"title":"数字","slug":"数字"},{"level":3,"title":"Register","slug":"register"},{"level":3,"title":"Net","slug":"net"},{"level":2,"title":"Gate Level","slug":"gate-level"},{"level":3,"title":"Basic Gate","slug":"basic-gate"},{"level":3,"title":"Use Gate","slug":"use-gate"},{"level":3,"title":"Self-Defined Gate(用户自定义原语)","slug":"self-defined-gate-用户自定义原语"},{"level":2,"title":"Dataflow Level","slug":"dataflow-level"},{"level":3,"title":"Operators","slug":"operators"},{"level":3,"title":"整数提升","slug":"整数提升"},{"level":2,"title":"Behavior Level","slug":"behavior-level"},{"level":3,"title":"时延控制","slug":"时延控制"},{"level":3,"title":"事件控制","slug":"事件控制"},{"level":3,"title":"语句内/间控制","slug":"语句内-间控制"},{"level":3,"title":"always","slug":"always"},{"level":3,"title":"if-else","slug":"if-else"},{"level":3,"title":"Case Statement","slug":"case-statement"},{"level":3,"title":"for","slug":"for"},{"level":3,"title":"repeat loop","slug":"repeat-loop"},{"level":3,"title":"forever loop","slug":"forever-loop"},{"level":3,"title":"Force and Release","slug":"force-and-release"},{"level":3,"title":"Blocking and Non-Blocking","slug":"blocking-and-non-blocking"},{"level":3,"title":"disable","slug":"disable"},{"level":2,"title":"结构建模","slug":"结构建模"},{"level":3,"title":"generate 语句","slug":"generate-语句"},{"level":2,"title":"Delay(时延)","slug":"delay-时延"},{"level":2,"title":"预编译指令","slug":"预编译指令"},{"level":3,"title":"define 宏","slug":"define-宏"},{"level":2,"title":"Data Path","slug":"data-path"},{"level":3,"title":"Multiplexer","slug":"multiplexer"},{"level":3,"title":"Adder","slug":"adder"},{"level":3,"title":"Register Data Path","slug":"register-data-path"},{"level":3,"title":"Memory","slug":"memory"},{"level":2,"title":"Demos","slug":"demos"},{"level":3,"title":"Binary Multiplier","slug":"binary-multiplier"},{"level":3,"title":"Multi-Hz","slug":"multi-hz"},{"level":2,"title":"Tips","slug":"tips"},{"level":3,"title":"不可综合结构","slug":"不可综合结构"},{"level":3,"title":"混合编程","slug":"混合编程"},{"level":3,"title":"上升沿/下降沿","slug":"上升沿-下降沿"},{"level":3,"title":"Parameter","slug":"parameter"},{"level":2,"title":"有限状态机(FSM)","slug":"有限状态机-fsm"},{"level":2,"title":"算术状态机(ASM)","slug":"算术状态机-asm"},{"level":2,"title":"System Verilog","slug":"system-verilog"},{"level":3,"title":"Enum","slug":"enum"},{"level":3,"title":"Struct and Union","slug":"struct-and-union"},{"level":3,"title":"Procedural Block","slug":"procedural-block"},{"level":3,"title":"Interface","slug":"interface"},{"level":3,"title":"Testing","slug":"testing"},{"level":2,"title":"U280 Platform","slug":"u280-platform"},{"level":3,"title":"Host Application","slug":"host-application"},{"level":3,"title":"U280 Tools","slug":"u280-tools"},{"level":2,"title":"AXI Protocol","slug":"axi-protocol"},{"level":3,"title":"AXI Channels","slug":"axi-channels"},{"level":3,"title":"AXI Burst","slug":"axi-burst"},{"level":2,"title":"Verilog Components","slug":"verilog-components"},{"level":3,"title":"Clock Unit","slug":"clock-unit"},{"level":3,"title":"Signal Unit","slug":"signal-unit"},{"level":3,"title":"ALU Unit","slug":"alu-unit"},{"level":3,"title":"Memory Unit","slug":"memory-unit"}],"relativePath":"language/verilog/verilogBasicNotes.md","lastUpdated":1627196319000}',o={},l=a("h1",{id:"verilog-basic-notes"},[a("a",{class:"header-anchor",href:"#verilog-basic-notes","aria-hidden":"true"},"#"),e(" Verilog Basic Notes")],-1),c=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#verilog-basic-notes"},"Verilog Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#general-flow"},"General Flow")]),a("li",null,[a("a",{href:"#timing-in-circuits"},"Timing in Circuits")]),a("li",null,[a("a",{href:"#key-words"},"Key Words")]),a("li",null,[a("a",{href:"#module"},"Module")]),a("li",null,[a("a",{href:"#function"},"function")]),a("li",null,[a("a",{href:"#data-structure"},"Data Structure")]),a("li",null,[a("a",{href:"#gate-level"},"Gate Level")]),a("li",null,[a("a",{href:"#dataflow-level"},"Dataflow Level")]),a("li",null,[a("a",{href:"#behavior-level"},"Behavior Level")]),a("li",null,[a("a",{href:"#结构建模"},"结构建模")]),a("li",null,[a("a",{href:"#delay-时延"},"Delay(时延)")]),a("li",null,[a("a",{href:"#预编译指令"},"预编译指令")]),a("li",null,[a("a",{href:"#data-path"},"Data Path")]),a("li",null,[a("a",{href:"#demos"},"Demos")]),a("li",null,[a("a",{href:"#tips"},"Tips")]),a("li",null,[a("a",{href:"#有限状态机-fsm"},"有限状态机(FSM)")]),a("li",null,[a("a",{href:"#算术状态机-asm"},"算术状态机(ASM)")]),a("li",null,[a("a",{href:"#system-verilog"},"System Verilog")]),a("li",null,[a("a",{href:"#u280-platform"},"U280 Platform")]),a("li",null,[a("a",{href:"#axi-protocol"},"AXI Protocol")]),a("li",null,[a("a",{href:"#verilog-components"},"Verilog Components")])])])])])],-1),p=a("h2",{id:"general-flow"},[a("a",{class:"header-anchor",href:"#general-flow","aria-hidden":"true"},"#"),e(" General Flow")],-1),u=a("h3",{id:"create-project"},[a("a",{class:"header-anchor",href:"#create-project","aria-hidden":"true"},"#"),e(" Create Project")],-1),r=a("ul",null,[a("li",null,[a("code",null,".v"),e(" (sources)")]),a("li",null,[a("code",null,".xdc"),e(" (constraints)")])],-1),i=a("h3",{id:"rtl-analysis"},[a("a",{class:"header-anchor",href:"#rtl-analysis","aria-hidden":"true"},"#"),e(" RTL Analysis")],-1),k=a("p",null,"Schematic",-1),b=a("h3",{id:"simulate-design"},[a("a",{class:"header-anchor",href:"#simulate-design","aria-hidden":"true"},"#"),e(" Simulate Design")],-1),m=a("ul",null,[a("li",null,[a("code",null,"_tb.v"),e(": test bench")]),a("li",null,"simulation options: 500ns"),a("li",null,"Scopes -> dut -> swt/led"),a("li",null,"Schematic")],-1),d=a("h3",{id:"synthesize-design"},[a("a",{class:"header-anchor",href:"#synthesize-design","aria-hidden":"true"},"#"),e(" Synthesize Design")],-1),g=a("p",null,"Schematic",-1),w=a("h3",{id:"implement-design"},[a("a",{class:"header-anchor",href:"#implement-design","aria-hidden":"true"},"#"),e(" Implement Design")],-1),y=a("p",null,"Schematic",-1),h=a("h3",{id:"post-implementation-timing-simulate-design"},[a("a",{class:"header-anchor",href:"#post-implementation-timing-simulate-design","aria-hidden":"true"},"#"),e(" Post-Implementation Timing Simulate Design")],-1),f=a("h3",{id:"generate-bit-stream"},[a("a",{class:"header-anchor",href:"#generate-bit-stream","aria-hidden":"true"},"#"),e(" Generate Bit Stream")],-1),v=a("h3",{id:"testing-and-verification"},[a("a",{class:"header-anchor",href:"#testing-and-verification","aria-hidden":"true"},"#"),e(" Testing and Verification")],-1),_=a("p",null,"low-level (circuit) simulation is much slower than high-level (C, HDL) simulation:",-1),A=a("ul",null,[a("li",null,"check only functionality at high level (C, HDL)"),a("li",null,"check only timing, power at low level (circuit)")],-1),D=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token function"},"testBench"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// clock and reset are internal"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" a"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},","),e(" c"),a("span",{class:"token punctuation"},","),e(" yExpected"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// values from testVectors"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" y"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// output of circuit"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" vectorNum"),a("span",{class:"token punctuation"},","),e(" errors"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// bookkeeping variables"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" testVectors"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10000"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// array of testVectors"),e("\n\n "),a("span",{class:"token comment"},"// instantiate device under test"),e("\n sillyFunction "),a("span",{class:"token function"},"dut"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"a"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"b"),a("span",{class:"token punctuation"},"("),e("b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"c"),a("span",{class:"token punctuation"},"("),e("c"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"y"),a("span",{class:"token punctuation"},"("),e("y"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// generate clock"),e("\n "),a("span",{class:"token important"},"always "),e(" "),a("span",{class:"token comment"},"// no sensitivity list, so it always executes"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token number"},"#5"),a("span",{class:"token punctuation"},";"),e(" clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token number"},"#5"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 10ns period"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token comment"},"// at start of test, load vectors and pulse reset"),e("\n "),a("span",{class:"token keyword"},"initial"),e(" "),a("span",{class:"token comment"},"// Only executes once"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token property"},"$readmemb"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"example.tv"'),a("span",{class:"token punctuation"},","),e(" testVectors"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// Read vectors: e.g 000_0 001_1 ... xxx_x"),e("\n vectorNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" errors "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// Initialize"),e("\n reset "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token number"},"#27"),a("span",{class:"token punctuation"},";"),e(" reset "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// Apply reset wait"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token comment"},"// Note: $readmemh reads testVector files written in"),e("\n "),a("span",{class:"token comment"},"// hexadecimal"),e("\n "),a("span",{class:"token comment"},"// apply test vectors on rising edge of clk"),e("\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token number"},"#1"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token operator"},"{"),e("a"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},","),e(" c"),a("span",{class:"token punctuation"},","),e(" yExpected"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"="),e(" testVectors"),a("span",{class:"token punctuation"},"["),e("vectorNum"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"negedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"~"),e("reset"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},"// don’t test during reset"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("y "),a("span",{class:"token operator"},"!=="),e(" yExpected"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"Error: inputs = %b"'),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"{"),e("a"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},","),e(" c"),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'" outputs = %b (%b exp)"'),a("span",{class:"token punctuation"},","),e("y"),a("span",{class:"token punctuation"},","),e("yExpected"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n errors "),a("span",{class:"token operator"},"="),e(" errors "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token comment"},"// increment array index and read next testVector"),e("\n vectorNum "),a("span",{class:"token operator"},"="),e(" vectorNum "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("testVectors"),a("span",{class:"token punctuation"},"["),e("vectorNum"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"==="),e(),a("span",{class:"token number"},"4'bx"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"%d tests completed with %d errors"'),a("span",{class:"token punctuation"},","),e(" vectorNum"),a("span",{class:"token punctuation"},","),e(" errors"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// End simulation"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br")])],-1),T=a("h2",{id:"timing-in-circuits"},[a("a",{class:"header-anchor",href:"#timing-in-circuits","aria-hidden":"true"},"#"),e(" Timing in Circuits")],-1),x=a("h3",{id:"combination-circuit-timing"},[a("a",{class:"header-anchor",href:"#combination-circuit-timing","aria-hidden":"true"},"#"),e(" Combination Circuit Timing")],-1),I=a("ul",null,[a("li",null,[e("contamination delay ("),a("code",null,"t_cd"),e("): minimum path in circuits, outputs start to change")]),a("li",null,[e("propagation delay ("),a("code",null,"t_pd"),e("): maximum path in circuits, outputs complete change")]),a("li",null,[e("(delay) heavy dependence on "),a("strong",null,"voltage"),e(" and "),a("strong",null,"temperature")])],-1),B=a("h3",{id:"sequential-circuit-timing"},[a("a",{class:"header-anchor",href:"#sequential-circuit-timing","aria-hidden":"true"},"#"),e(" Sequential Circuit Timing")],-1),W=a("p",null,[e("minimize clock skew time: requires intelligent "),a("strong",null,"clock network"),e(" across a chip, making clock arrives at all locations at roughly the same time.")],-1),C=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("T_clock "),a("span",{class:"token operator"},">="),e(" T_pcq "),a("span",{class:"token operator"},"+"),e(" T_pd "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token punctuation"},"("),e("T_setup "),a("span",{class:"token operator"},"+"),e(" T_skew"),a("span",{class:"token punctuation"},")"),e("\nT_ccq "),a("span",{class:"token operator"},"+"),e(" T_cd "),a("span",{class:"token operator"},">"),e(),a("span",{class:"token punctuation"},"("),e("T_hold "),a("span",{class:"token operator"},"+"),e(" T_skew"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),H=a("h2",{id:"key-words"},[a("a",{class:"header-anchor",href:"#key-words","aria-hidden":"true"},"#"),e(" Key Words")],-1),N=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e("\n"),a("span",{class:"token keyword"},"input"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"output"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"inout"),e("\n"),a("span",{class:"token keyword"},"wire"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"reg"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"parameter"),e("\n"),a("span",{class:"token important"},"always@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"assign"),e("\n"),a("span",{class:"token keyword"},"posedge"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"negedge"),e("\n"),a("span",{class:"token keyword"},"if"),a("span",{class:"token operator"},"-"),a("span",{class:"token keyword"},"else"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"case"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"begin"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e(),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),O=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("port"),a("span",{class:"token punctuation"},","),e(" signal\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),z=a("h2",{id:"module"},[a("a",{class:"header-anchor",href:"#module","aria-hidden":"true"},"#"),e(" Module")],-1),S=a("ul",null,[a("li",null,"Inout(Net) -> (Net)Module(Net) -> Inout(Net)"),a("li",null,"Input(Net/Register) -> (Net)Module(Net/Register) -> Output(Net)")],-1),P=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(),a("span",{class:"token function"},"moduleName"),a("span",{class:"token punctuation"},"("),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},","),e(" Out1"),a("span",{class:"token punctuation"},","),e(" Out2"),a("span",{class:"token punctuation"},","),e(" InOut1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"// 输入输出定义: 说明端口是输入还是输出"),e("\n "),a("span",{class:"token keyword"},"input"),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(" Out1"),a("span",{class:"token punctuation"},","),e(" Out2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"inout"),e(" InOut1"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// 数据类型定义: 说明端口数据类型 - Net/Register"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},","),e(" Out1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" InOut1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" Out2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"// Instantiation of lower level modules"),e("\n Design "),a("span",{class:"token function"},"u_2"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"("),e("端口"),a("span",{class:"token punctuation"},"("),e("信号"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"("),e("端口"),a("span",{class:"token function"},"2"),a("span",{class:"token punctuation"},"("),e("信号"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"("),e("端口"),a("span",{class:"token function"},"3"),a("span",{class:"token punctuation"},"("),e("信号"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// Functionality"),e("\n\n "),a("span",{class:"token comment"},"// 三种层次的描述: 逻辑层次(Gate Level), 数据流层次(Dataflow Level), 行为层次(Behavior Level)"),e("\n "),a("span",{class:"token comment"},"// and/or - gate level"),e("\n "),a("span",{class:"token keyword"},"and"),e(),a("span",{class:"token function"},"and1"),a("span",{class:"token punctuation"},"("),e(" Out1"),a("span",{class:"token punctuation"},","),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"// assign - dataflow level"),e("\n "),a("span",{class:"token keyword"},"assign"),e(),a("span",{class:"token number"},"#2"),e(" Out1 "),a("span",{class:"token operator"},"="),e(" In1 "),a("span",{class:"token operator"},"&"),e(" In2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"// always/initial - behavior level"),e("\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n Out2 "),a("span",{class:"token operator"},"="),e(" In1 "),a("span",{class:"token operator"},"&"),e(" In2\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token comment"},"// Timing specification"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br")])],-1),U=a("h3",{id:"外部端口"},[a("a",{class:"header-anchor",href:"#外部端口","aria-hidden":"true"},"#"),e(" 外部端口")],-1),M=a("ul",null,[a("li",null,"封装内部端口,装饰者模式"),a("li",null,"一个内部端口可与多个外部端口相连")],-1),E=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" scram_b "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),e("arb"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"control"),a("span",{class:"token punctuation"},"("),e("ctrl"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"mem_word"),a("span",{class:"token punctuation"},"("),e("mem_blk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"addr"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"byte"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(" arb"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" ctrl"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" mem_blk"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token keyword"},"byte"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br")])],-1),L=a("h2",{id:"function"},[a("a",{class:"header-anchor",href:"#function","aria-hidden":"true"},"#"),e(" function")],-1),R=a("ul",null,[a("li",null,"不含时间/事件控制"),a("li",null,"至少 1 个输入"),a("li",null,"至多 1 个输出"),a("li",null,"只含行为模块"),a("li",null,"只含阻塞赋值语句")],-1),F=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aligned_word"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// function declaration"),e("\n "),a("span",{class:"token keyword"},"input"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" word"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n aligned_word "),a("span",{class:"token operator"},"="),e(" word"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("aligned_word "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"while"),e(),a("span",{class:"token punctuation"},"("),e("aligned_word"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(" aligned_word "),a("span",{class:"token operator"},"="),e(" aligned_word "),a("span",{class:"token operator"},"<<"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),$=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" arithmetic_unit "),a("span",{class:"token punctuation"},"("),e("result_1"),a("span",{class:"token punctuation"},","),e(" result_2"),a("span",{class:"token punctuation"},","),e(" operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" result_1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" result_2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" result_1 "),a("span",{class:"token operator"},"="),e(" sum_of_operands "),a("span",{class:"token punctuation"},"("),e("operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" result_2 "),a("span",{class:"token operator"},"="),e(" larger_operand "),a("span",{class:"token punctuation"},"("),e("operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token function"},"sum_of_operands"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n sum_of_operands "),a("span",{class:"token operator"},"="),e(" operand_1 "),a("span",{class:"token operator"},"+"),e(" operand_2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token function"},"larger_operand"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" operand_1"),a("span",{class:"token punctuation"},","),e(" operand_2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n larger_operand "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("operand_1 "),a("span",{class:"token operator"},">="),e(" operand_2"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" operand_1 "),a("span",{class:"token punctuation"},":"),e(" operand_2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br")])],-1),q=a("h3",{id:"task"},[a("a",{class:"header-anchor",href:"#task","aria-hidden":"true"},"#"),e(" task")],-1),V=a("p",null,"将测试流程分为多个任务:",-1),G=a("ul",null,[a("li",null,"初始化任务"),a("li",null,"模拟生成任务"),a("li",null,"自我检查任务")],-1),X=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" adder_task "),a("span",{class:"token punctuation"},"("),e("c_out"),a("span",{class:"token punctuation"},","),e(" sum"),a("span",{class:"token punctuation"},","),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset"),a("span",{class:"token punctuation"},","),e(" c_in"),a("span",{class:"token punctuation"},","),e(" data_a"),a("span",{class:"token punctuation"},","),e(" data_b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" sum"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(" c_out"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" data_a"),a("span",{class:"token punctuation"},","),e(" data_b"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset"),a("span",{class:"token punctuation"},","),e(" c_in"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk "),a("span",{class:"token keyword"},"or"),e(),a("span",{class:"token keyword"},"posedge"),e(" reset"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("reset"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"{"),e("c_out"),a("span",{class:"token punctuation"},","),e(" sum"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"else"),e(" add_values "),a("span",{class:"token punctuation"},"("),e("sum"),a("span",{class:"token punctuation"},","),e(" c_out"),a("span",{class:"token punctuation"},","),e(" data_a"),a("span",{class:"token punctuation"},","),e(" data_b"),a("span",{class:"token punctuation"},","),e(" c_in"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// invoke task"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"task"),e(" add_values"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// task declaration"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" SUM"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(" C_OUT"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" DATA_A"),a("span",{class:"token punctuation"},","),e(" DATA_B"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" C_IN"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token operator"},"{"),e("C_OUT"),a("span",{class:"token punctuation"},","),e(" SUM"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"="),e(" DATA_A "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token punctuation"},"("),e("DATA_B "),a("span",{class:"token operator"},"+"),e(" C_IN"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br")])],-1),K=a("p",null,'常用的 task 有: $display("fmt", ...), $monitor("fmt", ...), $time, $finish',-1),j=a("h2",{id:"data-structure"},[a("a",{class:"header-anchor",href:"#data-structure","aria-hidden":"true"},"#"),e(" Data Structure")],-1),Z=a("h3",{id:"常量"},[a("a",{class:"header-anchor",href:"#常量","aria-hidden":"true"},"#"),e(" 常量")],-1),Y=a("ul",null,[a("li",null,"0: 逻辑 0"),a("li",null,"1: 逻辑 1"),a("li",null,"x/X: Unknown/Floating"),a("li",null,"z/Z: 高阻抗状态(High Impedance)"),a("li",null,"parameter: #define")],-1),Q=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"localparam"),e(" idle "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"2'b00"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"parameter"),e(" Bit "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},","),e(" cnt_up "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1'b1"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("Bit "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" A"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("Bit "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" A"),a("span",{class:"token punctuation"},";"),e("\n\nA "),a("span",{class:"token operator"},"="),e(" A "),a("span",{class:"token operator"},"+"),e(" cnt_up"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),J=a("h3",{id:"向量"},[a("a",{class:"header-anchor",href:"#向量","aria-hidden":"true"},"#"),e(" 向量")],-1),nn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"["),e("MSB"),a("span",{class:"token punctuation"},":"),e(" LSB"),a("span",{class:"token punctuation"},"]"),e(" 或 "),a("span",{class:"token punctuation"},"["),e("LSB"),a("span",{class:"token punctuation"},":"),e(" MSB"),a("span",{class:"token punctuation"},"]"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),sn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" A"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 4 bit"),e("\n"),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 4 bit"),e("\n"),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"63"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" D"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 64 bit"),e("\n"),a("span",{class:"token keyword"},"wire"),e(" E"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 1 bit"),e("\n\nA"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(" B"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" C"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A[2] = 0 & C[1], A[1] = B[0] & C[2]"),e("\nB"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\nC "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'b1011"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// C[0] = 1, C[1] = 0, C[2] = 1, C[3] = 1"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),an=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"integer"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" A "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 4元素数组. 每个元素为16bit元"),e("\n"),a("span",{class:"token keyword"},"reg"),e(" B "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// arr[4][16]数组, 每个元素为1bit元"),e("\n"),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" C "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// arr[4][16]数组, 每个元素为8bit元"),e("\n\nA"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 置零A中第三个元素(8 bit)"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 置零A"),e("\nB"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// B[1][0](1 bit)置1"),e("\nC"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'b0010"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// C[0][0] 低4bit为0010"),e("\nC"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// C[2][8] 第5bit为1"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),en=a("h4",{id:"部分位选"},[a("a",{class:"header-anchor",href:"#部分位选","aria-hidden":"true"},"#"),e(" 部分位选")],-1),tn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("vector"),a("span",{class:"token punctuation"},"["),e("base_expr"),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},":"),e(" const_width"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\nvector"),a("span",{class:"token punctuation"},"["),e("base_expr"),a("span",{class:"token operator"},"-"),a("span",{class:"token punctuation"},":"),e(" const_width"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\ninst_mode"),a("span",{class:"token punctuation"},"["),e("mark"),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// => mark,mark+1"),e("\ngpio_mode"),a("span",{class:"token punctuation"},"["),e("mark"),a("span",{class:"token operator"},"-"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// => mark,mark-1,mark-2,mark-3"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),on=a("h3",{id:"数字"},[a("a",{class:"header-anchor",href:"#数字","aria-hidden":"true"},"#"),e(" 数字")],-1),ln=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// size ' signed base value"),e("\n"),a("span",{class:"token operator"},"<"),e("Bits长度"),a("span",{class:"token operator"},">"),e("'"),a("span",{class:"token punctuation"},"["),a("span",{class:"token keyword"},"signed"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token operator"},"<"),e("进制"),a("span",{class:"token operator"},"><"),e("数值"),a("span",{class:"token operator"},">"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),cn=a("ul",null,[a("li",null,"位长不能用表达式表示,只可用固定的 parameter")],-1),pn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("Num "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"5'b01101"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 二进制"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"22"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 十进制"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"12'b0000_1111_0000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 可读性"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'hf"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 十六进制(1111)"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'bxxx1"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 前三位未知"),e("\nNum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4'bz01"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 前两位为z, 后两位为01"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),un=a("h4",{id:"有符号数"},[a("a",{class:"header-anchor",href:"#有符号数","aria-hidden":"true"},"#"),e(" 有符号数")],-1),rn=a("ul",null,[a("li",null,"signed reg"),a("li",null,"signed wire"),a("li",null,"integer"),a("li",null,"'sxx")],-1),kn=a("h4",{id:"无符号数"},[a("a",{class:"header-anchor",href:"#无符号数","aria-hidden":"true"},"#"),e(" 无符号数")],-1),bn=a("ul",null,[a("li",null,"reg"),a("li",null,"wire"),a("li",null,"'xx")],-1),mn=a("h3",{id:"register"},[a("a",{class:"header-anchor",href:"#register","aria-hidden":"true"},"#"),e(" Register")],-1),dn=a("ul",null,[a("li",null,"reg/integer/time/real/realtime"),a("li",null,"有记忆性"),a("li",null,"默认值: x")],-1),gn=a("h4",{id:"integer"},[a("a",{class:"header-anchor",href:"#integer","aria-hidden":"true"},"#"),e(" integer")],-1),wn=a("p",null,"长度为 32 Bit, 补码表示, 常用于计数器",-1),yn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" CLK"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"integer"),e(" i"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("i "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"="),e(" i "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(" tmp"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"="),e(" In"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),hn=a("h4",{id:"real"},[a("a",{class:"header-anchor",href:"#real","aria-hidden":"true"},"#"),e(" real")],-1),fn=a("ul",null,[a("li",null,"real 默认值为 0,不可为 x/z"),a("li",null,"不可声明位宽")],-1),vn=a("h3",{id:"net"},[a("a",{class:"header-anchor",href:"#net","aria-hidden":"true"},"#"),e(" Net")],-1),_n=a("ul",null,[a("li",null,"wire/wand/wor"),a("li",null,"局部变量, 没有记忆性"),a("li",null,"默认值: z"),a("li",null,"wire 间不可直接相连, wand/wor 间课直接相连"),a("li",null,"可用 wire 定义局部变量")],-1),An=a("h2",{id:"gate-level"},[a("a",{class:"header-anchor",href:"#gate-level","aria-hidden":"true"},"#"),e(" Gate Level")],-1),Dn=a("h3",{id:"basic-gate"},[a("a",{class:"header-anchor",href:"#basic-gate","aria-hidden":"true"},"#"),e(" Basic Gate")],-1),Tn=a("ul",null,[a("li",null,"and"),a("li",null,"nand(与非)"),a("li",null,"or"),a("li",null,"nor(或非)"),a("li",null,"xor(异或)"),a("li",null,"xnor(同或)")],-1),xn=a("h3",{id:"use-gate"},[a("a",{class:"header-anchor",href:"#use-gate","aria-hidden":"true"},"#"),e(" Use Gate")],-1),In=a("ul",null,[a("li",null,"同一模块中, 实例名不能与线网名相同")],-1),Bn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"and"),e(),a("span",{class:"token punctuation"},"("),e("w1"),a("span",{class:"token punctuation"},","),e(" In1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// w1 = Int and In2"),e("\n"),a("span",{class:"token keyword"},"or"),e(),a("span",{class:"token function"},"or1"),a("span",{class:"token punctuation"},"("),e("w2"),a("span",{class:"token punctuation"},","),e(" w1"),a("span",{class:"token punctuation"},","),e(" In2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// w2 = w1 or In2"),e("\n"),a("span",{class:"token keyword"},"xor"),e(),a("span",{class:"token function"},"xor"),a("span",{class:"token punctuation"},"("),e("Out"),a("span",{class:"token punctuation"},","),e(" w1"),a("span",{class:"token punctuation"},","),e(" w2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// Out = w1 xor w2"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Wn=a("ul",null,[a("li",null,"实例数组")],-1),Cn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" irq"),a("span",{class:"token punctuation"},","),e(" ctrl"),a("span",{class:"token punctuation"},","),e(" sense"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"/*\n * =>\n * nand\n * u8nand3 (irq[3], ctrl[3], sense[3]);\n * u8nand2 (irq[2], ctrl[2], sense[2]);\n * u8nand1 (irq[1], ctrl[1], sense[1]);\n * u8nand0 (irq[0], ctrl[0], sense[0]);\n */"),e("\n"),a("span",{class:"token keyword"},"nand"),e(" u8nand "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"("),e("irq"),a("span",{class:"token punctuation"},","),e(" ctrl"),a("span",{class:"token punctuation"},","),e(" sense"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Hn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"parameter"),e(" NUM_BITS "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),e("NUM_BITS "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" gated_d"),a("span",{class:"token punctuation"},","),e(" din"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"wire"),e(" bypass"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"and"),e(" #"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(" u0and "),a("span",{class:"token punctuation"},"["),e("NUM_BITS "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},"("),e("gated_d"),a("span",{class:"token punctuation"},","),e(" din"),a("span",{class:"token punctuation"},","),e(" bypass"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Nn=a("h3",{id:"self-defined-gate-用户自定义原语"},[a("a",{class:"header-anchor",href:"#self-defined-gate-用户自定义原语","aria-hidden":"true"},"#"),e(" Self-Defined Gate(用户自定义原语)")],-1),On=a("ul",null,[a("li",null,"可以有一个/多个输入"),a("li",null,"只能有一个输出"),a("li",null,"第一个端口必须是输出端口"),a("li",null,[a("code",null,"-"),e(' 表示 值"无变化"')])],-1),zn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"primitive"),e(" XOR2 "),a("span",{class:"token punctuation"},"("),e("D_OUT"),a("span",{class:"token punctuation"},","),e(" X1"),a("span",{class:"token punctuation"},","),e(" X2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"input"),e(" X1"),a("span",{class:"token punctuation"},","),e(" X2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"output"),e(" D_OUT"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"table"),e(),a("span",{class:"token comment"},"// X1 X2 : D_OUT"),e("\n "),a("span",{class:"token number"},"0"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"0"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"1"),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"1"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtable"),e("\n"),a("span",{class:"token keyword"},"endprimitive"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Sn=a("h2",{id:"dataflow-level"},[a("a",{class:"header-anchor",href:"#dataflow-level","aria-hidden":"true"},"#"),e(" Dataflow Level")],-1),Pn=a("ul",null,[a("li",null,[e("assign net = net/reg: "),a("strong",null,"左式只能是 net")])],-1),Un=a("h3",{id:"operators"},[a("a",{class:"header-anchor",href:"#operators","aria-hidden":"true"},"#"),e(" Operators")],-1),Mn=a("ul",null,[a("li",null,[a("a",{href:"https://hom-wang.gitbooks.io/verilog-hdl/content/Chapter_04.html",target:"_blank",rel:"noopener noreferrer"},"Operators List")])],-1),En=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("赋值"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token operator"},"<="),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"="),e("\n"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"<"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"<="),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},">="),e("\n"),a("span",{class:"token operator"},"!="),a("span",{class:"token punctuation"},"."),e(),a("span",{class:"token operator"},"=="),e("\n"),a("span",{class:"token punctuation"},"["),e(),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),e(),a("span",{class:"token operator"},"{"),e(),a("span",{class:"token operator"},"}"),e("\n"),a("span",{class:"token operator"},"<<"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},">>"),e("\n"),a("span",{class:"token operator"},"+"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"-"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"/"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"%"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Ln=a("h3",{id:"整数提升"},[a("a",{class:"header-anchor",href:"#整数提升","aria-hidden":"true"},"#"),e(" 整数提升")],-1),Rn=a("ul",null,[a("li",null,"表达式所有中间取 最大位宽(最长(左/右)操作数)")],-1),Fn=a("h4",{id:""},[a("a",{class:"header-anchor",href:"#","aria-hidden":"true"},"#"),e(" { }")],-1),$n=a("p",null,[e("可实现 haskell 中的"),a("strong",null,"模式匹配")],-1),qn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 连接运算符"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{"),e(),a("span",{class:"token number"},"1'b0"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"1'b1"),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A = 2'b01"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{"),e(" B"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(" D"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A = B[1], B[2], C[0], D[2]"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"2'b01"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A = 4'b0101"),e("\nA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{"),e(),a("span",{class:"token number"},"3'b101"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'b0"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"}"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// A = 5'b101_00"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Vn=a("h2",{id:"behavior-level"},[a("a",{class:"header-anchor",href:"#behavior-level","aria-hidden":"true"},"#"),e(" Behavior Level")],-1),Gn=a("ul",null,[a("li",null,[e("reg = net/reg: *"),a("em",null,"左式只能是 reg")])],-1),Xn=a("h3",{id:"时延控制"},[a("a",{class:"header-anchor",href:"#时延控制","aria-hidden":"true"},"#"),e(" 时延控制")],-1),Kn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("#num\n\n"),a("span",{class:"token keyword"},"parameter"),e(" cycle "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"30"),a("span",{class:"token punctuation"},";"),e("\n\n# "),a("span",{class:"token number"},"2"),e("\n# cycle"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),jn=a("h3",{id:"事件控制"},[a("a",{class:"header-anchor",href:"#事件控制","aria-hidden":"true"},"#"),e(" 事件控制")],-1),Zn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n@"),a("span",{class:"token punctuation"},"("),e(" sel"),a("span",{class:"token punctuation"},","),e(" a"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n@"),a("span",{class:"token punctuation"},"("),e("sel "),a("span",{class:"token keyword"},"or"),e(" a "),a("span",{class:"token keyword"},"or"),e(" b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" CLK"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"negedge"),e(" CLK"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Yn=a("h3",{id:"语句内-间控制"},[a("a",{class:"header-anchor",href:"#语句内-间控制","aria-hidden":"true"},"#"),e(" 语句内/间控制")],-1),Qn=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("q "),a("span",{class:"token operator"},"="),e(" @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk_iol"),a("span",{class:"token punctuation"},")"),e(" d"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// 语句内事件控制"),e("\n\n@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk_iol"),a("span",{class:"token punctuation"},")"),e(" "),a("span",{class:"token comment"},"// 语句间事件控制"),e("\n q "),a("span",{class:"token operator"},"="),e(" temp"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),Jn=a("h3",{id:"always"},[a("a",{class:"header-anchor",href:"#always","aria-hidden":"true"},"#"),e(" always")],-1),ns=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("事件"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" 事件"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),ss=a("h3",{id:"if-else"},[a("a",{class:"header-anchor",href:"#if-else","aria-hidden":"true"},"#"),e(" if-else")],-1),as=a("ul",null,[a("li",null,"必须添加 else")],-1),es=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("condition1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("condition2"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"else"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br")])],-1),ts=a("h3",{id:"case-statement"},[a("a",{class:"header-anchor",href:"#case-statement","aria-hidden":"true"},"#"),e(" Case Statement")],-1),os=a("ul",null,[a("li",null,"expr: 常量/变量/连接运算符{ }/x/z"),a("li",null,"casex: 当输入某一位为 x/z 时,忽略此位匹配(恒将此位匹配为真)"),a("li",null,"casez: 当输入某一位为 z 时,忽略此位匹配(恒将此位匹配为真)")],-1),ls=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token punctuation"},"("),e("expr"),a("span",{class:"token punctuation"},")"),e("\n item "),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n item "),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},":"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n item "),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"default"),a("span",{class:"token punctuation"},":"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endcase"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),cs=a("h3",{id:"for"},[a("a",{class:"header-anchor",href:"#for","aria-hidden":"true"},"#"),e(" for")],-1),ps=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("循环初值"),a("span",{class:"token punctuation"},";"),e(" 循环条件"),a("span",{class:"token punctuation"},";"),e(" 控制部分"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),us=a("h3",{id:"repeat-loop"},[a("a",{class:"header-anchor",href:"#repeat-loop","aria-hidden":"true"},"#"),e(" repeat loop")],-1),rs=a("ul",null,[a("li",null,[e("initial for "),a("strong",null,"test bench")]),a("li",null,[e("当需 if/else 进行断言时,注意 "),a("strong",null,"延时"),e(" 造成的错误逻辑")])],-1),is=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 重复事件控制:"),e("\n"),a("span",{class:"token comment"},"// 先计算好右值, 等待时钟 clk 上出现2个负跳变沿, 再把右值赋给 result"),e("\nresult "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"repeat"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e(" @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"negedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(" hw_data "),a("span",{class:"token operator"},"+"),e(" hr_data"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"// repeat 循环语句:"),e("\n"),a("span",{class:"token keyword"},"repeat"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(" result "),a("span",{class:"token operator"},"="),e(" hw_data "),a("span",{class:"token operator"},"+"),e(" hr_data"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),ks=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n inc_DAC "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e("’b1"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token function"},"repeat"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4095"),a("span",{class:"token punctuation"},")"),e(" @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// bring DAC right up to point of rollover"),e("\n inc_DAC "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e("’b0"),a("span",{class:"token punctuation"},";"),e("\n inc_sym "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e("’b1"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token function"},"repeat"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},")"),e("@"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// bring sample count up to 7"),e("\n inc_sym "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),e("’b0"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token number"},"#100"),e(),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// run simulation for 100 units"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),bs=a("h3",{id:"forever-loop"},[a("a",{class:"header-anchor",href:"#forever-loop","aria-hidden":"true"},"#"),e(" forever loop")],-1),ms=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// $stop, $finish 可以终止 forever loop"),e("\n"),a("span",{class:"token keyword"},"forever"),e(),a("span",{class:"token number"},"#10"),e(" clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"~"),e(" clk"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),ds=a("h3",{id:"force-and-release"},[a("a",{class:"header-anchor",href:"#force-and-release","aria-hidden":"true"},"#"),e(" Force and Release")],-1),gs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"initial"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"force"),e(" test_reset "),a("span",{class:"token operator"},"="),e(" penalty "),a("span",{class:"token operator"},"&"),e(" rtc_intr"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"#5"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"release"),e(" test_reset"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),ws=a("h3",{id:"blocking-and-non-blocking"},[a("a",{class:"header-anchor",href:"#blocking-and-non-blocking","aria-hidden":"true"},"#"),e(" Blocking and Non-Blocking")],-1),ys=a("ul",null,[a("li",null,"Blocking(=): 顺序执行"),a("li",null,"Non-Blocking(<=): 并行执行")],-1),hs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token operator"},"="),e(" input_logic"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token operator"},"<="),e(" input_logic"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),fs=a("h3",{id:"disable"},[a("a",{class:"header-anchor",href:"#disable","aria-hidden":"true"},"#"),e(" disable")],-1),vs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"begin"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"break"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<"),e(" n"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"="),e(" i"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"continue"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("a "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},'// "continue" loop'),e("\n "),a("span",{class:"token keyword"},"disable"),e(),a("span",{class:"token keyword"},"continue"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("a "),a("span",{class:"token operator"},"=="),e(" b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token comment"},'// "break" from loop'),e("\n "),a("span",{class:"token keyword"},"disable"),e(),a("span",{class:"token keyword"},"break"),a("span",{class:"token punctuation"},";"),e("\n statement1\n statement2\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),_s=a("h2",{id:"结构建模"},[a("a",{class:"header-anchor",href:"#结构建模","aria-hidden":"true"},"#"),e(" 结构建模")],-1),As=a("h3",{id:"generate-语句"},[a("a",{class:"header-anchor",href:"#generate-语句","aria-hidden":"true"},"#"),e(" generate 语句")],-1),Ds=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"generate"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("gv_i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" gv_i "),a("span",{class:"token operator"},"<"),e(" SIZE"),a("span",{class:"token punctuation"},";"),e(" gv_i "),a("span",{class:"token operator"},"="),e(" gv_i "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),a("span",{class:"token punctuation"},":"),e(" blk\n "),a("span",{class:"token keyword"},"xor"),e(" uxor "),a("span",{class:"token punctuation"},"("),e("y"),a("span",{class:"token punctuation"},"["),e("gv_i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(" a"),a("span",{class:"token punctuation"},"["),e("gv_i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),e(" b"),a("span",{class:"token punctuation"},"["),e("gv_i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"endgenerate"),e("\n"),a("span",{class:"token comment"},"// =>"),e("\n"),a("span",{class:"token comment"},"// module.blk[0].uxor"),e("\n"),a("span",{class:"token comment"},"// module.blk[1].uxor"),e("\n"),a("span",{class:"token comment"},"// module.blk[2].uxor"),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),Ts=a("h2",{id:"delay-时延"},[a("a",{class:"header-anchor",href:"#delay-时延","aria-hidden":"true"},"#"),e(" Delay(时延)")],-1),xs=a("ul",null,[a("li",null,"语句内时延"),a("li",null,"语句间时延"),a("li",null,"语句内时延期间:右值保持稳定不变,才可成功赋给左值")],-1),Is=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("sum "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("a "),a("span",{class:"token operator"},"^"),e(" b"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"^"),e(" cin"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token number"},"#4"),e(" t1 "),a("span",{class:"token operator"},"="),e(" a "),a("span",{class:"token operator"},"&"),e(" cin"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Bs=a("h2",{id:"预编译指令"},[a("a",{class:"header-anchor",href:"#预编译指令","aria-hidden":"true"},"#"),e(" 预编译指令")],-1),Ws=a("h3",{id:"define-宏"},[a("a",{class:"header-anchor",href:"#define-宏","aria-hidden":"true"},"#"),e(" define 宏")],-1),Cs=a("p",null,"将多个 define 宏,放至 _defines.v, 作为全局宏",-1),Hs=a("h2",{id:"data-path"},[a("a",{class:"header-anchor",href:"#data-path","aria-hidden":"true"},"#"),e(" Data Path")],-1),Ns=a("h3",{id:"multiplexer"},[a("a",{class:"header-anchor",href:"#multiplexer","aria-hidden":"true"},"#"),e(" Multiplexer")],-1),Os=a("h3",{id:"adder"},[a("a",{class:"header-anchor",href:"#adder","aria-hidden":"true"},"#"),e(" Adder")],-1),zs=a("h3",{id:"register-data-path"},[a("a",{class:"header-anchor",href:"#register-data-path","aria-hidden":"true"},"#"),e(" Register Data Path")],-1),Ss=a("h3",{id:"memory"},[a("a",{class:"header-anchor",href:"#memory","aria-hidden":"true"},"#"),e(" Memory")],-1),Ps=a("ul",null,[a("li",null,"其中数据文件中地址必须在系统任务中定义的范围内,系统任务中定义的地址必须在存储器定义的地址范围内"),a("li",null,"优先考虑数据文件中的地址>系统任务中定义的起始地址和结束地址>存储器定义的起始地址和结束地址")],-1),Us=a("h2",{id:"demos"},[a("a",{class:"header-anchor",href:"#demos","aria-hidden":"true"},"#"),e(" Demos")],-1),Ms=a("ul",null,[a("li",null,[a("a",{href:"https://hom-wang.gitbooks.io/verilog-hdl/content/Chapter_07.html",target:"_blank",rel:"noopener noreferrer"},"GitBook")]),a("li",null,[a("a",{href:"http://www.xilinx.com/support/university/ise/ise-teaching-material/hdl-design.html",target:"_blank",rel:"noopener noreferrer"},"Xilinx Lab")])],-1),Es=a("h3",{id:"binary-multiplier"},[a("a",{class:"header-anchor",href:"#binary-multiplier","aria-hidden":"true"},"#"),e(" Binary Multiplier")],-1),Ls=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e(" "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),e("the multiplicand"),a("span",{class:"token punctuation"},")"),e("\nx "),a("span",{class:"token number"},"1011"),e(),a("span",{class:"token punctuation"},"("),e("the multiplier"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"0000"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"initial"),e(" partial product"),a("span",{class:"token punctuation"},","),e(" start "),a("span",{class:"token keyword"},"with"),e(),a("span",{class:"token number"},"0000"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),e("st multiplier "),a("span",{class:"token keyword"},"bit"),e(" is "),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" so add the multiplicand"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),e("sum"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"01100"),e(),a("span",{class:"token punctuation"},"("),e("shift sum one position to the right"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),e("nd multiplier "),a("span",{class:"token keyword"},"bit"),e(" is "),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" so add multiplicand again"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"100100"),e(),a("span",{class:"token punctuation"},"("),e("sum"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"with"),e(" a carry generated on the left"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"100100"),e(),a("span",{class:"token punctuation"},"("),e("shift sum once to the right"),a("span",{class:"token punctuation"},","),e(" including carry"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"0100100"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"3"),e("rd multiplier "),a("span",{class:"token keyword"},"bit"),e(" is "),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),e(" so skip add"),a("span",{class:"token punctuation"},","),e(" shift once"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"1100"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"4"),e("th multiplier "),a("span",{class:"token keyword"},"bit"),e(" is "),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" so add multiplicand again"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"----"),e("\n "),a("span",{class:"token number"},"10000100"),e(),a("span",{class:"token punctuation"},"("),e("sum"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"with"),e(" a carry generated on the left"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"10000100"),e(),a("span",{class:"token punctuation"},"("),e("shift sum once to the right"),a("span",{class:"token punctuation"},","),e(" including carry"),a("span",{class:"token punctuation"},")"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),Rs=a("h3",{id:"multi-hz"},[a("a",{class:"header-anchor",href:"#multi-hz","aria-hidden":"true"},"#"),e(" Multi-Hz")],-1),Fs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/*\n * 利用计数器实现任意分频\n */"),e("\n"),a("span",{class:"token important"},"always"),e(" @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" f_clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n\n "),a("span",{class:"token comment"},"//设定频率控制字p "),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"=="),e(" p"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n i"),a("span",{class:"token operator"},"="),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n f_out"),a("span",{class:"token operator"},"=~"),e("f_out"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n i"),a("span",{class:"token operator"},"="),e("i"),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),$s=a("h2",{id:"tips"},[a("a",{class:"header-anchor",href:"#tips","aria-hidden":"true"},"#"),e(" Tips")],-1),qs=a("h3",{id:"不可综合结构"},[a("a",{class:"header-anchor",href:"#不可综合结构","aria-hidden":"true"},"#"),e(" 不可综合结构")],-1),Vs=a("ul",null,[a("li",null,"initial: 只用于 test bench"),a("li",null,"events: Events 同步测试各个组件"),a("li",null,"real: Real 数据类型不可综合"),a("li",null,"time: Time 数据类型不可综合"),a("li",null,"force/release"),a("li",null,"assign(reg)/deassign(reg)"),a("li",null,"fork join"),a("li",null,"primitive: 只有门级的原语(primitives)可综合"),a("li",null,"table: 用户自定义原语(UDP)及 table 不可综合"),a("li",null,[a("code",null,"#1"),e(" 延迟只用于仿真,综合器直接忽略延迟")])],-1),Gs=a("h3",{id:"混合编程"},[a("a",{class:"header-anchor",href:"#混合编程","aria-hidden":"true"},"#"),e(" 混合编程")],-1),Xs=a("ul",null,[a("li",null,"内部变量用 assign 赋值"),a("li",null,"输出变量通过监听 内部变量 改变输出值")],-1),Ks=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e(" "),a("span",{class:"token keyword"},"assign"),e(" DT0 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" DT1 "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("DT0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n AOut "),a("span",{class:"token operator"},"<="),e(" DT0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("DT1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n BOut "),a("span",{class:"token operator"},"<="),e(" DT1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br")])],-1),js=a("h3",{id:"上升沿-下降沿"},[a("a",{class:"header-anchor",href:"#上升沿-下降沿","aria-hidden":"true"},"#"),e(" 上升沿/下降沿")],-1),Zs=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e(" "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" A "),a("span",{class:"token keyword"},"or"),e(),a("span",{class:"token keyword"},"negedge"),e(" B"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),e("B"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n "),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n "),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Ys=a("h3",{id:"parameter"},[a("a",{class:"header-anchor",href:"#parameter","aria-hidden":"true"},"#"),e(" Parameter")],-1),Qs=a("ul",null,[a("li",null,"只在定义的模块内部起作用")],-1),Js=a("h4",{id:"overload-method"},[a("a",{class:"header-anchor",href:"#overload-method","aria-hidden":"true"},"#"),e(" Overload Method")],-1),na=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" data_path\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" A"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" B"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" C\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),sa=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" data_path_tb\n"),a("span",{class:"token punctuation"},"("),e("\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n data_path #"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"DATA_WIDTH"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e(" DUT "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"A"),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"B"),a("span",{class:"token punctuation"},"("),e("B"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"C"),a("span",{class:"token punctuation"},"("),e("C"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),aa=a("h4",{id:"constant-variable"},[a("a",{class:"header-anchor",href:"#constant-variable","aria-hidden":"true"},"#"),e(" Constant Variable")],-1),ea=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[e("reset_value "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"{{"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'b0"),a("span",{class:"token operator"},"}}"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"{"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'b1"),a("span",{class:"token operator"},"}}}"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),ta=a("h4",{id:"test-bench"},[a("a",{class:"header-anchor",href:"#test-bench","aria-hidden":"true"},"#"),e(" Test Bench")],-1),oa=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token important"},"always "),a("span",{class:"token keyword"},"begin"),e("\n clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"forever"),e(" #DELAY clk "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"~"),e("clk"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),la=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"reg"),e(" clock"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"integer"),e(" no_of_clocks"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"parameter"),e(" CLOCK_PERIOD "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"parameter"),e(" TIME "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"50000"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"initial"),e(" no_of_clocks "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"initial"),e(" clock "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1'b0"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token important"},"always "),e("#"),a("span",{class:"token punctuation"},"("),e("CLOCK_PERIOD"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"2.0"),a("span",{class:"token punctuation"},")"),e(" clock "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token operator"},"~"),e("clock"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clock"),a("span",{class:"token punctuation"},")"),e("\n no_of_clocks "),a("span",{class:"token operator"},"="),e(" no_of_clocks "),a("span",{class:"token operator"},"+"),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n #TIME"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),e('"End of simulation '),a("span",{class:"token keyword"},"time"),e(" is "),a("span",{class:"token operator"},"%"),e("d "),a("span",{class:"token punctuation"},","),e("\n total number of clocks seen is "),a("span",{class:"token operator"},"%"),e("d expected is "),a("span",{class:"token operator"},"%"),e('d"'),a("span",{class:"token punctuation"},","),a("span",{class:"token property"},"$time"),a("span",{class:"token punctuation"},","),e("no_of_clocks"),a("span",{class:"token punctuation"},","),a("span",{class:"token punctuation"},"("),a("span",{class:"token property"},"$time"),a("span",{class:"token operator"},"/"),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),ca=a("h2",{id:"有限状态机-fsm"},[a("a",{class:"header-anchor",href:"#有限状态机-fsm","aria-hidden":"true"},"#"),e(" 有限状态机(FSM)")],-1),pa=a("ul",null,[a("li",null,"reset: initial state"),a("li",null,"default: illegal/unreachable state")],-1),ua=a("h2",{id:"算术状态机-asm"},[a("a",{class:"header-anchor",href:"#算术状态机-asm","aria-hidden":"true"},"#"),e(" 算术状态机(ASM)")],-1),ra=a("ul",null,[a("li",null,"state box: moore fsm"),a("li",null,"conditional box: mealy fsm"),a("li",null,[e("decision box: "),a("code",null,"x_input"),e(" = 0/1")])],-1),ia=a("h2",{id:"system-verilog"},[a("a",{class:"header-anchor",href:"#system-verilog","aria-hidden":"true"},"#"),e(" System Verilog")],-1),ka=a("h3",{id:"enum"},[a("a",{class:"header-anchor",href:"#enum","aria-hidden":"true"},"#"),e(" Enum")],-1),ba=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"typedef"),e(),a("span",{class:"token keyword"},"enum"),e(),a("span",{class:"token keyword"},"logic"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"{"),e("\n RED"),a("span",{class:"token punctuation"},","),e(" GREEN"),a("span",{class:"token punctuation"},","),e(" BLUE"),a("span",{class:"token punctuation"},","),e(" CYAN"),a("span",{class:"token punctuation"},","),e(" MAGENTA"),a("span",{class:"token punctuation"},","),e(" YELLOW\n"),a("span",{class:"token operator"},"}"),e(" color_t"),a("span",{class:"token punctuation"},";"),e("\n\ncolor_t my_color "),a("span",{class:"token operator"},"="),e(" GREEN"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"The color is %s"'),a("span",{class:"token punctuation"},","),e(" my_color"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"name"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),ma=a("h3",{id:"struct-and-union"},[a("a",{class:"header-anchor",href:"#struct-and-union","aria-hidden":"true"},"#"),e(" Struct and Union")],-1),da=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"typedef"),e(),a("span",{class:"token keyword"},"struct"),e(),a("span",{class:"token keyword"},"packed"),e(),a("span",{class:"token operator"},"{"),e("\n "),a("span",{class:"token keyword"},"bit"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" expo"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"bit"),e(" sign"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"bit"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"51"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" man"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token operator"},"}"),e(" FP"),a("span",{class:"token punctuation"},";"),e("\n\nFP zero "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"64'b0"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),ga=a("h3",{id:"procedural-block"},[a("a",{class:"header-anchor",href:"#procedural-block","aria-hidden":"true"},"#"),e(" Procedural Block")],-1),wa=a("ul",null,[a("li",null,"always_comb: 用于组合逻辑电路(相当于 Verilog 中对所有输入变量电平敏感的 always,但 always_comb 无需手动列出所有输入变量,系统会自动识别)"),a("li",null,"always_ff: 用于触发器及相关的时序逻辑电路(相当于 Verilog 中对某个或某几个信号有效跳变沿敏感、并带有信号储存特性的 always)"),a("li",null,"always_latch: 用于锁存器级相关的时序逻辑电路(相当于 Verilog 中对某个或某几个信号电平敏感、并带有信号储存特性的的 always)")],-1),ya=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token important"},"always_comb "),a("span",{class:"token keyword"},"begin"),e("\n tmp "),a("span",{class:"token operator"},"="),e(" b "),a("span",{class:"token operator"},"*"),e(" b "),a("span",{class:"token operator"},"-"),e(),a("span",{class:"token number"},"4"),e(),a("span",{class:"token operator"},"*"),e(" a "),a("span",{class:"token operator"},"*"),e(" c"),a("span",{class:"token punctuation"},";"),e("\n no_root "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("tmp "),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token important"},"always_ff @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n\n\n"),a("span",{class:"token important"},"always_latch"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("en"),a("span",{class:"token punctuation"},")"),e(" q "),a("span",{class:"token operator"},"<="),e(" d"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),ha=a("h3",{id:"interface"},[a("a",{class:"header-anchor",href:"#interface","aria-hidden":"true"},"#"),e(" Interface")],-1),fa=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"interface"),e(" interfaceName"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"logic"),e(" a"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"logic"),e(" b"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"modport"),e(" in "),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"input"),e(" a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"output"),e(" b"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"modport"),e(" out "),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"input"),e(" b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token keyword"},"output"),e(" a"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endinterface"),e("\n\n"),a("span",{class:"token keyword"},"module"),e(" top"),a("span",{class:"token punctuation"},";"),e("\n interfaceName i "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n u_a m1 "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"i1"),a("span",{class:"token punctuation"},"("),e("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n u_b m2 "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"i2"),a("span",{class:"token punctuation"},"("),e("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n\n"),a("span",{class:"token keyword"},"module"),e(" u_a "),a("span",{class:"token punctuation"},"("),e("interfaceName"),a("span",{class:"token punctuation"},"."),e("in i1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n\n"),a("span",{class:"token keyword"},"module"),e(" u_b "),a("span",{class:"token punctuation"},"("),e("interfaceName"),a("span",{class:"token punctuation"},"."),e("out i2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br")])],-1),va=a("h3",{id:"testing"},[a("a",{class:"header-anchor",href:"#testing","aria-hidden":"true"},"#"),e(" Testing")],-1),_a=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"module"),e(" top"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"integer"),e(" num_packets "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$random"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"reg"),e(" A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},","),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset_n"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" D"),a("span",{class:"token punctuation"},";"),e("\n register_logic "),a("span",{class:"token function"},"dut"),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},","),e(" clk"),a("span",{class:"token punctuation"},","),e(" reset_n"),a("span",{class:"token punctuation"},","),e(" D"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"// generate clock"),e("\n "),a("span",{class:"token comment"},"// ..."),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n reset_n "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"#20"),e(" reset_n "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(" reset_n "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"#1"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"repeat"),e(),a("span",{class:"token punctuation"},"("),e("num_packets"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n A "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$random"),a("span",{class:"token punctuation"},";"),e(" B "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$random"),a("span",{class:"token punctuation"},";"),e(" C "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$random"),a("span",{class:"token punctuation"},";"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},","),e(" D"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br")])],-1),Aa=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(" Packet"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"string"),e(" name"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"rand"),e(),a("span",{class:"token keyword"},"bit"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" sa"),a("span",{class:"token punctuation"},","),e(" da"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"rand"),e(),a("span",{class:"token keyword"},"reg"),e(" A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"display"),a("span",{class:"token punctuation"},"("),e("result"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),e("A"),a("span",{class:"token punctuation"},","),e(" B"),a("span",{class:"token punctuation"},","),e(" C"),a("span",{class:"token punctuation"},","),e(" result"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n"),a("span",{class:"token keyword"},"endclass"),a("span",{class:"token punctuation"},":"),e(" Packet\n\n"),a("span",{class:"token comment"},"// inheritance"),e("\n"),a("span",{class:"token keyword"},"class"),e(" Packet_da_3 "),a("span",{class:"token keyword"},"extends"),e(" Packet"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"constraint"),e(" da_3 "),a("span",{class:"token operator"},"{"),e("\n da "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token operator"},"}"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"display"),a("span",{class:"token punctuation"},"("),e("result"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"super"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"display"),a("span",{class:"token punctuation"},"("),e("result"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token property"},"$display"),a("span",{class:"token punctuation"},"("),e("sa"),a("span",{class:"token punctuation"},","),e(" da"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n"),a("span",{class:"token keyword"},"endclass"),a("span",{class:"token punctuation"},":"),e(" Packet_da_3\n\n"),a("span",{class:"token keyword"},"class"),e(" Generator"),a("span",{class:"token punctuation"},";"),e("\n Packet pkt"),a("span",{class:"token punctuation"},";"),e("\n Channel out_chan"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"int"),e(" num_packets"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"function"),e(),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token function"},"gen"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n pkt "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),e("\n pkt"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"randomize"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n out_chan"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"put"),a("span",{class:"token punctuation"},"("),e("pkt"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endfunction"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"while"),e(),a("span",{class:"token punctuation"},"("),e("num_packets"),a("span",{class:"token operator"},"--"),e(),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"gen"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endclass"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(" Driver"),a("span",{class:"token punctuation"},";"),e("\n Channel in_chan"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"send"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n in_chan"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),e("pkt"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n top"),a("span",{class:"token punctuation"},"."),e("A "),a("span",{class:"token operator"},"="),e(" pkt"),a("span",{class:"token punctuation"},"."),e("A"),a("span",{class:"token punctuation"},";"),e("\n top"),a("span",{class:"token punctuation"},"."),e("B "),a("span",{class:"token operator"},"="),e(" pkt"),a("span",{class:"token punctuation"},"."),e("B"),a("span",{class:"token punctuation"},";"),e("\n top"),a("span",{class:"token punctuation"},"."),e("C "),a("span",{class:"token operator"},"="),e(" pkt"),a("span",{class:"token punctuation"},"."),e("C"),a("span",{class:"token punctuation"},";"),e("\n @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" top"),a("span",{class:"token punctuation"},"."),e("clk"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"forever"),e(),a("span",{class:"token function"},"send"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endclass"),e("\n\n"),a("span",{class:"token keyword"},"module"),e(" top"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token function"},"build"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"build"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n Config cfg "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n Channel chan "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n Generator gen "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n Driver drv "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token function"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n gen"),a("span",{class:"token punctuation"},"."),e("out_chan "),a("span",{class:"token operator"},"="),e(" chan"),a("span",{class:"token punctuation"},";"),e("\n drv"),a("span",{class:"token punctuation"},"."),e("in_chan "),a("span",{class:"token operator"},"="),e(" chan"),a("span",{class:"token punctuation"},";"),e("\n\n cfg"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"randomize"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"with"),e(),a("span",{class:"token operator"},"{"),e(" num_packets "),a("span",{class:"token operator"},">"),e(),a("span",{class:"token number"},"1500"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token operator"},"}"),e("\n gen"),a("span",{class:"token punctuation"},"."),e("num_packets "),a("span",{class:"token operator"},"="),e(" cfg"),a("span",{class:"token punctuation"},"."),e("num_packets"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n\n "),a("span",{class:"token keyword"},"task"),e(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"fork"),e("\n gen"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n drv"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"join"),e("\n "),a("span",{class:"token property"},"$finish"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endtask"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br")])],-1),Da=a("h2",{id:"u280-platform"},[a("a",{class:"header-anchor",href:"#u280-platform","aria-hidden":"true"},"#"),e(" U280 Platform")],-1),Ta=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[e("-xp param "),a("span",{class:"token punctuation"},"("),e("clock frequency etc."),a("span",{class:"token punctuation"},")"),e("\n-R report level\n-slr SLR region setting\n-sp memory resources mapping\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br")])],-1),xa=a("p",null,"tools:",-1),Ia=a("ul",null,[a("li",null,"xbutil query"),a("li",null,"platforminfo"),a("li",null,"kernelinfo"),a("li",null,[a("strong",null,"xclbinutil")]),a("li",null,"dmesg")],-1),Ba=a("h3",{id:"host-application"},[a("a",{class:"header-anchor",href:"#host-application","aria-hidden":"true"},"#"),e(" Host Application")],-1),Wa=a("h4",{id:"basic-flow"},[a("a",{class:"header-anchor",href:"#basic-flow","aria-hidden":"true"},"#"),e(" Basic Flow")],-1),Ca=a("ul",null,[a("li",null,"set the kernel arguments before performing any enqueue operation"),a("li",null,[e("keeping the buffer size "),a("strong",null,"2 MB ~ 4 GB")]),a("li",null,[a("code",null,"posix_memalign"),e(" is used instead of malloc for the host memory space pointer")])],-1),Ha=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"uint32_t"),e(),a("span",{class:"token operator"},"*"),e("a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"*"),e("b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"*"),e("c"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token operator"},"*"),e("d "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token constant"},"NULL"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"posix_memalign"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("a"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"4096"),a("span",{class:"token punctuation"},","),e(" BUF_SIZE "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"uint32_t"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"posix_memalign"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("b"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"4096"),a("span",{class:"token punctuation"},","),e(" BUF_SIZE "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"uint32_t"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"posix_memalign"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("c"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"4096"),a("span",{class:"token punctuation"},","),e(" BUF_SIZE "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"uint32_t"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"posix_memalign"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"void"),e(),a("span",{class:"token operator"},"*"),a("span",{class:"token operator"},"*"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"&"),e("d"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"4096"),a("span",{class:"token punctuation"},","),e(" BUF_SIZE "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token keyword"},"sizeof"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"uint32_t"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br")])],-1),Na=a("ul",null,[a("li",null,"release resources for proper performance profile report")],-1),Oa=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token function"},"clReleaseCommandQueue"),a("span",{class:"token punctuation"},"("),e("Command_Queue"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"clReleaseContext"),a("span",{class:"token punctuation"},"("),e("Context"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"clReleaseDevice"),a("span",{class:"token punctuation"},"("),e("Target_Device_ID"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"clReleaseKernel"),a("span",{class:"token punctuation"},"("),e("Kernel"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"clReleaseProgram"),a("span",{class:"token punctuation"},"("),e("Program"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"free"),a("span",{class:"token punctuation"},"("),e("Platform_IDs"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token function"},"free"),a("span",{class:"token punctuation"},"("),e("Device_IDs"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),za=a("h4",{id:"tlp"},[a("a",{class:"header-anchor",href:"#tlp","aria-hidden":"true"},"#"),e(" TLP")],-1),Sa=a("p",null,[e("It is advisable to use the "),a("code",null,"posix_spawn()"),e(" system call to launch another process from the SDAccel environment application.")],-1),Pa=a("h3",{id:"u280-tools"},[a("a",{class:"header-anchor",href:"#u280-tools","aria-hidden":"true"},"#"),e(" U280 Tools")],-1),Ua=a("h4",{id:"gdb-based-debugging"},[a("a",{class:"header-anchor",href:"#gdb-based-debugging","aria-hidden":"true"},"#"),e(" GDB Based Debugging")],-1),Ma=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[e("xprint queue "),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"<"),e("cl_command_queue"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},"]"),e("\nxprint event "),a("span",{class:"token operator"},"<"),e("cl_event"),a("span",{class:"token operator"},">"),e("\nxprint mem "),a("span",{class:"token punctuation"},"["),a("span",{class:"token operator"},"<"),e("cl_mem"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},"]"),e("\nxprint kernel\nxprint all\nxstatus all\nxstatus --"),a("span",{class:"token operator"},"<"),e("ipName"),a("span",{class:"token operator"},">"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),Ea=a("h4",{id:"xcl-binary-util"},[a("a",{class:"header-anchor",href:"#xcl-binary-util","aria-hidden":"true"},"#"),e(" XCL Binary Util")],-1),La=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,"xclbinutil -i binary_container_1.xclbin --info\n")]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Ra=a("h4",{id:"xocc"},[a("a",{class:"header-anchor",href:"#xocc","aria-hidden":"true"},"#"),e(" XOCC")],-1),Fa=a("p",null,"Checking out-of-bound access made by kernel interface buffers (option: address) and uninitialized memory access initiated by kernel local to kernel (option: memory).",-1),$a=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[e("xocc -l –t sw_emu --xp param:compiler.fsanitize"),a("span",{class:"token operator"},"="),e("address -o bin_kernel.xclbin\nxocc -l –t sw_emu --xp param:compiler.fsanitize"),a("span",{class:"token operator"},"="),e("memory -o bin_kernel.xclbin\nxocc -l –t sw_emu --xp param:compiler.fsanitize"),a("span",{class:"token operator"},"="),e("address,memory -o bin_kernel.xclbin\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),qa=a("h4",{id:"xbutil"},[a("a",{class:"header-anchor",href:"#xbutil","aria-hidden":"true"},"#"),e(" XBUtil")],-1),Va=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token function"},"sudo"),e(" /opt/xilinx/xrt/bin/xbutil flash -a "),a("span",{class:"token operator"},"<"),e("shell_name"),a("span",{class:"token operator"},">"),e(),a("span",{class:"token comment"},"# flash the firmware"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br")])],-1),Ga=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token function"},"sudo"),e(" lspci -vd 10ee:\n"),a("span",{class:"token function"},"sudo"),e(" /opt/xilinx/xrt/bin/xbutil flash scan\n"),a("span",{class:"token function"},"sudo"),e(" /opt/xilinx/xrt/bin/xbutil validate -d "),a("span",{class:"token operator"},"<"),e("card_id"),a("span",{class:"token operator"},">"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Xa=a("div",{class:"language-bash line-numbers-mode"},[a("pre",null,[a("code",null,[e("xbutil program -p "),a("span",{class:"token operator"},"<"),e("xclbin"),a("span",{class:"token operator"},">"),e("\nxbutil query "),a("span",{class:"token comment"},"# check memory banks usage"),e("\nxbutil status --lapc "),a("span",{class:"token comment"},"# check AXI violations"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br")])],-1),Ka=a("h4",{id:"dmesg"},[a("a",{class:"header-anchor",href:"#dmesg","aria-hidden":"true"},"#"),e(" dmesg")],-1),ja=a("h4",{id:"ila-trigger"},[a("a",{class:"header-anchor",href:"#ila-trigger","aria-hidden":"true"},"#"),e(" ILA Trigger")],-1),Za=a("ul",null,[a("li",null,"debug protocol hangs"),a("li",null,"examine the burst size, pipelining and data width to locate the bottleneck")],-1),Ya=a("div",{class:"language-cpp line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),a("span",{class:"token punctuation"},"."),e("\nstd"),a("span",{class:"token double-colon punctuation"},"::"),e("string binaryFile "),a("span",{class:"token operator"},"="),e(" xcl"),a("span",{class:"token double-colon punctuation"},"::"),a("span",{class:"token function"},"find_binary_file"),a("span",{class:"token punctuation"},"("),e("device_name"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"vAdd"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\ncl"),a("span",{class:"token double-colon punctuation"},"::"),e("Program"),a("span",{class:"token double-colon punctuation"},"::"),e("Binaries bins "),a("span",{class:"token operator"},"="),e(" xcl"),a("span",{class:"token double-colon punctuation"},"::"),a("span",{class:"token function"},"import_binary_file"),a("span",{class:"token punctuation"},"("),e("binaryFile"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\ndevices"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"resize"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\ncl"),a("span",{class:"token double-colon punctuation"},"::"),e("Program "),a("span",{class:"token function"},"program"),a("span",{class:"token punctuation"},"("),e("context"),a("span",{class:"token punctuation"},","),e(" devices"),a("span",{class:"token punctuation"},","),e(" bins"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\ncl"),a("span",{class:"token double-colon punctuation"},"::"),e("Kernel "),a("span",{class:"token function"},"kernel_vAdd"),a("span",{class:"token punctuation"},"("),e("program"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},'"kernel_vAdd_rtl"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},'// wait_for_enter("\\nPress ENTER to continue after setting up ILA trigger...");'),e("\nstd"),a("span",{class:"token double-colon punctuation"},"::"),e("cout "),a("span",{class:"token operator"},"<<"),e(),a("span",{class:"token string"},'"Pausing to arm ILA trigger. Hit enter here to resume host program..."'),e("\n "),a("span",{class:"token operator"},"<<"),e(" std"),a("span",{class:"token double-colon punctuation"},"::"),e("endl"),a("span",{class:"token punctuation"},";"),e("\nstd"),a("span",{class:"token double-colon punctuation"},"::"),e("cin"),a("span",{class:"token double-colon punctuation"},"::"),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"//Allocate Buffer in Global Memory"),e("\nstd"),a("span",{class:"token double-colon punctuation"},"::"),e("vector"),a("span",{class:"token operator"},"<"),e("cl"),a("span",{class:"token double-colon punctuation"},"::"),e("Memory"),a("span",{class:"token operator"},">"),e(" inBufVec"),a("span",{class:"token punctuation"},","),e(" outBufVec"),a("span",{class:"token punctuation"},";"),e("\ncl"),a("span",{class:"token double-colon punctuation"},"::"),e("Buffer "),a("span",{class:"token function"},"buffer_r1"),a("span",{class:"token punctuation"},"("),e("context"),a("span",{class:"token punctuation"},","),e("CL_MEM_USE_HOST_PTR "),a("span",{class:"token operator"},"|"),e(" CL_MEM_READ_ONLY"),a("span",{class:"token punctuation"},","),e("\nvector_size_bytes"),a("span",{class:"token punctuation"},","),e(" source_input1"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n\n"),a("span",{class:"token comment"},"//Copy input data to device global memory"),e("\nq"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"enqueueMigrateMemObjects"),a("span",{class:"token punctuation"},"("),e("inBufVec"),a("span",{class:"token punctuation"},","),a("span",{class:"token number"},"0"),a("span",{class:"token comment"},"/* 0 means from host*/"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token comment"},"//Set the Kernel Arguments"),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n"),a("span",{class:"token comment"},"// ..."),e("\n\n"),a("span",{class:"token comment"},"//Launch the Kernel"),e("\nq"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"enqueueTask"),a("span",{class:"token punctuation"},"("),e("kernel_vAdd"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br")])],-1),Qa=a("h2",{id:"axi-protocol"},[a("a",{class:"header-anchor",href:"#axi-protocol","aria-hidden":"true"},"#"),e(" AXI Protocol")],-1),Ja=a("p",null,"Advanced eXtensible Interface Protocol:",-1),ne=a("ul",null,[a("li",null,"handshake protocol: ready-valid protocol")],-1),se=a("h3",{id:"axi-channels"},[a("a",{class:"header-anchor",href:"#axi-channels","aria-hidden":"true"},"#"),e(" AXI Channels")],-1),ae=a("h4",{id:"read-address-channel"},[a("a",{class:"header-anchor",href:"#read-address-channel","aria-hidden":"true"},"#"),e(" Read Address Channel")],-1),ee=a("ul",null,[a("li",null,"arburst: burst type"),a("li",null,"araddr: start address"),a("li",null,"arlen: (# of transfers) - 1"),a("li",null,"arsize: bytes/transfer"),a("li",null,"arready (memory to host)"),a("li",null,"arvalid")],-1),te=a("h4",{id:"read-data-channel"},[a("a",{class:"header-anchor",href:"#read-data-channel","aria-hidden":"true"},"#"),e(" Read Data Channel")],-1),oe=a("ul",null,[a("li",null,"rdata: data"),a("li",null,"rresp: response (failure check)"),a("li",null,"rlast: flag for last piece of data"),a("li",null,"rready (host to memory)"),a("li",null,"rvalid")],-1),le=a("h4",{id:"write-address-channel"},[a("a",{class:"header-anchor",href:"#write-address-channel","aria-hidden":"true"},"#"),e(" Write Address Channel")],-1),ce=a("ul",null,[a("li",null,"awburst: burst type"),a("li",null,"awaddr: start address"),a("li",null,"awlen: (# of transfers) - 1"),a("li",null,"awsize: bytes/transfer"),a("li",null,"awready (memory to host)"),a("li",null,"awvalid")],-1),pe=a("h4",{id:"write-data-channel"},[a("a",{class:"header-anchor",href:"#write-data-channel","aria-hidden":"true"},"#"),e(" Write Data Channel")],-1),ue=a("ul",null,[a("li",null,"wdata: data"),a("li",null,"wstrb: write strobe -> write mask (1 bit mask for 1 byte data)"),a("li",null,"wlast: flag for last piece of data"),a("li",null,"wready (memory to host)"),a("li",null,"wvalid")],-1),re=a("h4",{id:"write-response-channel"},[a("a",{class:"header-anchor",href:"#write-response-channel","aria-hidden":"true"},"#"),e(" Write Response Channel")],-1),ie=a("ul",null,[a("li",null,"bresp: response (failure check)"),a("li",null,"bready (host to memory)"),a("li",null,"bvalid")],-1),ke=a("h3",{id:"axi-burst"},[a("a",{class:"header-anchor",href:"#axi-burst","aria-hidden":"true"},"#"),e(" AXI Burst")],-1),be=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"right"}},"AxBURST[1:0]"),a("th",{style:{"text-align":"left"}},"Burst Type")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"right"}},"0b00"),a("td",{style:{"text-align":"left"}},"FIXED")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b01"),a("td",{style:{"text-align":"left"}},"INCR")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b10"),a("td",{style:{"text-align":"left"}},"WRAP")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b11"),a("td",{style:{"text-align":"left"}},"Reserved")])])],-1),me=a("p",null,"burst length = AxLEN[7:0] + 1 (up to 256 transfers in each burst)",-1),de=a("table",null,[a("thead",null,[a("tr",null,[a("th",{style:{"text-align":"right"}},"AxSIZE[2:0]"),a("th",{style:{"text-align":"right"}},"Bytes in Transfer")])]),a("tbody",null,[a("tr",null,[a("td",{style:{"text-align":"right"}},"0b000"),a("td",{style:{"text-align":"right"}},"1")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b001"),a("td",{style:{"text-align":"right"}},"2")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b010"),a("td",{style:{"text-align":"right"}},"4")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b011"),a("td",{style:{"text-align":"right"}},"8")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b100"),a("td",{style:{"text-align":"right"}},"16")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b101"),a("td",{style:{"text-align":"right"}},"32")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b110"),a("td",{style:{"text-align":"right"}},"64")]),a("tr",null,[a("td",{style:{"text-align":"right"}},"0b111"),a("td",{style:{"text-align":"right"}},"128")])])],-1),ge=a("h4",{id:"read-burst"},[a("a",{class:"header-anchor",href:"#read-burst","aria-hidden":"true"},"#"),e(" Read Burst")],-1),we=a("p",null,[a("img",{src:"/awesome-notes/assets/AXI_Read_Burst.0e9ed250.png",alt:"Read Burst Example"})],-1),ye=a("h4",{id:"write-burst"},[a("a",{class:"header-anchor",href:"#write-burst","aria-hidden":"true"},"#"),e(" Write Burst")],-1),he=a("p",null,[a("img",{src:"/awesome-notes/assets/AXI_Write_Burst.63cb974a.png",alt:"Write Burst Example"})],-1),fe=a("h2",{id:"verilog-components"},[a("a",{class:"header-anchor",href:"#verilog-components","aria-hidden":"true"},"#"),e(" Verilog Components")],-1),ve=a("h3",{id:"clock-unit"},[a("a",{class:"header-anchor",href:"#clock-unit","aria-hidden":"true"},"#"),e(" Clock Unit")],-1),_e=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module tick_divider\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief simple clock divider\n * @param DATA_WIDTH data width\n * @input clk_src clock signal\n * @output clk_group divided clock signals\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" tick_divider\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk_src"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" clk_group\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n clk_group "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token operator"},"{"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'b0"),a("span",{class:"token operator"},"}}"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk_src"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n clk_group "),a("span",{class:"token operator"},"<="),e(" clk_group "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br")])],-1),Ae=a("h3",{id:"signal-unit"},[a("a",{class:"header-anchor",href:"#signal-unit","aria-hidden":"true"},"#"),e(" Signal Unit")],-1),De=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module integer_to_segment\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief raw data to segment encoder\n * @param DATA_WIDTH data width\n * @input data raw decimal data (4 bit)\n * @output seg_data bit data for cNodes\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" integer_to_segment\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" int_data"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" seg_data\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("int_data"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token punctuation"},"("),e("int_data"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"4'b0000"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11000000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 0"),e("\n "),a("span",{class:"token number"},"4'b0001"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11111001"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 1"),e("\n "),a("span",{class:"token number"},"4'b0010"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10100100"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 2"),e("\n "),a("span",{class:"token number"},"4'b0011"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10110000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 3"),e("\n "),a("span",{class:"token number"},"4'b0100"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10011001"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 4"),e("\n "),a("span",{class:"token number"},"4'b0101"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10010010"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 5"),e("\n "),a("span",{class:"token number"},"4'b0110"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10000010"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 6"),e("\n "),a("span",{class:"token number"},"4'b0111"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11111000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 7"),e("\n "),a("span",{class:"token number"},"4'b1000"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10000000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 8"),e("\n "),a("span",{class:"token number"},"4'b1001"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10010000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// 9"),e("\n "),a("span",{class:"token number"},"4'b1010"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10001000"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// a"),e("\n "),a("span",{class:"token number"},"4'b1011"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10000011"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// b"),e("\n "),a("span",{class:"token number"},"4'b1100"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11000110"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// c"),e("\n "),a("span",{class:"token number"},"4'b1101"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10100001"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// d"),e("\n "),a("span",{class:"token number"},"4'b1110"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10000110"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// e"),e("\n "),a("span",{class:"token number"},"4'b1111"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b10001110"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// f"),e("\n "),a("span",{class:"token keyword"},"default"),a("span",{class:"token punctuation"},":"),e(" seg_data "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"8'b11111111"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// off"),e("\n "),a("span",{class:"token keyword"},"endcase"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br")])],-1),Te=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module data_to_segment\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief raw data to segment encoder\n * @param DATA_WIDTH data width\n * @input data raw decimal data\n * @output seg_data bit data for cNodes\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" data_to_segment\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" data"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" seg_data\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans1 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans2 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans3 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"11"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"23"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans4 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"12"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"24"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans5 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"19"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"39"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans6 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"23"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"20"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"47"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"40"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans7 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"27"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"24"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"55"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"48"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n integer_to_segment trans8 "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"int_data"),a("span",{class:"token punctuation"},"("),e("data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"28"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"63"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"56"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br")])],-1),xe=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module led_unit\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief led display module (bind to aNodes and cNodes in FPGA)\n * @param DATA_WIDTH data width\n * @input clk_src clock signal (light different led on in round turn)\n * @input led_data raw decimal data\n * @output aNodes determine which led light on at now\n * @output cNodes determine how led light on (number)\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" led_unit\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk_src"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" led_data"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aNodes"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" cNodes\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" count"),a("span",{class:"token punctuation"},";"),e(),a("span",{class:"token comment"},"// 2^3 = 8"),e("\n "),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"*"),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" seg_data"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n aNodes "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n data_to_segment #"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"DATA_WIDTH"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),e(" data_to_segment "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),e("led_data"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"seg_data"),a("span",{class:"token punctuation"},"("),e("seg_data"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk_src"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"="),e(" count "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),e("count"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token punctuation"},"("),e("count"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"3'b000"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111110"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"7"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b001"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111101"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"15"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b010"),a("span",{class:"token punctuation"},":"),e(" "),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111011"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"23"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"16"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b011"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11110111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"24"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b100"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11101111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"39"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b101"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11011111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"47"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"40"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b110"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b10111111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"55"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"48"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token number"},"3'b111"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b01111111"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(" seg_data"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"63"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"56"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"default"),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token keyword"},"begin"),e("\n aNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111110"),a("span",{class:"token punctuation"},";"),e("\n cNodes "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"8'b11111111"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"endcase"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br")])],-1),Ie=a("h3",{id:"alu-unit"},[a("a",{class:"header-anchor",href:"#alu-unit","aria-hidden":"true"},"#"),e(" ALU Unit")],-1),Be=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module counter\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief up counter\n * @param DATA_WIDTH data width\n * @param STEP counting step\n * @input clk clock signal\n * @input rst reset signal\n * @output en enable signal\n * @output count counting value\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" counter\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),e(" STEP "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" rst"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" en"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" count\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("rst"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("en"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// counter"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br")])],-1),We=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module latch_counter\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief latch counter (latching when reaching max value)\n * @input clk clock signal\n * @input rst reset signal\n * @output en enable signal\n * @output count counting value\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" latch_counter\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" rst"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" en"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(" count\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token important"},"always @"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("rst"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),e(),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("en"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("count "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n count "),a("span",{class:"token operator"},"<="),e(" count"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// latch_counter"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br")])],-1),Ce=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module alu_flags\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief get flags after alu calculation\n * @param DATA_WIDTH data width\n * @input srcA A port data\n * @input srcB B port data\n * @input aluOP operation code\n * @output zero equal flag\n * @output of signed overflow flag\n * @output uof unsigned overflow flag\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" alu_flags\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" srcA"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" srcB"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aluOP"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" zero"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" of"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" uof\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" sum"),a("span",{class:"token punctuation"},","),e(" diff"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"wire"),e(" carry1"),a("span",{class:"token punctuation"},","),e(" carry2"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(),a("span",{class:"token operator"},"{"),e("carry1"),a("span",{class:"token punctuation"},","),e(" sum"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"="),e(" srcA "),a("span",{class:"token operator"},"+"),e(" srcB"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// awesome tip"),e("\n "),a("span",{class:"token keyword"},"assign"),e(),a("span",{class:"token operator"},"{"),e("carry2"),a("span",{class:"token punctuation"},","),e(" diff"),a("span",{class:"token operator"},"}"),e(),a("span",{class:"token operator"},"="),e(" srcA "),a("span",{class:"token operator"},"-"),e(" srcB"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// awesome tip"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" zero "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("srcA "),a("span",{class:"token operator"},"=="),e(" srcB"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" of "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("aluOP "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"4'd5"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"("),e("srcA"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" srcB"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(),a("span",{class:"token operator"},"~"),e("sum"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"~"),e("srcA"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(),a("span",{class:"token operator"},"~"),e("srcB"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" sum"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token punctuation"},"("),e("aluOP "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"4'd6"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"("),e("srcA"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(),a("span",{class:"token operator"},"~"),e("srcB"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(),a("span",{class:"token operator"},"~"),e("diff"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token operator"},"|"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"~"),e("srcA"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" srcB"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"&"),e(" diff"),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" uof "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("aluOP "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"4'd5"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token punctuation"},"("),e("carry1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token punctuation"},"("),e("aluOP "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token number"},"4'd6"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token punctuation"},"("),e("carry2"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// alu_flags"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br")])],-1),He=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module alu\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @param DATA_WIDTH data width\n * @input srcA A port data\n * @input srcB B port data\n * @input aluOP operation code\n * @output aluOut calculation result\n * @output zero equal flag\n * @output of signed overflow flag\n * @output uof unsigned overflow flag\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" alu\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" srcA"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" srcB"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aluOP"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" aluOut"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" zero"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" of"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(" uof\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token keyword"},"signed"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" signed_srcA"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"wire"),e(),a("span",{class:"token keyword"},"signed"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" signed_srcB"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" signed_srcA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$signed"),a("span",{class:"token punctuation"},"("),e("srcA"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" signed_srcB "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token property"},"$signed"),a("span",{class:"token punctuation"},"("),e("srcB"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),e(),a("span",{class:"token punctuation"},"("),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"case"),e(),a("span",{class:"token punctuation"},"("),e("aluOP"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token number"},"4'd0"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"<<"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd1"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" signed_srcA "),a("span",{class:"token operator"},">>>"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd2"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},">>"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd3"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"*"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd4"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"/"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd5"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"+"),e(" srcB"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// awesome tip"),e("\n "),a("span",{class:"token number"},"4'd6"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"-"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd7"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"&"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd8"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"|"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd9"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(" srcA "),a("span",{class:"token operator"},"^"),e(" srcB"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd10"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token operator"},"~"),a("span",{class:"token punctuation"},"("),e("srcA "),a("span",{class:"token operator"},"|"),e(" srcB"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd11"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token punctuation"},"("),e("signed_srcA "),a("span",{class:"token operator"},"<"),e(" signed_srcB"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token number"},"4'd12"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token punctuation"},"("),e("srcA "),a("span",{class:"token operator"},"<"),e(" srcB"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(),a("span",{class:"token number"},"1"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"default"),a("span",{class:"token punctuation"},":"),e(" aluOut "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"endcase"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n alu_flags #"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"DATA_WIDTH"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),e(" FLAGS "),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"srcA"),a("span",{class:"token punctuation"},"("),e("srcA"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"srcB"),a("span",{class:"token punctuation"},"("),e("srcB"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"aluOP"),a("span",{class:"token punctuation"},"("),e("aluOP"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"zero"),a("span",{class:"token punctuation"},"("),e("zero"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"of"),a("span",{class:"token punctuation"},"("),e("of"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"uof"),a("span",{class:"token punctuation"},"("),e("uof"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// alu"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br")])],-1),Ne=a("h3",{id:"memory-unit"},[a("a",{class:"header-anchor",href:"#memory-unit","aria-hidden":"true"},"#"),e(" Memory Unit")],-1),Oe=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module register\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief D flip flop\n * @param DATA_WIDTH data width\n * @input clk clock signal\n * @input rst reset signal\n * @input en enable signal\n * @input din data in\n * @output dout data out\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" register\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" rst"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" en"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" din"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" dout\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("rst"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n dout "),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// reset"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("en"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n dout "),a("span",{class:"token operator"},"<="),e(" din"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// update"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"begin"),e("\n dout "),a("span",{class:"token operator"},"<="),e(" dout"),a("span",{class:"token punctuation"},";"),e(" "),a("span",{class:"token comment"},"// hold"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// register"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br")])],-1),ze=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module regFile\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief register files for MIPS CPU, contains 32 D flip-flop registers\n * @param DATA_WIDTH data width\n * @input clk clock signal\n * @input we write enable signal\n * @input raddrA read address (No.register) for A out port\n * @input raddrB read address (No.register) for B out port\n * @input waddr write address (No.register) for wdata (in port)\n * @input wdata data to write into regFile\n * @output regA A port output\n * @output regB B port output\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" regFile\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" rst"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" we"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" raddrA"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" raddrB"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" waddr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" wdata"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" regA"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" regB"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" v0_data"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" a0_data\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token constant"},"`include"),e(),a("span",{class:"token string"},'"defines.vh"'),e("\n\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" i"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token comment"},"///< three ported regFile contains 32 registers"),e("\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" regFile "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("rst"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"for"),e(),a("span",{class:"token punctuation"},"("),e("i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<"),e(),a("span",{class:"token number"},"31"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"="),e(" i "),a("span",{class:"token operator"},"+"),e(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token keyword"},"begin"),e("\n regFile"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e(),a("span",{class:"token keyword"},"else"),e(),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("we "),a("span",{class:"token operator"},"&&"),e(" waddr "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n regFile"),a("span",{class:"token punctuation"},"["),e("waddr"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<="),e(" wdata"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" regA "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("we "),a("span",{class:"token operator"},"&&"),e(" waddr "),a("span",{class:"token operator"},"=="),e(" raddrA"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" wdata\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token punctuation"},"("),e("raddrA "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" regFile"),a("span",{class:"token punctuation"},"["),e("raddrA"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" regB "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"("),e("we "),a("span",{class:"token operator"},"&&"),e(" waddr "),a("span",{class:"token operator"},"=="),e(" raddrB"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" wdata\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token punctuation"},"("),e("raddrB "),a("span",{class:"token operator"},"!="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"?"),e(" regFile"),a("span",{class:"token punctuation"},"["),e("raddrB"),a("span",{class:"token punctuation"},"]"),e("\n "),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" v0_data "),a("span",{class:"token operator"},"="),e(" regFile"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"`V0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" a0_data "),a("span",{class:"token operator"},"="),e(" regFile"),a("span",{class:"token punctuation"},"["),a("span",{class:"token constant"},"`A0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// regFile"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br")])],-1),Se=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module imem\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief instruction cache memory (ROM)\n * @param DATA_WIDTH data width\n * @param BUS_WIDTH bus width\n * @param CODE_FILE MIPS assembly hexadecimal code file\n * @input addr memory address\n * @output rdata instruction read out from memory\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" imem\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},","),e(" BUS_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},","),e(" CODE_FILE"),a("span",{class:"token operator"},"="),e(),a("span",{class:"token string"},'"mips/benchmark.hex"'),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("BUS_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" addr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" rdata\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" ROM "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"**"),e("BUS_WIDTH"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"initial"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token property"},"$readmemh"),a("span",{class:"token punctuation"},"("),e("CODE_FILE"),a("span",{class:"token punctuation"},","),e(" ROM"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"**"),e("BUS_WIDTH"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" rdata "),a("span",{class:"token operator"},"="),e(" ROM"),a("span",{class:"token punctuation"},"["),e("addr"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// imem"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br")])],-1),Pe=a("div",{class:"language-verilog line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * @module dmem\n * @author sabertazimi\n * @email sabertazimi@gmail.com\n * @brief data cache memory (RAM)\n * @param DATA_WIDTH data width\n * @param BUS_WIDTH bus width\n * @input clk clock signal\n * @input re read enable signal\n * @input we write enable signal\n * @input addr memory address\n * @input wdata data write into memory\n * @output rdata data read out from memory\n */"),e("\n"),a("span",{class:"token keyword"},"module"),e(" dmem\n#"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"parameter"),e(" DATA_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"32"),a("span",{class:"token punctuation"},","),e(" BUS_WIDTH "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"("),e("\n "),a("span",{class:"token keyword"},"input"),e(" clk"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" re"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(" we"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("BUS_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" addr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" wdata"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"input"),e(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" switch_addr"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" rdata"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"output"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" led_data\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token keyword"},"reg"),e(),a("span",{class:"token punctuation"},"["),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},":"),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),e(" RAM "),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},":"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token operator"},"**"),e("BUS_WIDTH"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n "),a("span",{class:"token important"},"always @"),e(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"posedge"),e(" clk"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n "),a("span",{class:"token keyword"},"if"),e(),a("span",{class:"token punctuation"},"("),e("we"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token keyword"},"begin"),e("\n RAM"),a("span",{class:"token punctuation"},"["),e("addr"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"<="),e(" wdata"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n "),a("span",{class:"token keyword"},"end"),e("\n\n "),a("span",{class:"token keyword"},"assign"),e(" rdata "),a("span",{class:"token operator"},"="),e(" re "),a("span",{class:"token operator"},"?"),e(" RAM"),a("span",{class:"token punctuation"},"["),e("addr"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token punctuation"},":"),e(),a("span",{class:"token operator"},"{"),a("span",{class:"token punctuation"},"("),e("DATA_WIDTH"),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},"{"),a("span",{class:"token number"},"1'bx"),a("span",{class:"token operator"},"}}"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"assign"),e(" led_data "),a("span",{class:"token operator"},"="),e(" RAM"),a("span",{class:"token punctuation"},"["),e("switch_addr"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\n"),a("span",{class:"token keyword"},"endmodule"),e(),a("span",{class:"token comment"},"// dmem"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br")])],-1);o.render=function(a,e,t,o,Ue,Me){return n(),s("div",null,[l,c,p,u,r,i,k,b,m,d,g,w,y,h,f,v,_,A,D,T,x,I,B,W,C,H,N,O,z,S,P,U,M,E,L,R,F,$,q,V,G,X,K,j,Z,Y,Q,J,nn,sn,an,en,tn,on,ln,cn,pn,un,rn,kn,bn,mn,dn,gn,wn,yn,hn,fn,vn,_n,An,Dn,Tn,xn,In,Bn,Wn,Cn,Hn,Nn,On,zn,Sn,Pn,Un,Mn,En,Ln,Rn,Fn,$n,qn,Vn,Gn,Xn,Kn,jn,Zn,Yn,Qn,Jn,ns,ss,as,es,ts,os,ls,cs,ps,us,rs,is,ks,bs,ms,ds,gs,ws,ys,hs,fs,vs,_s,As,Ds,Ts,xs,Is,Bs,Ws,Cs,Hs,Ns,Os,zs,Ss,Ps,Us,Ms,Es,Ls,Rs,Fs,$s,qs,Vs,Gs,Xs,Ks,js,Zs,Ys,Qs,Js,na,sa,aa,ea,ta,oa,la,ca,pa,ua,ra,ia,ka,ba,ma,da,ga,wa,ya,ha,fa,va,_a,Aa,Da,Ta,xa,Ia,Ba,Wa,Ca,Ha,Na,Oa,za,Sa,Pa,Ua,Ma,Ea,La,Ra,Fa,$a,qa,Va,Ga,Xa,Ka,ja,Za,Ya,Qa,Ja,ne,se,ae,ee,te,oe,le,ce,pe,ue,re,ie,ke,be,me,de,ge,we,ye,he,fe,ve,_e,Ae,De,Te,xe,Ie,Be,We,Ce,He,Ne,Oe,ze,Se,Pe])};export default o;export{t as __pageData}; diff --git a/assets/programming_android_androidBasicNotes.md.4d20a147.js b/assets/programming_android_androidBasicNotes.md.4d20a147.js new file mode 100644 index 00000000000..17a8a2b9cc5 --- /dev/null +++ b/assets/programming_android_androidBasicNotes.md.4d20a147.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"Android Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Android Framework","slug":"android-framework"},{"level":3,"title":"Dalvik virtual machine","slug":"dalvik-virtual-machine"},{"level":3,"title":"Basic Building Blocks","slug":"basic-building-blocks"},{"level":2,"title":"Android Studio","slug":"android-studio"},{"level":3,"title":"Plugins","slug":"plugins"},{"level":2,"title":"API Conventions","slug":"api-conventions"},{"level":3,"title":"Manager Service","slug":"manager-service"},{"level":2,"title":"Activity","slug":"activity"},{"level":3,"title":"Base Activity","slug":"base-activity"},{"level":3,"title":"Activity Collector","slug":"activity-collector"},{"level":3,"title":"Start Activity","slug":"start-activity"},{"level":2,"title":"UI Design","slug":"ui-design"},{"level":3,"title":"Layout","slug":"layout"},{"level":3,"title":"Component","slug":"component"},{"level":3,"title":"Drawable","slug":"drawable"},{"level":2,"title":"Fragment","slug":"fragment"},{"level":3,"title":"Basic Fragment","slug":"basic-fragment"},{"level":3,"title":"Transfer Information","slug":"transfer-information"},{"level":3,"title":"Runtime Loop","slug":"runtime-loop"},{"level":2,"title":"Broadcast","slug":"broadcast"},{"level":3,"title":"Register Receiver","slug":"register-receiver"},{"level":3,"title":"Custom Broadcast","slug":"custom-broadcast"},{"level":3,"title":"Local Broadcast","slug":"local-broadcast"},{"level":2,"title":"Data Store","slug":"data-store"},{"level":3,"title":"Files Store","slug":"files-store"},{"level":3,"title":"Shared Preferences","slug":"shared-preferences"},{"level":3,"title":"DataBase","slug":"database"},{"level":2,"title":"Content Provider","slug":"content-provider"},{"level":3,"title":"Read Other App Content","slug":"read-other-app-content"},{"level":3,"title":"Provide App Content","slug":"provide-app-content"},{"level":2,"title":"Service","slug":"service"},{"level":3,"title":"Handler","slug":"handler"},{"level":3,"title":"Async Task","slug":"async-task"},{"level":3,"title":"Basic Service","slug":"basic-service"},{"level":3,"title":"ForeGround Service","slug":"foreground-service"},{"level":3,"title":"Intent Service","slug":"intent-service"},{"level":3,"title":"Alarm Service","slug":"alarm-service"},{"level":2,"title":"Media","slug":"media"},{"level":3,"title":"Notification","slug":"notification"},{"level":3,"title":"SMS","slug":"sms"},{"level":3,"title":"Audio","slug":"audio"},{"level":3,"title":"Music","slug":"music"},{"level":2,"title":"NetWork","slug":"network"},{"level":3,"title":"WebView","slug":"webview"},{"level":3,"title":"HTTP URL Connection","slug":"http-url-connection"},{"level":3,"title":"HTTP Client","slug":"http-client"},{"level":3,"title":"XML","slug":"xml"},{"level":3,"title":"JSON","slug":"json"},{"level":3,"title":"Network Best Practice","slug":"network-best-practice"},{"level":2,"title":"Map","slug":"map"},{"level":3,"title":"Location","slug":"location"},{"level":2,"title":"Sensor","slug":"sensor"},{"level":3,"title":"Light Sensor","slug":"light-sensor"},{"level":3,"title":"Accelerometer Sensor","slug":"accelerometer-sensor"},{"level":3,"title":"Orientation Sensor","slug":"orientation-sensor"},{"level":2,"title":"Best Practice","slug":"best-practice"},{"level":3,"title":"Global Context","slug":"global-context"},{"level":3,"title":"用 Intent 传递对象","slug":"用-intent-传递对象"},{"level":3,"title":"Custom Logger","slug":"custom-logger"}],"relativePath":"programming/android/androidBasicNotes.md","lastUpdated":1627196177000}',t={},e=a('

Android Basic Notes

Android Framework

Dalvik virtual machine

  1. register-based machine 基于寄存器(不写入内存)
  2. minimizing instruction dispatch and memory accesses 最小化指令分配黑内存访问
  3. giving more efficient instruction stream(a lot more semantic content) 提供更加高效的指令流

Basic Building Blocks

  1. Activity(Managed by activity stack)
  2. Service(Running in the background;with no UI)
  3. Broadcast Receiver(Can invoke(调用) activity;with no UI)
  4. Content Provider(accessing and managing application data)

Android Studio

Plugins

Code generator

  • Constructor
  • getter/setter
  • ViewHolder
  • Parcelable Implementation
  • GsonFormat : 根据 JSONObject 生成相应类

API Conventions

Manager Service

  • PreferenceManager.getDefaultSharedPreferences
  • LocalBroadcastManager.getInstance

Activity

Base Activity

查看当前界面属于哪个 Activity,自定义 Activity 继承 BaseActivity

public class BaseActivity extends Activity {\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        Log.d("BaseActivity", getClass().getSimpleName());\n    }\n}\n
1
2
3
4
5
6
7

Activity Collector

在所有 Activity 的 onCreate 方法调用静态的 addActivity 方法,onDestroy 方法调用静态的 removeActivity 方法。

  • ActivityCollector.addActivity(this);
  • ActivityCollector.removeActivity(this);
public class ActivityCollector {\n\n    public static List<Activity> activities = new ArrayList<Activity>();\n\n    public static void addActivity(Activity activity) {\n        activities.add(activity);\n\n    }\n\n    public static void removeActivity(Activity activity) {\n        activities.remove(activity);\n\n    }\n\n    public static void finishAll() {\n        for (Activity activity : activities) {\n            if (!activity.isFinishing()) {\n                activity.finish();\n            }\n        }\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Start Activity

为每个 Activity 添加静态的 actionStart 方法,供其他 Activity 启用此 Activity

public static void actionStart(Context context, String data1, String data2) {\n    Intent intent = new Intent(context, thisActivity.class);\n    intent.putExtra("param1", data1);\n    intent.putExtra("param2", data2);\n    context.startActivity(intent);\n}\n
1
2
3
4
5
6

UI Design

N activities can respond to a particular intent:

Android will pop(弹出) up a little dialogue list(对话框) to user showing application icon defining the intent

当有多个活动可相应某个特定意图时,系统将会弹出对话框提示用户选择一个应用的活动或者设定默认值(default)

e.g web browsers

<intent-filter>\n  <action android:name="android.intent.action.MAIN" />\n  <category android:name="android.intent.category.LAUNCHER" />\n</intent-filter>\n
1
2
3
4

Layout

Basic

  • android:layout_weight 自适配布局
  • android:SingleLine 单行显示模式
  • android:ellipsize="end" 文字过多时缩略方式

Table Layout

  • <TableLayout android:stretchColumns="1"> 拉伸第 2 列
  • android:layout_span="2" 占 2 列

Custom Layout

LayoutInflater 作用是将 layout 的 xml 布局文件实例化为 View 类对象。

View view = LayoutInflater.from(context).inflate(R.layout.title, this/null);\n
1

Component

View(ViewGroup): e.g button、textbox(文本框)、checkbox(复选框)

Custom Component

custom XML

title.xml

<LinearLayout\n  xmlns:android="http://schemas.android.com/apk/res/android"\n  android:layout_width="match_parent"\n  android:layout_height="wrap_content"\n  android:background="@drawable/title_bg"\n>\n  <button\n    android:id="@+id/title_back"\n    android:layout_width="wrap_content"\n    android:layout_height="wrap_content"\n    android:layout_gravity="center"\n    android:layout_margin="5dip"\n    android:background="@drawable/back_bg"\n    android:text="Back"\n    android:textColor="#fff"\n  />\n  <TextView\n    android:id="@+id/title_text"\n    android:layout_width="0dip"\n    android:layout_height="wrap_content"\n    android:layout_gravity="center"\n    android:layout_weight="1"\n    android:gravity="center"\n    android:text="Title Text"\n    android:textColor="#fff"\n    android:textSize="24sp"\n  />\n  <button\n    android:id="@+id/title_edit"\n    android:layout_width="wrap_content"\n    android:layout_height="wrap_content"\n    android:layout_gravity="center"\n    android:layout_margin="5dip"\n    android:background="@drawable/edit_bg"\n    android:text="Edit"\n    android:textColor="#fff"\n  />\n</LinearLayout>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

custom class

public class TitleLayout extends LinearLayout {\n    public TitleLayout(Context context, AttributeSet attrs) {\n        super(context, attrs);\n        LayoutInflater.from(context).inflate(R.layout.title, this);\n\n        //Register button click Listener\n        titleBack.setOnClickListener(new OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                ((Activity) getContext()).finish();\n            }\n        });\n        titleEdit.setOnClickListener(new OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();\n            }\n        });\n\n        //other awesome things\n        //like material design ripple effect\n        //animations and music\n    }\n\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

AlertDialog

//builder pattern\nAlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);\n\ndialogBuilder.setTitle("Warning");\ndialogBuilder.setMessage("You are forced to be offline. Please try to login again.");\ndialogBuilder.setCancelable(false);\ndialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {\n    @Override\n    public void onClick(DialogInterface dialog, int which) {\n        ActivityCollector.finishAll(); // 销毁所有活动\n        Intent intent = new Intent(context, LoginActivity.class);\n        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\n        context.startActivity(intent); // 重新启动LoginActivity\n    }\n});\n\nAlertDialog alertDialog = dialogBuilder.create();\n\n// 需要设置AlertDialog的类型,保证在广播接收器中可以正常弹出\nalertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);\n\nalertDialog.show();\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

List View

ArrayAdapter<String> adapter = new ArrayAdapter<String>(\n    MainActivity.this, android.R.layout.simple_list_item_1, data\n);\nlistView.setAdapter(adapter);\n
1
2
3
4
Custom List View Layout
  • Custom class
  • Custom Sub Xml(单项)
  • Custom ArrayAdapter
    • 重写构造函数
    • 重写 getView 方法
      • 重用 convertView 提升性能
      • ViewHolder 提升性能
\n//内部类,其中字段与自定义class的字段一致\nclass ViewHolder {\n    ImageView fruitImage;\n    TextView fruitName;\n}\npublic FruitAdapter(\n  Context context,\n  int textViewResourceId,\n  List<Fruit> objects) {\n    super(context, textViewResourceId, objects);\n    resourceId = textViewResourceId;\n}\n\n@Override\npublic View getView(int position, View convertView, ViewGroup parent) {\n\n    Fruit fruit = getItem(position); // 获取当前项的Fruit实例\n    View view;\n    ViewHolder viewHolder;\n\n    //大幅提升性能\n    if (convertView == null) {\n        view = LayoutInflater.from(getContext()).inflate(resourceId, null);\n\n        viewHolder = new ViewHolder();\n        viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);\n        viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);\n        view.setTag(viewHolder); // 将ViewHolder存储在View中\n\n    } else {\n        view = convertView;\n        viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder\n    }\n\n    ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);\n    TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);\n    fruitImage.setImageResource(fruit.getImageId());\n    fruitName.setText(fruit.getName());\n    return view;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Custom List View Listener
listView.setOnItemClickListener(new OnItemClickListener() {\n    @Override\n    public void onItemClick(\n      AdapterView<?> parent,\n      View view,\n      int position,\n      long id) {\n        Fruit fruit = fruitList.get(position);\n        Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();\n    }\n});\n\nadapter.notifyDataSetChanged();           // 当有新消息时,刷新ListView中的显示\nmsgListView.setSelection(msgList.size()); // 将ListView定位到最后一行\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Drawable

修改特定组件的背景颜色

Resources myColor = getBaseContext().getResources();\n// getBaseContext()获得基础Context\n// getResources()获得资源\nDrawable color_M = myColor.getDrawable(R.color. lightGreen);\n// 由资源 myColor来获得Drawable\n// R.color.lightGreen是颜色值的ID引用\ntext.setBackgroundDrawable(color_M);\n//设置背景\n
1
2
3
4
5
6
7
8

Fragment

android.app.Fragment

Basic Fragment

Activity XML

<fragment android:id="@+id/right_fragment" <!-- custom fragment class -->\n  android:name="com.example.fragmentTest.RightFragment"\n  android:layout_width="0dp" android:layout_height="match_parent"\n  android:layout_weight="1" /></fragment\n>\n
1
2
3
4
5

Create View in Fragment

@Override\npublic View onCreateView(\n  LayoutInflater inflater,\n  ViewGroup container,\n  Bundle savedInstanceState) {\n    View view = inflater.inflate(R.layout.left_fragment, container, false);\n    return view;\n}\n
1
2
3
4
5
6
7
8

Add Fragment in Activity

AnotherRightFragment fragment = new AnotherRightFragment();\nFragmentTransaction transaction = getFragmentManager.beginTransaction();\n\n//容器的 id 和待添加的碎片实例\ntransaction.replace(R.id.right_layout, fragment);\n//模拟返回栈\ntransaction.addToBackStack(null);\ntransaction.commit();\n
1
2
3
4
5
6
7
8

Transfer Information

In Activity

getFragmentManager().findFragmentById(R.id.right_fragment);

In Fragment

MainActivity activity = (MainActivity) getActivity();

Runtime Loop

Basic Override Function

  • onAttach() 当碎片和活动建立关联的时候调用。
  • onCreateView() 为碎片创建视图(加载布局)时调用。
  • onActivityCreated() 确保与碎片相关联的活动一定已经创建完毕的时候调用。
  • onDestroyView() 当与碎片关联的视图被移除的时候调用。
  • onDetach() 当碎片和活动解除关联的时候调用。

Broadcast

  • Normal Broadcasts : async
  • Ordered Broadcasts : sync

Register Receiver

In Activity

//Custom BroadcastReceiver,Override onReceive methods\n//intentFilter : action\n//前为响应后的行为,后为响应何种广播\nregisterReceiver(networkChangeReceiver, intentFilter);\n\n//in onDestroy\nunregisterReceiver();\n
1
2
3
4
5
6
7

In AndroidManifest,xml

<!-- custom receiver class -->\n<receiver android:name=".MyBroadcastReceiver">\n  <!-- receiver priority -->\n  <intent-filter android:priority="100">\n    <!-- custom broadcast -->\n    <action android:name="com.example.broadcastTest. MY_BROADCAST" />\n  </intent-filter>\n</receiver>\n
1
2
3
4
5
6
7
8

Custom Broadcast

Normal Broadcast

intent intent = new Intent("com.example.broadcastTest.MY_BROADCAST");\nsendBroadcast(intent);\n
1
2

Ordered Broadcast

intent intent = new Intent("com.example.broadcastTest.MY_BROADCAST");\nsendOrderedBroadcast(intent, null);\n
1
2

Local Broadcast

// 获取实例\nlocalBroadcastManager = LocalBroadcastManager.getInstance(this);\n
1
2
localBroadcastManager.sendBroadcast(intent); // 发送本地广播\n
1

Local Receiver

localBroadcastManager.registerReceiver(CustomReceiver, intentFilter);\nlocalBroadcastManager.unregisterReceiver(CustomReceiver);\n
1
2

Data Store

Files Store

/data/data/<packageName>/files/

Write

String data = "Data to save";\nFileOutputStream out = null;\nBufferedWriter writer = null;\ntry {\n    out = openFileOutput("data", Context.MODE_PRIVATE);\n    writer = new BufferedWriter(new OutputStreamWriter(out));\n    writer.write(data);\n} catch (IOException e) {\n    e.printStackTrace();\n} finally {\n    try {\n        if (writer != null) {\n            writer.close();\n        }\n    } catch (IOException e) {\n        e.printStackTrace();\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Read

FileInputStream in = null;\nBufferedReader reader = null;\nStringBuilder content = new StringBuilder();\ntry {\n    in = openFileInput("data");\n    reader = new BufferedReader(new InputStreamReader(in));\n    String line = "";\n    while ((line = reader.readLine()) != null) {\n        content.append(line);\n    }\n} catch (IOException e) {\n    e.printStackTrace();\n} finally {\n    if (reader != null) {\n        try {\n            reader.close();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n\nreturn content.toString();\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Shared Preferences

/data/data/<packageName>/shared_preferences/

Write

//get Editor\nSharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();\n//store date\neditor.putString("name", "Tom");\neditor.putInt("age", 28);\neditor.putBoolean("married", false);\n//commit\neditor.commit();\n
1
2
3
4
5
6
7
8
editor.clear();\n
1

clear pref file content

Read

SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);\n//second argument - default value if target key don't exists\nString name = pref.getString("name", "");\nint age = pref.getInt("age", 0);\nboolean married = pref.getBoolean("married", false);\n
1
2
3
4
5

DataBase

/data/data/<package name>/databases/

SQLite Open Helper

@Override\nonCreate()\n@Override\npublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n    switch (oldVersion) {\n        case 1:\n            db.execSQL(CREATE_CATEGORY);\n        case 2:\n            db.execSQL("alter table Book add column category_id integer");\n        default:\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12

Custom 实现创建、升级数据库的逻辑 构建出 SQLiteOpenHelper 的实例后,再调用getReadableDatabase()getWritableDatabase()方法创建数据库

establish table
create table Book (\n    id integer primary key autoIncrement,\n    author text,\n    price real,\n    pages integer,\n    name text\n    )\n
1
2
3
4
5
6
7

Basic Operator

  • 创建一个新的数据库并返回一个 SQLiteDatabase 对象
Context.createDatabase(String name,int version ,int mode,CursorFactory factory);\n
1
  • 删除数据库
this.deleteDatabase("myDatabase.db");\n
1
  • 打开数据库
SQLiteDatabase my_DataBase =\nthis.openOrCreateDatabase("myDateBase.db",MODE_PRIVATE , null);\nmy_DataBase.close();\n
1
2
3
  • 非查询 SQL 指令
//创建一个名为"test"并带两个参数的表\nmy_DataBase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY,\nsomeNumber INTEGER);");\n//在数据库中插入一个元组\nmy_DataBase.execSQL("INSERT INTO test (_id,someNumber) values(1,8);");\n\nSQLiteDatabase db = dbHelper.getWritableDatabase();\n\n//INSERT\nContentValues values = new ContentValues();\n// 开始组装第一条数据\nvalues.put("name", "The Da Vinci Code");\nvalues.put("author", "Dan Brown");\nvalues.put("pages", 454);\nvalues.put("price", 16.96);\ndb.insert("Book", null, values); // 插入第一条数据\nvalues.clear();\n// 开始组装第二条数据\nvalues.put("name", "The Lost Symbol");\nvalues.put("author", "Dan Brown");\nvalues.put("pages", 510);\nvalues.put("price", 19.95);\ndb.insert("Book", null, values); // 插入第二条数据\n\n//update\ncontentValues values = new ContentValues();\nvalues.put("price", 10.99);\ndb.update("Book", values, "name = ?", new String[] { "The DaVinci Code"  });\n\n//delete\ndb.delete("Book", "pages > ?", new String[] { "500"  });\n\n//删除表\nmy_DataBase.execSQL("DROP TABLE test");\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  • 查询 SQL 指令-游标 Cursors
    • query()
方法参数对应 SQL 部分描述
tablefrom tableName指定查询的表名
columnsselect column1, column2指定查询的列名
selectionwhere column = value指定 where 的约束条件
selectionArgs-为 where 中的占位符提供具体的值
groupBygroup by column指定需要 group by 的列
havinghaving column = value对 group by 后的结果进一步约束
orderByorder by column1, column2指定查询结果的排序方式
SQLiteDatabase db = dbHelper.getWritableDatabase();\n// 查询Book表中所有的数据\nCursor cursor = db.query("Book", null, null, null, null, null, null);\n// 遍历Cursor对象,取出数据\n//cursor.moveToFirst()\n//cursor.moveToNext()\nString name = cursor.getString(cursor.getColumnIndex("name"));\nString author = cursor.getString(cursor.getColumnIndex("author"));\nint pages = cursor.getInt(cursor.getColumnIndex("pages"));\ndouble price = cursor.getDouble(cursor.getColumnIndex("price"));\n
1
2
3
4
5
6
7
8
9
10
//为了创建一个Cursor(游标),必须执行一个查询,要么通过SQL使用rawQuery()方法\n//或是更精心设计的方法,像query()方法\nCursor cur = my_DataBase.rawQuery("SELECT * FORM test", null);\n\nif(cur!=null) {//游标不为空\n    //返回给定名称的列的基于0开始的index,如果该属性列不存在则返回-1\n    //通过它们的index来检索属性值\n    int numColumn=cur.getColumnIndex("someNumber");\n\n    if(cur.moveToFirst()) {\n        //cur.moveToFirst()让游标指向第一行,如果游标指向第一行,则返回true\n        do {\n        int num=cur.getInt(numColumn);//获得当前行该属性的值\n        /*Cursor提供了不同的方法来回索不同的数据类型\n        例如getInt(int index)/getString(int index)等等*/\n        /*做一些事情*/\n        } while (cur.moveToNext());\n        /*游标移动到下一行,如果游标已经通过了结果集中的最后,\n        即没有行可以移动时,则返回false*/\n        //其他可能移动的是 previous() 和first()方法\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Transaction

  • SQLiteDatabase 的beginTransaction()方法
  • 调用setTransactionSuccessful()表示事务已经执行成功
  • finally 代码块中调用endTransaction()来结束事务

Content Provider

Read Other App Content

Uri uri = Uri.parse("content://com.example.app.provider/table1");\ngetContentResolver().query/insert/delete/update();\n
1
2
  • ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
  • ContactsContract.CommonDataKinds.Phone.NUMBER

Provide App Content

With ContentProvider:

public class MyProvider extends ContentProvider {\n    @Override\n    public boolean onCreate() {\n        return false;\n    }\n    @Override\n    public Cursor query(Uri uri, String[] projection, String selection,\n      String[] selectionArgs, String sortOrder) {\n        return null;\n    }\n    @Override\n    public Uri insert(Uri uri, ContentValues values) {\n        return null;\n    }\n    @Override\n    public int update(Uri uri, ContentValues values, String selection,\n      String[] selectionArgs) {\n        return 0;\n    }\n    @Override\n    public int delete(Uri uri, String selection, String[] selectionArgs) {\n        return 0;\n    }\n    @Override\n    public String getType(Uri uri) {\n        return null;\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
UriMatcher.addURI(uri, customNumber)/.match(uri)\n
1
  • 为传入 URI 指定自定义常量作为代号

Service

Handler

//在主线程重写handleMessage,更新UI\nnew Handler() {\n    public void handleMessage(Message msg) {\n        switch (msg.what) {\n            case UPDATE_TEXT:\n            // 在这里可以进行UI操作\n            text.setText("Nice to meet you");\n            break;\n            default:\n            break;\n        }\n    }\n}\n\n//在后台执行子线程\nnew Thread(new Runnable() {\n    @Override\n    public void run() {\n        Message message = new Message();\n        message.what = UPDATE_TEXT;\n        handler.sendMessage(message); // 将Message对象发送出去\n    }\n}).start();\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Async Task

AsyncTask 中的几个方法才能完成对任务的定制。经常需要去重写的方法 有以下四个:

  1. onPreExecute() 这个方法会在后台任务开始执行之前调用,用于进行一些界面上的初始化操作, 比如显示一个进度条对话框等。
  2. doInBackground(Params...) 这个方法中的所有代码都会在子线程中运行,我们应该在这里去处理所有的耗时任务。 任务一旦完成就可以通过 return 语句来将任务的执行结果返回,如果 AsyncTask 的 第三个泛型参数指定的是 Void,就可以不返回任务执行结果。注意,在这个方法中是不 可以进行 UI 操作的,如果需要更新 UI 元素,比如说反馈当前任务的执行进度, 可以调用 publishProgress(Progress...)方法来完成。
  3. onProgressUpdate(Progress...) 当在后台任务中调用了 publishProgress(Progress...)方法后,这个方法就会很快被调用, 方法中携带的参数就是在后台任务中传递过来的。在这个方法中可以对 UI 进行操作, 利用参数中的数值就可以对界面元素进行相应地更新。
  4. onPostExecute(Result) 当后台任务执行完毕并通过 return 语句进行返回时,这个方法就很快会被调用。 返回的数据会作为参数传递到此方法中,可以利用返回的数据来进行一些 UI 操作, 比如说提醒任务执行的结果,以及关闭掉进度条对话框等。
class DownloadTask extends AsyncTask<Void, Integer, Boolean> {\n    @Override\n    protected void onPreExecute() {\n        progressDialog.show(); // 显示进度对话框\n    }\n    @Override\n    protected Boolean doInBackground(Void... params) {\n        try {\n            while (true) {\n                int downloadPercent = doDownload(); // 这是一个虚构的方法\n                publishProgress(downloadPercent);\n            if (downloadPercent >= 100) {\n                break;\n                }\n            }\n        } catch (Exception e) {\n            return false;\n        }\n    return true;\n}\n    @Override\n    protected void onProgressUpdate(Integer... values) {\n        // 在这里更新下载进度\n        progressDialog.setMessage("Downloaded " + values[0] + "%");\n    }\n    @Override\n    protected void onPostExecute(Boolean result) {\n        progressDialog.dismiss(); // 关闭进度对话框\n        // 在这里提示下载结果\n        if (result) {\n            Toast.makeText(context, "Download succeeded",\n            Toast.LENGTH_SHORT).show();\n        } else {\n            Toast.makeText(context, " Download failed",\n            Toast.LENGTH_SHORT).show();\n        }\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

Basic Service

//in AndroidManifest.xml <service android:name=".MyService"> </service>\n
1
stopSelf()\n
1
public class MyService extends Service {\n    @Override\n    public IBinder onBind(Intent intent) {\n        return null;\n    }\n    @Override\n        public void onCreate() {\n        super.onCreate();\n    }\n    @Override\n    public int onStartCommand(Intent intent, int flags, int startId) {\n        return super.onStartCommand(intent, flags, startId);\n    }\n    @Override\n    public void onDestroy() {\n        super.onDestroy();\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

IBinder

  • In Custom Service class
private DownloadBinder mBinder = new DownloadBinder();\n\nclass DownloadBinder extends Binder {\n    public void startDownload() {\n        Log.d("MyService", "startDownload executed");\n    }\n    public int getProgress() {\n        Log.d("MyService", "getProgress executed");\n        return 0;\n    }\n}\n\n@Override\npublic IBinder onBind(Intent intent) {\n    return mBinder;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • In Activity class
case R.id.bind_service:\n    Intent bindIntent = new Intent(this, MyService.class);\n    bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务\n    break;\ncase R.id.unbind_service:\n    unbindService(connection); // 解绑服务\n    break;\ndefault:\n    break;\n\n@Override\npublic void onServiceDisconnected(ComponentName name) {\n    }\n@Override\npublic void onServiceConnected(ComponentName name, IBinder service) {\n    downloadBinder = (MyService.DownloadBinder) service;\n    downloadBinder.startDownload();\n    downloadBinder.getProgress();\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

ForeGround Service

  • In service onCreate
Notification notification = new Notification(R.drawable.ic_launcher,\n  "Notification comes", System. currentTimeMills());\nIntent notificationIntent = new Intent(this, MainActivity.class);\nPendingIntent pendingIntent = PendingIntent.getActivity(this, 0,\n  notificationIntent, 0);\nnotification.setLatestEventInfo(this, "This is title", "This is content", pendingIntent);\nstartForeground(1, notification);\n
1
2
3
4
5
6
7

Intent Service

public class MyIntentService extends IntentService {\n    public MyIntentService() {\n        super("MyIntentService"); // 调用父类的有参构造函数\n    }\n    @Override\n    protected void onHandleIntent(Intent intent) {\n        // 打印当前线程的id\n        Log.d("MyIntentService", "Thread id is " + Thread.currentThread().getId());\n    }\n    @Override\n    public void onDestroy() {\n        super.onDestroy();\n        Log.d("MyIntentService", "onDestroy executed");\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Alarm Service

结合 BroadcastReceiver 可以实现定时任务

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);\nlong triggerAtTime = SystemClock.elapsedRealtime() + 10 * 1000;\nmanager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pendingIntent);\n
1
2
3
  • Service 延时发出广播
  • BroadcastReceiver 接受广播后再次启动 Service
public int onStartCommand(Intent intent, int flags, int startId) {\n    new Thread(new Runnable() {\n        @Override\n        public void run() {\n            Log.d("LongRunningService", "executed at " + new Date().toString());\n        }\n    }).start();\n\n    AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);\n    int anHour = 60 * 60 * 1000; // 这是一小时的毫秒数\n    long triggerAtTime = SystemClock.elapsedRealtime() + anHour;\n\n    //关键:在服务里发送广播\n    Intent i = new Intent(this, AlarmReceiver.class);\n    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);\n\n    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);\n\n    return super.onStartCommand(intent, flags, startId);\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Media

Notification

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);\n\nNotification notification = new Notification(R.drawable.ic_launcher, "This is\n  ticker text", System.currentTimeMills());\n\nIntent intent = new Intent(this, NotificationActivity.class);\nPendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);\n\nnotification.setLatestEventInfo(this, "This is content title", "This is\n  content text", pi);\n\nmanager.notify(1, notification);\n//在被启动Activity manager.cancel(1);\n
1
2
3
4
5
6
7
8
9
10
11
12
13

SMS

Audio

Music

NetWork

Networked Apps

  1. Network latency(网络延迟)——UI thread separated from data loading thread
  2. Battery drain(电池耗尽)
  3. Intermittent service(中断服务)

WebView

<uses-permission android:name="android.permission.INTERNET" />\n
1

Three Steps

webView.getSettings().setJavaScriptEnabled(true);\n\nwebView.setWebViewClient(new WebViewClient() {\n    @Override\n    public boolean shouldOverrideUrlLoading(WebView view, String url) {\n        view.loadUrl(url); // 根据传入的参数再去加载新的网页\n        return true; // 表示当前WebView可以处理打开新网页的请求,不用借助系统浏览器\n    }\n});\n\nwebView.loadUrl("http://www.github.com");\n
1
2
3
4
5
6
7
8
9
10
11

HTTP URL Connection

HTTP Client

XML

Pull

HttpClient httpClient = new DefaultHttpClient();\n\n// 指定访问的服务器地址是电脑本机\nHttpGet httpGet = new HttpGet("http://10.0.2.2/get_data.xml");\nHttpResponse httpResponse = httpClient.execute(httpGet);\n\nif (httpResponse.getStatusLine().getStatusCode() == 200) {\n    // 请求和响应都成功了\n    HttpEntity entity = httpResponse.getEntity();\n    String response = EntityUtils.toString(entity,"utf-8");\n\n    //XML Pull 方式解析\n    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();\n    XmlPullParser xmlPullParser = factory.newPullParser();\n    xmlPullParser.setInput(new StringReader(xmlData));\n    int eventType = xmlPullParser.getEventType();\n\n    String id = "";\n    String name = "";\n    String version = "";\n\n    while (eventType != XmlPullParser.END_DOCUMENT) {\n\n        String nodeName = xmlPullParser.getName();\n\n        switch (eventType) {\n        // 开始解析某个结点\n        case XmlPullParser.START_TAG: {\n            if ("id".equals(nodeName)) {\n                id = xmlPullParser.nextText();\n            } else if ("name".equals(nodeName)) {\n                name = xmlPullParser.nextText();\n            } else if ("version".equals(nodeName)) {\n               version = xmlPullParser.nextText();\n            }\n            break;\n        }\n        // 完成解析某个结点\n        case XmlPullParser.END_TAG: {\n            if ("app".equals(nodeName)) {\n                Log.d("MainActivity", "id is " + id);\n                Log.d("MainActivity", "name is " + name);\n                Log.d("MainActivity", "version is " + version);\n            }\n            break;\n        }\n        default:\n            break;\n        }\n\n        eventType = xmlPullParser.next();\n    } // end of while\n}  //  end of if\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

SAX

Default Handler
public class ContentHandler extends DefaultHandler {\n    private String nodeName;\n    private StringBuilder id;\n    private StringBuilder name;\n    private StringBuilder version;\n\n    @Override\n    public void startDocument() throws SAXException {\n        id = new StringBuilder();\n        name = new StringBuilder();\n        version = new StringBuilder();\n    }\n    @Override\n    public void startElement(String uri, String localName, String qName,\n      Attributes attributes) throws SAXException {\n        // 记录当前结点名\n        nodeName = localName;\n    }\n    @Override\n    public void characters(\n      char[] ch,\n      int start,\n      int length) throws SAXException {\n        // 根据当前的结点名判断将内容添加到哪一个StringBuilder对象中\n        if ("id".equals(nodeName)) {\n            id.append(ch, start, length);\n        } else if ("name".equals(nodeName)) {\n            name.append(ch, start, length);\n        } else if ("version".equals(nodeName)) {\n            version.append(ch, start, length);\n        }\n    }\n    @Override\n    public void endElement(\n      String uri,\n      String localName,\n      String qName) throws SAXException {\n        if ("app".equals(localName)) {\n            Log.d("ContentHandler", "id is " + id.toString().trim());\n            Log.d("ContentHandler", "name is " + name.toString().trim());\n            Log.d("ContentHandler", "version is " + version.toString().trim());\n            // 最后要将StringBuilder清空掉\n            id.setLength(0);\n            name.setLength(0);\n            version.setLength(0);\n        }\n    }\n    @Override\n    public void endDocument() throws SAXException {\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
HttpClient httpClient = new DefaultHttpClient();\n// 指定访问的服务器地址是电脑本机\nHttpGet httpGet = new HttpGet("http://10.0.2.2:8080/get_data.xml");\nHttpResponse httpResponse = httpClient.execute(httpGet);\n\nif (httpResponse.getStatusLine().getStatusCode() == 200) {\n    // 请求和响应都成功了\n    HttpEntity entity = httpResponse.getEntity();\n    String response = EntityUtils.toString(entity, "utf-8");\n\n    SAXParserFactory factory = SAXParserFactory.newInstance();\n    XMLReader xmlReader = factory.newSAXParser().getXMLReader();\n    ContentHandler handler = new ContentHandler();\n\n    // 将ContentHandler的实例设置到XMLReader中\n    xmlReader.setContentHandler(handler);\n    // 开始执行解析\n    xmlReader.parse(new InputSource(new StringReader(xmlData)));\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

JSON

JSON Object

HttpClient httpClient = new DefaultHttpClient();\n// 指定访问的服务器地址是电脑本机\nHttpGet httpGet = new HttpGet("http://10.0.2.2/get_data.json");\nHttpResponse httpResponse = httpClient.execute(httpGet);\n\nif (httpResponse.getStatusLine().getStatusCode() == 200) {\n    // 请求和响应都成功了\n    HttpEntity entity = httpResponse.getEntity();\n    String response = EntityUtils.toString(entity, "utf-8");\n\n    JSONArray jsonArray = new JSONArray(response);\n\n    for (int i = 0; i < jsonArray.length(); i++) {\n        JSONObject jsonObject = jsonArray.getJSONObject(i);\n        String id = jsonObject.getString("id");\n        String name = jsonObject.getString("name");\n        String version = jsonObject.getString("version");\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

GSON

HttpClient httpClient = new DefaultHttpClient();\n// 指定访问的服务器地址是电脑本机\nHttpGet httpGet = new HttpGet("http://10.0.2.2/get_data.json");\nHttpResponse httpResponse = httpClient.execute(httpGet);\n\nif (httpResponse.getStatusLine().getStatusCode() == 200) {\n    // 请求和响应都成功了\n    HttpEntity entity = httpResponse.getEntity();\n    String response = EntityUtils.toString(entity, "utf-8");\n\n    Gson gson = new Gson();\n    List<App> appList = gson.fromJson(response, new TypeToken<List<App>>() {}.getType());\n\n    for (App app : appList) {\n        Log.d("MainActivity", "id is " + app.getId());\n        Log.d("MainActivity", "name is " + app.getName());\n        Log.d("MainActivity", "version is " + app.getVersion());\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Network Best Practice

public interface HttpCallbackListener {\nvoid onFinish(String response);\nvoid onError(Exception e);\n}\n
1
2
3
4
public class HttpUtil {\n    public static void sendHttpRequest(final String address, final\n      HttpCallbackListener listener) {\n        new Thread(new Runnable() {\n        @Override\n        public void run() {\n            HttpURLConnection connection = null;\n            try {\n\n            URL url = new URL(address);\n            connection = (HttpURLConnection) url.openConnection();\n            connection.setRequestMethod("GET");\n            connection.setConnectTimeout(8000);\n            connection.setReadTimeout(8000);\n            connection.setDoInput(true);\n            connection.setDoOutput(true);\n\n            InputStream in = connection.getInputStream();\n            BufferedReader reader = new BufferedReader(new InputStreamReader(in));\n            StringBuilder response = new StringBuilder();\n            String line;\n\n            while ((line = reader.readLine()) != null) {\n                response.append(line);\n            }\n\n            if (listener != null) {\n                // 回调 onFinish() 方法\n                // 将 response 传入回调方法\n                listener.onFinish(response.toString());\n                }\n            } catch (Exception e) {\n                if (listener != null) {\n                // 回调 onError() 方法\n                listener.onError(e);\n                }\n            } finally {\n                if (connection != null) {\n                    connection.disconnect();\n                }\n            }\n        }  //  end of run\n        }).start();  //  end of runnable\n    }  //   end of sendHttpRequest\n}  //  end of class\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//以后每当需要发起一条 HTTP 请求的时候就可以这样写:\nString address = "http://www.github.com";\nString response = HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {\n    @Override\n    public void onFinish(String response) {\n        // 在这里根据返回内容执行具体的逻辑\n    }\n    @Override\n    public void onError(Exception e) {\n        // 在这里对异常情况进行处理\n    }\n});\n
1
2
3
4
5
6
7
8
9
10
11
12

Map

Location

public class MainActivity extends Activity {\n\n  public static final int SHOW_LOCATION = 0;\n\n  private TextView positionTextView;\n\n  private LocationManager locationManager;\n\n  private String provider;\n\n  @Override\n  protected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n    positionTextView = (TextView) findViewById(R.id.position_text_view);\n    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);\n    // 获取所有可用的位置提供器\n    List<String> providerList = locationManager.getProviders(true);\n    if (providerList.contains(LocationManager.GPS_PROVIDER)) {\n      provider = LocationManager.GPS_PROVIDER;\n    } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {\n      provider = LocationManager.NETWORK_PROVIDER;\n    } else {\n      // 当没有可用的位置提供器时,弹出Toast提示用户\n      Toast.makeText(this, "No location provider to use",\n          Toast.LENGTH_SHORT).show();\n      return;\n    }\n    Location location = locationManager.getLastKnownLocation(provider);\n    if (location != null) {\n      // 显示当前设备的位置信息\n      showLocation(location);\n    }\n    locationManager.requestLocationUpdates(provider, 5000, 1,\n        locationListener);\n  }\n\n  protected void onDestroy() {\n    super.onDestroy();\n    if (locationManager != null) {\n      // 关闭程序时将监听器移除\n      locationManager.removeUpdates(locationListener);\n    }\n  }\n\n  LocationListener locationListener = new LocationListener() {\n\n    @Override\n    public void onStatusChanged(String provider, int status, Bundle extras) {\n    }\n\n    @Override\n    public void onProviderEnabled(String provider) {\n    }\n\n    @Override\n    public void onProviderDisabled(String provider) {\n    }\n\n    @Override\n    public void onLocationChanged(Location location) {\n      // 更新当前设备的位置信息\n      showLocation(location);\n    }\n  };\n\n  private void showLocation(final Location location) {\n    new Thread(new Runnable() {\n      @Override\n      public void run() {\n        try {\n          // 组装反向地理编码的接口地址\n          StringBuilder url = new StringBuilder();\n          url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");\n          url.append(location.getLatitude()).append(",")\n              .append(location.getLongitude());\n          url.append("&sensor=false");\n          HttpClient httpClient = new DefaultHttpClient();\n          HttpGet httpGet = new HttpGet(url.toString());\n          // 在请求消息头中指定语言,保证服务器会返回中文数据\n          httpGet.addHeader("Accept-Language", "zh-CN");\n          HttpResponse httpResponse = httpClient.execute(httpGet);\n          if (httpResponse.getStatusLine().getStatusCode() == 200) {\n            HttpEntity entity = httpResponse.getEntity();\n            String response = EntityUtils.toString(entity, "utf-8");\n            JSONObject jsonObject = new JSONObject(response);\n            // 获取results节点下的位置信息\n            JSONArray resultArray = jsonObject.getJSONArray("results");\n            if (resultArray.length() > 0) {\n              JSONObject subObject = resultArray.getJSONObject(0);\n              // 取出格式化后的位置信息\n              String address = subObject.getString("formatted_address");\n              Message message = new Message();\n              message.what = SHOW_LOCATION;\n              message.obj = address;\n              handler.sendMessage(message);\n            }\n          }\n        } catch (Exception e) {\n          e.printStackTrace();\n        }\n      }\n    }).start();\n  }\n\n  private Handler handler = new Handler() {\n\n    public void handleMessage(Message msg) {\n      switch (msg.what) {\n      case SHOW_LOCATION:\n        String currentPosition = (String) msg.obj;\n        positionTextView.setText(currentPosition);\n        break;\n      default:\n        break;\n      }\n    }\n\n  };\n\n}\n\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

Sensor

SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);\n
1
SensorEventListener listener = new SensorEventListener() {\n    @Override\n    public void onAccuracyChanged(Sensor sensor, int accuracy) {\n    }\n    @Override\n    public void onSensorChanged(SensorEvent event) {\n    }\n};\n
1
2
3
4
5
6
7
8
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);\nsensorManager.unregisterListener(listener);\n
1
2

Light Sensor

Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);\n
1

Accelerometer Sensor

Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);\n
1

Orientation Sensor

Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);


Best Practice

Global Context

<application android:name="com.example.networkTest.MyApplication">\n  ......\n</application>\n
1
2
3
public class MyApplication extends Application {\n    private static Context context;\n    @Override\n    public void onCreate() {\n        context = getApplicationContext();\n    }\n    public static Context getContext() {\n        return context;\n    }\n}\n
1
2
3
4
5
6
7
8
9
10

用 Intent 传递对象

Serializable

public class Person implements Serializable\n
1

Parcelable

public class Person implements Parcelable {\n    private String name;\n    private int age;\n\n    @Override\n    public int describeContents() {\n        return 0;\n    }\n    @Override\n    public void writeToParcel(Parcel dest, int flags) {\n        dest.writeString(name); // 写出name\n        dest.writeInt(age); // 写出age\n    }\n\n    public static final Parcelable.Creator<Person> CREATOR = new\n      Parcelable.Creator<Person>() {\n        @Override\n        public Person createFromParcel(Parcel source) {\n            Person person = new Person();\n            person.name = source.readString(); // 读取name\n            person.age = source.readInt(); // 读取age\n            return person;\n        }\n        @Override\n        public Person[] newArray(int size) {\n            return new Person[size];\n        }\n    };\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Custom Logger

public class LogUtil {\n    public static final int VERBOSE = 1;\n    public static final int DEBUG = 2;\n    public static final int INFO = 3;\n    public static final int WARN = 4;\n    public static final int ERROR = 5;\n    public static final int NOTHING = 6;\n    //custom key\n    public static final int LEVEL = VERBOSE;\n\n    public static void v(String tag, String msg) {\n        if (LEVEL <= VERBOSE) {\n            Log.v(tag, msg);\n        }\n    }\n    public static void d(String tag, String msg) {\n        if (LEVEL <= DEBUG) {\n            Log.d(tag, msg);\n        }\n    }\n    public static void i(String tag, String msg) {\n        if (LEVEL <= INFO) {\n            Log.i(tag, msg);\n        }\n    }\n    public static void w(String tag, String msg) {\n        if (LEVEL <= WARN) {\n            Log.w(tag, msg);\n        }\n    }\n    public static void e(String tag, String msg) {\n        if (LEVEL <= ERROR) {\n            Log.e(tag, msg);\n        }\n    }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
',230);t.render=function(a,p,t,o,c,l){return n(),s("div",null,[e])};export default t;export{p as __pageData}; diff --git a/assets/programming_android_androidBasicNotes.md.4d20a147.lean.js b/assets/programming_android_androidBasicNotes.md.4d20a147.lean.js new file mode 100644 index 00000000000..a3c7e8e0d4e --- /dev/null +++ b/assets/programming_android_androidBasicNotes.md.4d20a147.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const p='{"title":"Android Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Android Framework","slug":"android-framework"},{"level":3,"title":"Dalvik virtual machine","slug":"dalvik-virtual-machine"},{"level":3,"title":"Basic Building Blocks","slug":"basic-building-blocks"},{"level":2,"title":"Android Studio","slug":"android-studio"},{"level":3,"title":"Plugins","slug":"plugins"},{"level":2,"title":"API Conventions","slug":"api-conventions"},{"level":3,"title":"Manager Service","slug":"manager-service"},{"level":2,"title":"Activity","slug":"activity"},{"level":3,"title":"Base Activity","slug":"base-activity"},{"level":3,"title":"Activity Collector","slug":"activity-collector"},{"level":3,"title":"Start Activity","slug":"start-activity"},{"level":2,"title":"UI Design","slug":"ui-design"},{"level":3,"title":"Layout","slug":"layout"},{"level":3,"title":"Component","slug":"component"},{"level":3,"title":"Drawable","slug":"drawable"},{"level":2,"title":"Fragment","slug":"fragment"},{"level":3,"title":"Basic Fragment","slug":"basic-fragment"},{"level":3,"title":"Transfer Information","slug":"transfer-information"},{"level":3,"title":"Runtime Loop","slug":"runtime-loop"},{"level":2,"title":"Broadcast","slug":"broadcast"},{"level":3,"title":"Register Receiver","slug":"register-receiver"},{"level":3,"title":"Custom Broadcast","slug":"custom-broadcast"},{"level":3,"title":"Local Broadcast","slug":"local-broadcast"},{"level":2,"title":"Data Store","slug":"data-store"},{"level":3,"title":"Files Store","slug":"files-store"},{"level":3,"title":"Shared Preferences","slug":"shared-preferences"},{"level":3,"title":"DataBase","slug":"database"},{"level":2,"title":"Content Provider","slug":"content-provider"},{"level":3,"title":"Read Other App Content","slug":"read-other-app-content"},{"level":3,"title":"Provide App Content","slug":"provide-app-content"},{"level":2,"title":"Service","slug":"service"},{"level":3,"title":"Handler","slug":"handler"},{"level":3,"title":"Async Task","slug":"async-task"},{"level":3,"title":"Basic Service","slug":"basic-service"},{"level":3,"title":"ForeGround Service","slug":"foreground-service"},{"level":3,"title":"Intent Service","slug":"intent-service"},{"level":3,"title":"Alarm Service","slug":"alarm-service"},{"level":2,"title":"Media","slug":"media"},{"level":3,"title":"Notification","slug":"notification"},{"level":3,"title":"SMS","slug":"sms"},{"level":3,"title":"Audio","slug":"audio"},{"level":3,"title":"Music","slug":"music"},{"level":2,"title":"NetWork","slug":"network"},{"level":3,"title":"WebView","slug":"webview"},{"level":3,"title":"HTTP URL Connection","slug":"http-url-connection"},{"level":3,"title":"HTTP Client","slug":"http-client"},{"level":3,"title":"XML","slug":"xml"},{"level":3,"title":"JSON","slug":"json"},{"level":3,"title":"Network Best Practice","slug":"network-best-practice"},{"level":2,"title":"Map","slug":"map"},{"level":3,"title":"Location","slug":"location"},{"level":2,"title":"Sensor","slug":"sensor"},{"level":3,"title":"Light Sensor","slug":"light-sensor"},{"level":3,"title":"Accelerometer Sensor","slug":"accelerometer-sensor"},{"level":3,"title":"Orientation Sensor","slug":"orientation-sensor"},{"level":2,"title":"Best Practice","slug":"best-practice"},{"level":3,"title":"Global Context","slug":"global-context"},{"level":3,"title":"用 Intent 传递对象","slug":"用-intent-传递对象"},{"level":3,"title":"Custom Logger","slug":"custom-logger"}],"relativePath":"programming/android/androidBasicNotes.md","lastUpdated":1627196177000}',t={},e=a('',230);t.render=function(a,p,t,o,c,l){return n(),s("div",null,[e])};export default t;export{p as __pageData}; diff --git a/assets/programming_devops_cleanCodeBasicNotes.md.f8256997.js b/assets/programming_devops_cleanCodeBasicNotes.md.f8256997.js new file mode 100644 index 00000000000..bafc73c9acb --- /dev/null +++ b/assets/programming_devops_cleanCodeBasicNotes.md.f8256997.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as e}from"./app.6dd2a1b7.js";const t='{"title":"Clean Code Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Programming Principles","slug":"programming-principles"},{"level":3,"title":"SOLID principle","slug":"solid-principle"},{"level":2,"title":"Programming Paradigms","slug":"programming-paradigms"},{"level":3,"title":"Taxonomy of Paradigms","slug":"taxonomy-of-paradigms"},{"level":3,"title":"Functional Programming","slug":"functional-programming"},{"level":2,"title":"Code Review","slug":"code-review"},{"level":3,"title":"Code Review Basis","slug":"code-review-basis"},{"level":3,"title":"Security Review","slug":"security-review"},{"level":3,"title":"Documents Review","slug":"documents-review"},{"level":3,"title":"Comments Review","slug":"comments-review"},{"level":3,"title":"Test Review","slug":"test-review"}],"relativePath":"programming/devops/cleanCodeBasicNotes.md","lastUpdated":1627196177000}',c={},l=a("h1",{id:"clean-code-basic-notes"},[a("a",{class:"header-anchor",href:"#clean-code-basic-notes","aria-hidden":"true"},"#"),e(" Clean Code Basic Notes")],-1),o=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#clean-code-basic-notes"},"Clean Code Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#programming-principles"},"Programming Principles")]),a("li",null,[a("a",{href:"#programming-paradigms"},"Programming Paradigms")]),a("li",null,[a("a",{href:"#code-review"},"Code Review")])])])])])],-1),p=a("h2",{id:"programming-principles"},[a("a",{class:"header-anchor",href:"#programming-principles","aria-hidden":"true"},"#"),e(" Programming Principles")],-1),u=a("h3",{id:"solid-principle"},[a("a",{class:"header-anchor",href:"#solid-principle","aria-hidden":"true"},"#"),e(" SOLID principle")],-1),i=a("h4",{id:"single-responsibility-principle"},[a("a",{class:"header-anchor",href:"#single-responsibility-principle","aria-hidden":"true"},"#"),e(" Single Responsibility Principle")],-1),r=a("p",null,"A class should have only one job",-1),k=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"name"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"getAnimalName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"saveAnimal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"animal"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token comment"},"// =>"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"name"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"getAnimalName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"AnimalDB"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"getAnimal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"animal"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"saveAnimal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"animal"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),m=a("h4",{id:"open-closed-principle"},[a("a",{class:"header-anchor",href:"#open-closed-principle","aria-hidden":"true"},"#"),e(" Open-Closed Principle")],-1),b=a("p",null,"Software entities(Classes, modules, functions) should be open for extension, close for modification.",-1),d=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),e(" animals"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),e("\n "),a("span",{class:"token keyword"},"new"),e(),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'lion'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"new"),e(),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'mouse'"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\nfunction "),a("span",{class:"token class-name"},"AnimalSound"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<="),e(" a"),a("span",{class:"token punctuation"},"."),e("length"),a("span",{class:"token punctuation"},";"),e(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),e("name "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token string"},"'lion'"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'roar'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),e("name "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token string"},"'mouse'"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'squeak'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token class-name"},"AnimalSound"),a("span",{class:"token punctuation"},"("),e("animals"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),g=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Lion"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token string"},"'roar'"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Squirrel"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token string"},"'squeak'"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Snake"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token string"},"'hiss'"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token comment"},"//..."),e("\nfunction "),a("span",{class:"token class-name"},"AnimalSound"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<="),e(" a"),a("span",{class:"token punctuation"},"."),e("length"),a("span",{class:"token punctuation"},";"),e(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token class-name"},"AnimalSound"),a("span",{class:"token punctuation"},"("),e("animals"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br")])],-1),w=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Discount"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"giveDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("customer "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token string"},"'fav'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("price "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"0.2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("customer "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token string"},"'vip'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("price "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"0.4"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),h=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"VIPDiscount"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Discount"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"getDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token keyword"},"super"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"SuperVIPDiscount"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"VIPDiscount"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"getDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token keyword"},"super"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),f=a("h4",{id:"liskov-substitution-principle"},[a("a",{class:"header-anchor",href:"#liskov-substitution-principle","aria-hidden":"true"},"#"),e(" Liskov Substitution Principle")],-1),y=a("p",null,"A sub-class must be substitutable for its super-class",-1),v=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[e("function "),a("span",{class:"token class-name"},"AnimalLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<="),e(" a"),a("span",{class:"token punctuation"},"."),e("length"),a("span",{class:"token punctuation"},";"),e(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("typeof a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token class-name"},"Lion"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"LionLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("typeof a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token class-name"},"Mouse"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"MouseLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("typeof a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token class-name"},"Snake"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"SnakeLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token class-name"},"AnimalLegCount"),a("span",{class:"token punctuation"},"("),e("animals"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),S=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token class-name"},"LegCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token comment"},"//..."),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Lion"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token class-name"},"LegCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token comment"},"//..."),e("\n\nfunction "),a("span",{class:"token class-name"},"AnimalLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),a("span",{class:"token punctuation"},"("),e("let i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<="),e(" a"),a("span",{class:"token punctuation"},"."),e("length"),a("span",{class:"token punctuation"},";"),e(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"LegCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token class-name"},"AnimalLegCount"),a("span",{class:"token punctuation"},"("),e("animals"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),C=a("h4",{id:"interface-segregation-principle"},[a("a",{class:"header-anchor",href:"#interface-segregation-principle","aria-hidden":"true"},"#"),e(" Interface Segregation Principle")],-1),A=a("ul",null,[a("li",null,[e("Make "),a("strong",null,"fine grained"),e(" interfaces that are client specific")]),a("li",null,"Clients should not be forced to depend upon interfaces that they do not use")],-1),P=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Circle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Square"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Rectangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br")])],-1),q=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"ICircle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"ISquare"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"IRectangle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"ITriangle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawTriangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Circle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"ICircle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Square"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"ISquare"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Rectangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IRectangle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Triangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"ITriangle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawTriangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"CustomShape"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br")])],-1),R=a("p",null,"OR",-1),I=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Circle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Triangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Square"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Rectangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br")])],-1),x=a("h4",{id:"dependency-inversion-principle"},[a("a",{class:"header-anchor",href:"#dependency-inversion-principle","aria-hidden":"true"},"#"),e(" Dependency Inversion Principle")],-1),L=a("p",null,[e("Dependency should be on "),a("strong",null,"abstractions"),e(" not concretions:")],-1),D=a("ul",null,[a("li",null,[e("High-level modules should not depend upon low-level modules. Both should depend upon "),a("strong",null,"abstractions")]),a("li",null,"Abstractions should not depend on details. Details should depend upon abstractions")],-1),T=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"XMLHttpService"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"XMLHttpRequestService"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Http"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"private"),e(" xmlHttpService"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"XMLHttpService"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string "),a("span",{class:"token punctuation"},","),e(" options"),a("span",{class:"token operator"},":"),e(" any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("xmlHttpService"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},"'GET'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"post"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("xmlHttpService"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},"'POST'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),j=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"Connection"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string"),a("span",{class:"token punctuation"},","),e(" opts"),a("span",{class:"token operator"},":"),e("any"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token comment"},"// Abstraction not upon on details (but upon on abstractions)"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Http"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"private"),e(" httpConnection"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Connection"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string "),a("span",{class:"token punctuation"},","),e(" options"),a("span",{class:"token operator"},":"),e(" any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("httpConnection"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},"'GET'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"post"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("httpConnection"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},"'POST'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"XMLHttpService"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"Connection"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"const"),e(" xhr "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"new"),e(),a("span",{class:"token class-name"},"XMLHttpRequest"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string"),a("span",{class:"token punctuation"},","),e(" opts"),a("span",{class:"token operator"},":"),e("any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n xhr"),a("span",{class:"token punctuation"},"."),a("span",{class:"token keyword"},"open"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n xhr"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"send"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"NodeHttpService"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"Connection"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string"),a("span",{class:"token punctuation"},","),e(" opts"),a("span",{class:"token operator"},":"),e("any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"MockHttpService"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"Connection"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string"),a("span",{class:"token punctuation"},","),e(" opts"),a("span",{class:"token operator"},":"),e("any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br")])],-1),H=a("h2",{id:"programming-paradigms"},[a("a",{class:"header-anchor",href:"#programming-paradigms","aria-hidden":"true"},"#"),e(" Programming Paradigms")],-1),N=a("p",null,"Each programming language realizes one or more paradigms. Each paradigm is defined by a set of programming concepts.",-1),M=a("h3",{id:"taxonomy-of-paradigms"},[a("a",{class:"header-anchor",href:"#taxonomy-of-paradigms","aria-hidden":"true"},"#"),e(" Taxonomy of Paradigms")],-1),B=a("p",null,[a("img",{src:"/awesome-notes/assets/ProgrammingParadigms.04c60b43.jpg",alt:"Taxonomy of Programming Paradigms"})],-1),O=a("p",null,"Nondeterminism is important for real-world interaction. Named state is important for modularity.",-1),X=a("h4",{id:"observable-nondeterminism"},[a("a",{class:"header-anchor",href:"#observable-nondeterminism","aria-hidden":"true"},"#"),e(" Observable Nondeterminism")],-1),E=a("p",null,[e("During the execution, this choice is made by a part of the run-time system called the scheduler. The nondeterminism is observable if a user can see "),a("strong",null,"different results"),e(" from executions that start at the "),a("strong",null,"same internal configuration"),e(".")],-1),F=a("ul",null,[a("li",null,"concurrency or race condition (timing effects)"),a("li",null,"shared-state concurrency or message-passing concurrency"),a("li",null,"concurrent programming language e.g Java")],-1),V=a("h4",{id:"named-state"},[a("a",{class:"header-anchor",href:"#named-state","aria-hidden":"true"},"#"),e(" Named State")],-1),G=a("p",null,"State is the ability to remember information (a sequence of values in time). Distinguish three axes of expressiveness, depending on whether the state is:",-1),z=a("ul",null,[a("li",null,"unnamed or named"),a("li",null,"deterministic or nondeterministic"),a("li",null,"sequential or concurrent"),a("li",null,"named, nondeterministic and concurrent paradigm as the most expressiveness of state"),a("li",null,"named state for updatable memory (mutable state) and modularity")],-1),J=a("p",null,"The least expressive combination is functional programming (threaded state: unnamed, deterministic, sequential). Adding concurrency gives declarative concurrent programming (synchronous cells: unnamed, deterministic, concurrent). Adding nondeterministic choice gives concurrent logic programming (stream mergers: unnamed, nondeterministic, concurrent). Adding ports or cells, gives message passing or shared state (named, nondeterministic, concurrent).",-1),U=a("h3",{id:"functional-programming"},[a("a",{class:"header-anchor",href:"#functional-programming","aria-hidden":"true"},"#"),e(" Functional Programming")],-1),K=a("p",null,[e("Based on the concept of first-class function or closure, which makes it equivalent to the λ-calculus which is "),a("code",null,"Turing complete"),e(".")],-1),Q=a("h2",{id:"code-review"},[a("a",{class:"header-anchor",href:"#code-review","aria-hidden":"true"},"#"),e(" Code Review")],-1),W=a("h3",{id:"code-review-basis"},[a("a",{class:"header-anchor",href:"#code-review-basis","aria-hidden":"true"},"#"),e(" Code Review Basis")],-1),Y=a("ul",null,[a("li",null,[a("p",null,"逻辑正确")]),a("li",null,[a("p",null,"简单易懂")]),a("li",null,[a("p",null,"编程规范(linter)")]),a("li",null,[a("p",null,"模块化")]),a("li",null,[a("p",null,"多余或重复 代码/日志/测试")]),a("li",null,[a("p",null,"重复轮子(可被基本库替换)")]),a("li",null,[a("p",null,"无必要的全局变量")]),a("li",null,[a("p",null,"代码误注释")]),a("li",null,[a("p",null,"死循环(边界检查)")])],-1),Z=a("h3",{id:"security-review"},[a("a",{class:"header-anchor",href:"#security-review","aria-hidden":"true"},"#"),e(" Security Review")],-1),$=a("ul",null,[a("li",null,"所有的数据输入是否都进行了检查(检测正确的类型,长度,格式和范围)并且进行了编码?"),a("li",null,"在哪里使用了第三方工具,返回的错误是否被捕获?"),a("li",null,"输出的值是否进行了检查并且编码?"),a("li",null,"无效的参数值是否能够处理?")],-1),_=a("h3",{id:"documents-review"},[a("a",{class:"header-anchor",href:"#documents-review","aria-hidden":"true"},"#"),e(" Documents Review")],-1),nn=a("h3",{id:"comments-review"},[a("a",{class:"header-anchor",href:"#comments-review","aria-hidden":"true"},"#"),e(" Comments Review")],-1),sn=a("ul",null,[a("li",null,"数据结构"),a("li",null,"计量单位"),a("li",null,"描述代码意图"),a("li",null,"函数块"),a("li",null,"非常规行为/边界检查"),a("li",null,"TODO 标记"),a("li",null,"FIXME 标记"),a("li",null,"第三方库文档")],-1),an=a("h3",{id:"test-review"},[a("a",{class:"header-anchor",href:"#test-review","aria-hidden":"true"},"#"),e(" Test Review")],-1),en=a("ul",null,[a("li",null,"代码是否可以测试?比如,不要添加太多的或是隐藏的依赖关系,不能够初始化对象,测试框架可以使用方法等。"),a("li",null,"是否存在测试,它们是否可以被理解?比如,至少达到你满意的代码覆盖(code coverage)。"),a("li",null,"单元测试是否真正的测试了代码是否可以完成预期的功能?"),a("li",null,"是否检查了数组的“越界“错误?"),a("li",null,"是否有可以被已经存在的 API 所替代的测")],-1);c.render=function(a,e,t,c,tn,cn){return n(),s("div",null,[l,o,p,u,i,r,k,m,b,d,g,w,h,f,y,v,S,C,A,P,q,R,I,x,L,D,T,j,H,N,M,B,O,X,E,F,V,G,z,J,U,K,Q,W,Y,Z,$,_,nn,sn,an,en])};export default c;export{t as __pageData}; diff --git a/assets/programming_devops_cleanCodeBasicNotes.md.f8256997.lean.js b/assets/programming_devops_cleanCodeBasicNotes.md.f8256997.lean.js new file mode 100644 index 00000000000..bafc73c9acb --- /dev/null +++ b/assets/programming_devops_cleanCodeBasicNotes.md.f8256997.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as e}from"./app.6dd2a1b7.js";const t='{"title":"Clean Code Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Programming Principles","slug":"programming-principles"},{"level":3,"title":"SOLID principle","slug":"solid-principle"},{"level":2,"title":"Programming Paradigms","slug":"programming-paradigms"},{"level":3,"title":"Taxonomy of Paradigms","slug":"taxonomy-of-paradigms"},{"level":3,"title":"Functional Programming","slug":"functional-programming"},{"level":2,"title":"Code Review","slug":"code-review"},{"level":3,"title":"Code Review Basis","slug":"code-review-basis"},{"level":3,"title":"Security Review","slug":"security-review"},{"level":3,"title":"Documents Review","slug":"documents-review"},{"level":3,"title":"Comments Review","slug":"comments-review"},{"level":3,"title":"Test Review","slug":"test-review"}],"relativePath":"programming/devops/cleanCodeBasicNotes.md","lastUpdated":1627196177000}',c={},l=a("h1",{id:"clean-code-basic-notes"},[a("a",{class:"header-anchor",href:"#clean-code-basic-notes","aria-hidden":"true"},"#"),e(" Clean Code Basic Notes")],-1),o=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#clean-code-basic-notes"},"Clean Code Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#programming-principles"},"Programming Principles")]),a("li",null,[a("a",{href:"#programming-paradigms"},"Programming Paradigms")]),a("li",null,[a("a",{href:"#code-review"},"Code Review")])])])])])],-1),p=a("h2",{id:"programming-principles"},[a("a",{class:"header-anchor",href:"#programming-principles","aria-hidden":"true"},"#"),e(" Programming Principles")],-1),u=a("h3",{id:"solid-principle"},[a("a",{class:"header-anchor",href:"#solid-principle","aria-hidden":"true"},"#"),e(" SOLID principle")],-1),i=a("h4",{id:"single-responsibility-principle"},[a("a",{class:"header-anchor",href:"#single-responsibility-principle","aria-hidden":"true"},"#"),e(" Single Responsibility Principle")],-1),r=a("p",null,"A class should have only one job",-1),k=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"name"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"getAnimalName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"saveAnimal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"animal"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token comment"},"// =>"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"name"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"getAnimalName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"AnimalDB"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"getAnimal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"animal"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"saveAnimal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"animal"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br")])],-1),m=a("h4",{id:"open-closed-principle"},[a("a",{class:"header-anchor",href:"#open-closed-principle","aria-hidden":"true"},"#"),e(" Open-Closed Principle")],-1),b=a("p",null,"Software entities(Classes, modules, functions) should be open for extension, close for modification.",-1),d=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),e(" animals"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),e(),a("span",{class:"token operator"},"="),e(),a("span",{class:"token punctuation"},"["),e("\n "),a("span",{class:"token keyword"},"new"),e(),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'lion'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),e("\n "),a("span",{class:"token keyword"},"new"),e(),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'mouse'"),a("span",{class:"token punctuation"},")"),e("\n"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),e("\n\nfunction "),a("span",{class:"token class-name"},"AnimalSound"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<="),e(" a"),a("span",{class:"token punctuation"},"."),e("length"),a("span",{class:"token punctuation"},";"),e(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),e("name "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token string"},"'lion'"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'roar'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),e("name "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token string"},"'mouse'"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'squeak'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token class-name"},"AnimalSound"),a("span",{class:"token punctuation"},"("),e("animals"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br")])],-1),g=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Lion"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token string"},"'roar'"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Squirrel"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token string"},"'squeak'"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Snake"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token string"},"'hiss'"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token comment"},"//..."),e("\nfunction "),a("span",{class:"token class-name"},"AnimalSound"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<="),e(" a"),a("span",{class:"token punctuation"},"."),e("length"),a("span",{class:"token punctuation"},";"),e(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"makeSound"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token class-name"},"AnimalSound"),a("span",{class:"token punctuation"},"("),e("animals"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br")])],-1),w=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Discount"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"giveDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("customer "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token string"},"'fav'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("price "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"0.2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("customer "),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token string"},"'vip'"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("price "),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"0.4"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),h=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"VIPDiscount"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Discount"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"getDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token keyword"},"super"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"SuperVIPDiscount"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"VIPDiscount"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"getDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"return"),e(),a("span",{class:"token keyword"},"super"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getDiscount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token operator"},"*"),e(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),f=a("h4",{id:"liskov-substitution-principle"},[a("a",{class:"header-anchor",href:"#liskov-substitution-principle","aria-hidden":"true"},"#"),e(" Liskov Substitution Principle")],-1),y=a("p",null,"A sub-class must be substitutable for its super-class",-1),v=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[e("function "),a("span",{class:"token class-name"},"AnimalLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"int"),e(" i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<="),e(" a"),a("span",{class:"token punctuation"},"."),e("length"),a("span",{class:"token punctuation"},";"),e(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("typeof a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token class-name"},"Lion"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"LionLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("typeof a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token class-name"},"Mouse"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"MouseLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),e("typeof a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),e(),a("span",{class:"token operator"},"=="),e(),a("span",{class:"token class-name"},"Snake"),a("span",{class:"token punctuation"},")"),e("\n "),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"SnakeLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token class-name"},"AnimalLegCount"),a("span",{class:"token punctuation"},"("),e("animals"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),S=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Animal"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token class-name"},"LegCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token comment"},"//..."),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Lion"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token class-name"},"LegCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token comment"},"//..."),e("\n\nfunction "),a("span",{class:"token class-name"},"AnimalLegCount"),a("span",{class:"token punctuation"},"("),e("a"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Array"),a("span",{class:"token generics"},[a("span",{class:"token punctuation"},"<"),a("span",{class:"token class-name"},"Animal"),a("span",{class:"token punctuation"},">")]),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"for"),a("span",{class:"token punctuation"},"("),e("let i "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),e(" i "),a("span",{class:"token operator"},"<="),e(" a"),a("span",{class:"token punctuation"},"."),e("length"),a("span",{class:"token punctuation"},";"),e(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n a"),a("span",{class:"token punctuation"},"["),e("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"LegCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token class-name"},"AnimalLegCount"),a("span",{class:"token punctuation"},"("),e("animals"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),C=a("h4",{id:"interface-segregation-principle"},[a("a",{class:"header-anchor",href:"#interface-segregation-principle","aria-hidden":"true"},"#"),e(" Interface Segregation Principle")],-1),A=a("ul",null,[a("li",null,[e("Make "),a("strong",null,"fine grained"),e(" interfaces that are client specific")]),a("li",null,"Clients should not be forced to depend upon interfaces that they do not use")],-1),P=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Circle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Square"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Rectangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br")])],-1),q=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"ICircle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"ISquare"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"IRectangle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"ITriangle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawTriangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Circle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"ICircle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawCircle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Square"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"ISquare"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawSquare"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Rectangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IRectangle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawRectangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Triangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"ITriangle"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"drawTriangle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"CustomShape"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br")])],-1),R=a("p",null,"OR",-1),I=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Circle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Triangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Square"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Rectangle"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"IShape"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"draw"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br")])],-1),x=a("h4",{id:"dependency-inversion-principle"},[a("a",{class:"header-anchor",href:"#dependency-inversion-principle","aria-hidden":"true"},"#"),e(" Dependency Inversion Principle")],-1),L=a("p",null,[e("Dependency should be on "),a("strong",null,"abstractions"),e(" not concretions:")],-1),D=a("ul",null,[a("li",null,[e("High-level modules should not depend upon low-level modules. Both should depend upon "),a("strong",null,"abstractions")]),a("li",null,"Abstractions should not depend on details. Details should depend upon abstractions")],-1),T=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"XMLHttpService"),e(),a("span",{class:"token keyword"},"extends"),e(),a("span",{class:"token class-name"},"XMLHttpRequestService"),e(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Http"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"private"),e(" xmlHttpService"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"XMLHttpService"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string "),a("span",{class:"token punctuation"},","),e(" options"),a("span",{class:"token operator"},":"),e(" any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("xmlHttpService"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},"'GET'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"post"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("xmlHttpService"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},"'POST'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),j=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"interface"),e(),a("span",{class:"token class-name"},"Connection"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string"),a("span",{class:"token punctuation"},","),e(" opts"),a("span",{class:"token operator"},":"),e("any"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token comment"},"// Abstraction not upon on details (but upon on abstractions)"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"Http"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"private"),e(" httpConnection"),a("span",{class:"token operator"},":"),e(),a("span",{class:"token class-name"},"Connection"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e(),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string "),a("span",{class:"token punctuation"},","),e(" options"),a("span",{class:"token operator"},":"),e(" any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("httpConnection"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},"'GET'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token function"},"post"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),e("httpConnection"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token punctuation"},","),a("span",{class:"token string"},"'POST'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"XMLHttpService"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"Connection"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token keyword"},"const"),e(" xhr "),a("span",{class:"token operator"},"="),e(),a("span",{class:"token keyword"},"new"),e(),a("span",{class:"token class-name"},"XMLHttpRequest"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string"),a("span",{class:"token punctuation"},","),e(" opts"),a("span",{class:"token operator"},":"),e("any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n xhr"),a("span",{class:"token punctuation"},"."),a("span",{class:"token keyword"},"open"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n xhr"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"send"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"NodeHttpService"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"Connection"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string"),a("span",{class:"token punctuation"},","),e(" opts"),a("span",{class:"token operator"},":"),e("any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token keyword"},"class"),e(),a("span",{class:"token class-name"},"MockHttpService"),e(),a("span",{class:"token keyword"},"implements"),e(),a("span",{class:"token class-name"},"Connection"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),e("url"),a("span",{class:"token operator"},":"),e(" string"),a("span",{class:"token punctuation"},","),e(" opts"),a("span",{class:"token operator"},":"),e("any"),a("span",{class:"token punctuation"},")"),e(),a("span",{class:"token punctuation"},"{"),e("\n "),a("span",{class:"token comment"},"//..."),e("\n "),a("span",{class:"token punctuation"},"}"),e("\n"),a("span",{class:"token punctuation"},"}"),e("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br")])],-1),H=a("h2",{id:"programming-paradigms"},[a("a",{class:"header-anchor",href:"#programming-paradigms","aria-hidden":"true"},"#"),e(" Programming Paradigms")],-1),N=a("p",null,"Each programming language realizes one or more paradigms. Each paradigm is defined by a set of programming concepts.",-1),M=a("h3",{id:"taxonomy-of-paradigms"},[a("a",{class:"header-anchor",href:"#taxonomy-of-paradigms","aria-hidden":"true"},"#"),e(" Taxonomy of Paradigms")],-1),B=a("p",null,[a("img",{src:"/awesome-notes/assets/ProgrammingParadigms.04c60b43.jpg",alt:"Taxonomy of Programming Paradigms"})],-1),O=a("p",null,"Nondeterminism is important for real-world interaction. Named state is important for modularity.",-1),X=a("h4",{id:"observable-nondeterminism"},[a("a",{class:"header-anchor",href:"#observable-nondeterminism","aria-hidden":"true"},"#"),e(" Observable Nondeterminism")],-1),E=a("p",null,[e("During the execution, this choice is made by a part of the run-time system called the scheduler. The nondeterminism is observable if a user can see "),a("strong",null,"different results"),e(" from executions that start at the "),a("strong",null,"same internal configuration"),e(".")],-1),F=a("ul",null,[a("li",null,"concurrency or race condition (timing effects)"),a("li",null,"shared-state concurrency or message-passing concurrency"),a("li",null,"concurrent programming language e.g Java")],-1),V=a("h4",{id:"named-state"},[a("a",{class:"header-anchor",href:"#named-state","aria-hidden":"true"},"#"),e(" Named State")],-1),G=a("p",null,"State is the ability to remember information (a sequence of values in time). Distinguish three axes of expressiveness, depending on whether the state is:",-1),z=a("ul",null,[a("li",null,"unnamed or named"),a("li",null,"deterministic or nondeterministic"),a("li",null,"sequential or concurrent"),a("li",null,"named, nondeterministic and concurrent paradigm as the most expressiveness of state"),a("li",null,"named state for updatable memory (mutable state) and modularity")],-1),J=a("p",null,"The least expressive combination is functional programming (threaded state: unnamed, deterministic, sequential). Adding concurrency gives declarative concurrent programming (synchronous cells: unnamed, deterministic, concurrent). Adding nondeterministic choice gives concurrent logic programming (stream mergers: unnamed, nondeterministic, concurrent). Adding ports or cells, gives message passing or shared state (named, nondeterministic, concurrent).",-1),U=a("h3",{id:"functional-programming"},[a("a",{class:"header-anchor",href:"#functional-programming","aria-hidden":"true"},"#"),e(" Functional Programming")],-1),K=a("p",null,[e("Based on the concept of first-class function or closure, which makes it equivalent to the λ-calculus which is "),a("code",null,"Turing complete"),e(".")],-1),Q=a("h2",{id:"code-review"},[a("a",{class:"header-anchor",href:"#code-review","aria-hidden":"true"},"#"),e(" Code Review")],-1),W=a("h3",{id:"code-review-basis"},[a("a",{class:"header-anchor",href:"#code-review-basis","aria-hidden":"true"},"#"),e(" Code Review Basis")],-1),Y=a("ul",null,[a("li",null,[a("p",null,"逻辑正确")]),a("li",null,[a("p",null,"简单易懂")]),a("li",null,[a("p",null,"编程规范(linter)")]),a("li",null,[a("p",null,"模块化")]),a("li",null,[a("p",null,"多余或重复 代码/日志/测试")]),a("li",null,[a("p",null,"重复轮子(可被基本库替换)")]),a("li",null,[a("p",null,"无必要的全局变量")]),a("li",null,[a("p",null,"代码误注释")]),a("li",null,[a("p",null,"死循环(边界检查)")])],-1),Z=a("h3",{id:"security-review"},[a("a",{class:"header-anchor",href:"#security-review","aria-hidden":"true"},"#"),e(" Security Review")],-1),$=a("ul",null,[a("li",null,"所有的数据输入是否都进行了检查(检测正确的类型,长度,格式和范围)并且进行了编码?"),a("li",null,"在哪里使用了第三方工具,返回的错误是否被捕获?"),a("li",null,"输出的值是否进行了检查并且编码?"),a("li",null,"无效的参数值是否能够处理?")],-1),_=a("h3",{id:"documents-review"},[a("a",{class:"header-anchor",href:"#documents-review","aria-hidden":"true"},"#"),e(" Documents Review")],-1),nn=a("h3",{id:"comments-review"},[a("a",{class:"header-anchor",href:"#comments-review","aria-hidden":"true"},"#"),e(" Comments Review")],-1),sn=a("ul",null,[a("li",null,"数据结构"),a("li",null,"计量单位"),a("li",null,"描述代码意图"),a("li",null,"函数块"),a("li",null,"非常规行为/边界检查"),a("li",null,"TODO 标记"),a("li",null,"FIXME 标记"),a("li",null,"第三方库文档")],-1),an=a("h3",{id:"test-review"},[a("a",{class:"header-anchor",href:"#test-review","aria-hidden":"true"},"#"),e(" Test Review")],-1),en=a("ul",null,[a("li",null,"代码是否可以测试?比如,不要添加太多的或是隐藏的依赖关系,不能够初始化对象,测试框架可以使用方法等。"),a("li",null,"是否存在测试,它们是否可以被理解?比如,至少达到你满意的代码覆盖(code coverage)。"),a("li",null,"单元测试是否真正的测试了代码是否可以完成预期的功能?"),a("li",null,"是否检查了数组的“越界“错误?"),a("li",null,"是否有可以被已经存在的 API 所替代的测")],-1);c.render=function(a,e,t,c,tn,cn){return n(),s("div",null,[l,o,p,u,i,r,k,m,b,d,g,w,h,f,y,v,S,C,A,P,q,R,I,x,L,D,T,j,H,N,M,B,O,X,E,F,V,G,z,J,U,K,Q,W,Y,Z,$,_,nn,sn,an,en])};export default c;export{t as __pageData}; diff --git a/assets/programming_devops_designPatternsBasicNotes.md.0d5c71a8.js b/assets/programming_devops_designPatternsBasicNotes.md.0d5c71a8.js new file mode 100644 index 00000000000..32c8b5911ba --- /dev/null +++ b/assets/programming_devops_designPatternsBasicNotes.md.0d5c71a8.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as t}from"./app.6dd2a1b7.js";const e='{"title":"Design Patterns Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Patterns","slug":"basic-patterns"},{"level":3,"title":"SOLID Principles","slug":"solid-principles"},{"level":3,"title":"Literal Pattern","slug":"literal-pattern"},{"level":3,"title":"Closure and IIFE","slug":"closure-and-iife"},{"level":3,"title":"Check Pattern","slug":"check-pattern"},{"level":3,"title":"函数(function)","slug":"函数-function"},{"level":3,"title":"解耦","slug":"解耦"},{"level":3,"title":"Env and Config","slug":"env-and-config"},{"level":3,"title":"Stand Library Idioms","slug":"stand-library-idioms"},{"level":3,"title":"Other","slug":"other"},{"level":2,"title":"Modular Patterns","slug":"modular-patterns"},{"level":3,"title":"Object Literal","slug":"object-literal"},{"level":3,"title":"IIFE Pattern","slug":"iife-pattern"},{"level":3,"title":"UMD Pattern","slug":"umd-pattern"},{"level":2,"title":"Common Design Patterns","slug":"common-design-patterns"},{"level":3,"title":"Classification","slug":"classification"},{"level":3,"title":"Prototype","slug":"prototype"},{"level":3,"title":"Bridge","slug":"bridge"},{"level":3,"title":"Composite","slug":"composite"},{"level":3,"title":"Strategy","slug":"strategy"},{"level":3,"title":"State","slug":"state"},{"level":3,"title":"Iterator","slug":"iterator"},{"level":3,"title":"IOC and DI","slug":"ioc-and-di"},{"level":3,"title":"Class Pattern","slug":"class-pattern"},{"level":3,"title":"Mix-In Pattern","slug":"mix-in-pattern"},{"level":3,"title":"Singleton Pattern","slug":"singleton-pattern"},{"level":3,"title":"Abstract Factory","slug":"abstract-factory"},{"level":3,"title":"Factory Method","slug":"factory-method"},{"level":3,"title":"Adapter Pattern","slug":"adapter-pattern"},{"level":3,"title":"Decorator Pattern","slug":"decorator-pattern"},{"level":3,"title":"Facade Pattern","slug":"facade-pattern"},{"level":3,"title":"Flyweight Pattern","slug":"flyweight-pattern"},{"level":3,"title":"Proxy Pattern","slug":"proxy-pattern"},{"level":3,"title":"Command Pattern","slug":"command-pattern"},{"level":3,"title":"Mediator Pattern","slug":"mediator-pattern"},{"level":3,"title":"Observer Pattern","slug":"observer-pattern"},{"level":3,"title":"Pub-Sub Pattern","slug":"pub-sub-pattern"},{"level":2,"title":"MVC Pattern","slug":"mvc-pattern"},{"level":2,"title":"jQuery Pattern","slug":"jquery-pattern"},{"level":3,"title":"Plugin Pattern","slug":"plugin-pattern"},{"level":2,"title":"Scalability Design","slug":"scalability-design"},{"level":2,"title":"Domain Driven Design","slug":"domain-driven-design"},{"level":3,"title":"Interface Layer","slug":"interface-layer"},{"level":3,"title":"Application Layer","slug":"application-layer"},{"level":3,"title":"Domain Layer","slug":"domain-layer"},{"level":3,"title":"Infrastructure Layer","slug":"infrastructure-layer"},{"level":3,"title":"DDD Layout","slug":"ddd-layout"},{"level":2,"title":"高并发系统设计","slug":"高并发系统设计"},{"level":3,"title":"Concurrent Code Layer","slug":"concurrent-code-layer"},{"level":3,"title":"Concurrent DataBase Layer","slug":"concurrent-database-layer"},{"level":3,"title":"Concurrent Architecture Layer","slug":"concurrent-architecture-layer"},{"level":2,"title":"高可用系统设计","slug":"高可用系统设计"},{"level":3,"title":"Resource Isolation","slug":"resource-isolation"},{"level":3,"title":"Load Balance Design","slug":"load-balance-design"},{"level":3,"title":"Idempotence Design","slug":"idempotence-design"},{"level":3,"title":"CAP Theory","slug":"cap-theory"},{"level":3,"title":"服务熔断","slug":"服务熔断"},{"level":3,"title":"服务限流","slug":"服务限流"},{"level":3,"title":"服务降级","slug":"服务降级"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"programming/devops/designPatternsBasicNotes.md","lastUpdated":1627735972000}',o={},c=a("h1",{id:"design-patterns-basic-notes"},[a("a",{class:"header-anchor",href:"#design-patterns-basic-notes","aria-hidden":"true"},"#"),t(" Design Patterns Basic Notes")],-1),p=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#design-patterns-basic-notes"},"Design Patterns Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#basic-patterns"},"Basic Patterns")]),a("li",null,[a("a",{href:"#modular-patterns"},"Modular Patterns")]),a("li",null,[a("a",{href:"#common-design-patterns"},"Common Design Patterns")]),a("li",null,[a("a",{href:"#mvc-pattern"},"MVC Pattern")]),a("li",null,[a("a",{href:"#jquery-pattern"},"jQuery Pattern")]),a("li",null,[a("a",{href:"#scalability-design"},"Scalability Design")]),a("li",null,[a("a",{href:"#domain-driven-design"},"Domain Driven Design")]),a("li",null,[a("a",{href:"#高并发系统设计"},"高并发系统设计")]),a("li",null,[a("a",{href:"#高可用系统设计"},"高可用系统设计")]),a("li",null,[a("a",{href:"#reference"},"Reference")])])])])])],-1),l=a("h2",{id:"basic-patterns"},[a("a",{class:"header-anchor",href:"#basic-patterns","aria-hidden":"true"},"#"),t(" Basic Patterns")],-1),u=a("h3",{id:"solid-principles"},[a("a",{class:"header-anchor",href:"#solid-principles","aria-hidden":"true"},"#"),t(" SOLID Principles")],-1),i=a("ul",null,[a("li",null,"Single Responsibility Principle 单一功能原则"),a("li",null,"Open-closed Principle 开闭原则"),a("li",null,"Liskov Substitution Principle 里氏替换原则"),a("li",null,"Interface Segregation Principle 接口隔离原则"),a("li",null,"Dependency Inversion Principle 依赖反转原则")],-1),r=a("blockquote",null,[a("p",null,"SOLID Principles")],-1),k=a("ul",null,[a("li",null,"单一职责是所有设计原则的基础"),a("li",null,"开闭原则是设计的终极目标"),a("li",null,"里氏替换原则强调的是子类替换父类后程序运行时的正确性, 它用来帮助实现开闭原则"),a("li",null,"接口隔离原则用来帮助实现里氏替换原则, 同时它也体现了单一职责"),a("li",null,"依赖倒置原则是过程式设计与面向对象设计的分水岭, 同时它也被用来指导接口隔离原则")],-1),b=a("h4",{id:"single-responsibility-principle"},[a("a",{class:"header-anchor",href:"#single-responsibility-principle","aria-hidden":"true"},"#"),t(" Single Responsibility Principle")],-1),m=a("p",null,"Too much functionality is in one class and you modify a piece of it, it can be difficult to understand how that will affect other dependent modules.",-1),d=a("h4",{id:"open-closed-principle"},[a("a",{class:"header-anchor",href:"#open-closed-principle","aria-hidden":"true"},"#"),t(" Open-Closed Principle")],-1),y=a("p",null,"Allow users to add new functionalities without changing existing code, open for extension, close for modification.",-1),f=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"Coder"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("fullName"),a("span",{class:"token punctuation"},","),t(" language"),a("span",{class:"token punctuation"},","),t(" hobby"),a("span",{class:"token punctuation"},","),t(" education"),a("span",{class:"token punctuation"},","),t(" workplace"),a("span",{class:"token punctuation"},","),t(" position")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("fullName "),a("span",{class:"token operator"},"="),t(" fullName"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("language "),a("span",{class:"token operator"},"="),t(" language"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("hobby "),a("span",{class:"token operator"},"="),t(" hobby"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("education "),a("span",{class:"token operator"},"="),t(" education"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("workplace "),a("span",{class:"token operator"},"="),t(" workplace"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position "),a("span",{class:"token operator"},"="),t(" position"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// BAD: filter by any other new property have to change CodeFilter's code."),t("\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"CoderFilter"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"filterByName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("coders"),a("span",{class:"token punctuation"},","),t(" fullName")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" coders"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"coder"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(" coder"),a("span",{class:"token punctuation"},"."),t("fullName "),a("span",{class:"token operator"},"==="),t(" fullName"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"filterByLang"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("coders"),a("span",{class:"token punctuation"},","),t(" language")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" coders"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"coder"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(" coder"),a("span",{class:"token punctuation"},"."),t("language "),a("span",{class:"token operator"},"==="),t(" language"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"filterByHobby"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("coders"),a("span",{class:"token punctuation"},","),t(" hobby")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" coders"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"coder"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(" coder"),a("span",{class:"token punctuation"},"."),t("hobby "),a("span",{class:"token operator"},"==="),t(" hobby"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// GOOD"),t("\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"CoderFilter"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"filterByProp"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("array"),a("span",{class:"token punctuation"},","),t(" propName"),a("span",{class:"token punctuation"},","),t(" value")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t("\n array"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"element"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(" element"),a("span",{class:"token punctuation"},"["),t("propName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(" value"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br")])],-1),h=a("h4",{id:"liskov-substitution-principle"},[a("a",{class:"header-anchor",href:"#liskov-substitution-principle","aria-hidden":"true"},"#"),t(" Liskov Substitution Principle")],-1),w=a("p",null,"Objects of ParentType can be replaced with objects of SubType without altering. Altering shows that SubType should not be subtype of ParentType (break Open Closed Principle), you should re-design ParentType and SubType.",-1),g=a("h4",{id:"interface-segregation-principle"},[a("a",{class:"header-anchor",href:"#interface-segregation-principle","aria-hidden":"true"},"#"),t(" Interface Segregation Principle")],-1),v=a("p",null,"Clients should not be forced to depend upon interfaces that they do not use.",-1),P=a("h4",{id:"dependency-inversion-principle"},[a("a",{class:"header-anchor",href:"#dependency-inversion-principle","aria-hidden":"true"},"#"),t(" Dependency Inversion Principle")],-1),C=a("ul",null,[a("li",null,"High-level modules should not depend on low-level modules. Both should depend on abstractions."),a("li",null,"Abstractions should not depend upon details. Details should depend on abstractions."),a("li",null,[t("Pros: "),a("ul",null,[a("li",null,"Loosely coupled modules."),a("li",null,"Better reusability."),a("li",null,"Better testability.")])])],-1),S=a("h3",{id:"literal-pattern"},[a("a",{class:"header-anchor",href:"#literal-pattern","aria-hidden":"true"},"#"),t(" Literal Pattern")],-1),j=a("ul",null,[a("li",null,"不要使用 new Boolean()/new Number()/new String()"),a("li",null,"避免使用 new Object()/new Array()")],-1),M=a("h3",{id:"closure-and-iife"},[a("a",{class:"header-anchor",href:"#closure-and-iife","aria-hidden":"true"},"#"),t(" Closure and IIFE")],-1),x=a("h3",{id:"check-pattern"},[a("a",{class:"header-anchor",href:"#check-pattern","aria-hidden":"true"},"#"),t(" Check Pattern")],-1),D=a("ul",null,[a("li",null,[a("code",null,"O || {}"),t(),a("code",null,"O || (O = {})")]),a("li",null,[a("code",null,"if (O && O.property)")]),a("li",null,[a("code",null,'if (typeof v === " ")')]),a("li",null,[a("code",null,"toString. apply(var)")])],-1),A=a("h3",{id:"函数-function"},[a("a",{class:"header-anchor",href:"#函数-function","aria-hidden":"true"},"#"),t(" 函数(function)")],-1),I=a("h4",{id:"参数"},[a("a",{class:"header-anchor",href:"#参数","aria-hidden":"true"},"#"),t(" 参数")],-1),N=a("ul",null,[a("li",null,"函数不应依赖于全局变量,实现与执行全局环境的的解耦"),a("li",null,"全局变量应以函数参数/依赖的形式,注入函数内部")],-1),O=a("h3",{id:"解耦"},[a("a",{class:"header-anchor",href:"#解耦","aria-hidden":"true"},"#"),t(" 解耦")],-1),L=a("h4",{id:"事件处理与-ui-逻辑"},[a("a",{class:"header-anchor",href:"#事件处理与-ui-逻辑","aria-hidden":"true"},"#"),t(" 事件处理与 UI 逻辑")],-1),$=a("ul",null,[a("li",null,"事件处理函数与应用逻辑函数分开成单独函数,提高代码重用率"),a("li",null,"应用逻辑函数不依赖于 event 对象,其属性值作为参数传入,易于解耦与测试")],-1),_=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(" MyApp "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token comment"},"// 事件处理函数"),t("\n "),a("span",{class:"token function-variable function"},"handleClick"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"event"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token comment"},"/* 将事件的属性作为参数,传递给应用逻辑函数\n * 使得应用逻辑函数不依赖于 event 对象,易于解耦与测试\n */"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"showPopup"),a("span",{class:"token punctuation"},"("),t("event"),a("span",{class:"token punctuation"},"."),t("clientX"),a("span",{class:"token punctuation"},","),t(" event"),a("span",{class:"token punctuation"},"."),t("clientY"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// 应用逻辑函数"),t("\n "),a("span",{class:"token function-variable function"},"showPopup"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("x"),a("span",{class:"token punctuation"},","),t(" y")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"const"),t(" popup "),a("span",{class:"token operator"},"="),t(" document"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getElementById"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'popup'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n popup"),a("span",{class:"token punctuation"},"."),t("style"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(" x "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"'px'"),a("span",{class:"token punctuation"},";"),t("\n popup"),a("span",{class:"token punctuation"},"."),t("style"),a("span",{class:"token punctuation"},"."),t("top "),a("span",{class:"token operator"},"="),t(" y "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"'px'"),a("span",{class:"token punctuation"},";"),t("\n popup"),a("span",{class:"token punctuation"},"."),t("className "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'reveal'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1),E=a("h3",{id:"env-and-config"},[a("a",{class:"header-anchor",href:"#env-and-config","aria-hidden":"true"},"#"),t(" Env and Config")],-1),B=a("p",null,"配置文件以 .env/JS(Object)/JSON/JSONP/XML/YML 格式单独存放,方便读取",-1),F=a("h3",{id:"stand-library-idioms"},[a("a",{class:"header-anchor",href:"#stand-library-idioms","aria-hidden":"true"},"#"),t(" Stand Library Idioms")],-1),T=a("ul",null,[a("li",null,[t("use "),a("code",null,"Number.isNaN"),t(" not "),a("code",null,"isNaN")]),a("li",null,[t("use "),a("code",null,"Number.isFinite"),t(" not "),a("code",null,"isFinite")])],-1),V=a("h3",{id:"other"},[a("a",{class:"header-anchor",href:"#other","aria-hidden":"true"},"#"),t(" Other")],-1),q=a("p",null,"!!result 转化成 Boolean",-1),R=a("h2",{id:"modular-patterns"},[a("a",{class:"header-anchor",href:"#modular-patterns","aria-hidden":"true"},"#"),t(" Modular Patterns")],-1),U=a("h3",{id:"object-literal"},[a("a",{class:"header-anchor",href:"#object-literal","aria-hidden":"true"},"#"),t(" Object Literal")],-1),H=a("p",null,"通过对象字面量创建命名空间",-1),z=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"namespace"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"namespaceString"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" parts "),a("span",{class:"token operator"},"="),t(" namespaceString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"split"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'.'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t("\n parent "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},","),t("\n i"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// strip redundant leading global"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("parts"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'APP'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// remove leading global"),t("\n parts "),a("span",{class:"token operator"},"="),t(" parts"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" parts"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// create a property if it doesn't exist"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" parent"),a("span",{class:"token punctuation"},"["),t("parts"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'undefined'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n parent"),a("span",{class:"token punctuation"},"["),t("parts"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"//关键: 向内嵌套"),t("\n parent "),a("span",{class:"token operator"},"="),t(" parent"),a("span",{class:"token punctuation"},"["),t("parts"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"// 返回最内层模块"),t("\n "),a("span",{class:"token keyword"},"return"),t(" parent"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),G=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// assign returned value to a local var"),t("\n"),a("span",{class:"token keyword"},"var"),t(" module2 "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"namespace"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'APP.modules.module2'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nmodule2 "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),t("modules"),a("span",{class:"token punctuation"},"."),t("module2"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// true"),t("\n"),a("span",{class:"token comment"},"// skip initial `APP`"),t("\n"),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"namespace"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'modules.module51'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"// long namespace"),t("\n"),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"namespace"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'once.upon.a.time.there.was.this.long.nested.property'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),W=a("h3",{id:"iife-pattern"},[a("a",{class:"header-anchor",href:"#iife-pattern","aria-hidden":"true"},"#"),t(" IIFE Pattern")],-1),Q=a("p",null,"立即函数模式, 通过调用立即函数,返回一个对象,暴露(exposed to public)公共接口(特权/公共方法):",-1),X=a("ul",null,[a("li",null,"闭包: 定义私有变量与特权方法"),a("li",null,"返回对象: 即使通过外部代码改变返回对象的接口,也不会影响原接口")],-1),J=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" obj "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// private member"),t("\n "),a("span",{class:"token keyword"},"var"),t(" name "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},'"tazimi"'),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token comment"},"// private method"),t("\n "),a("span",{class:"token comment"},"// excluded in return object"),t("\n\n "),a("span",{class:"token comment"},"// privileged method"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"getName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" name"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"setName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"n"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" n "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'string'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n name "),a("span",{class:"token operator"},"="),t(" n"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token comment"},"// public method"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"logName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("name"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// 闭包"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// 公共接口: 特权/公共方法"),t("\n\n "),a("span",{class:"token comment"},"// 特权方法"),t("\n getName"),a("span",{class:"token operator"},":"),t(" getName"),a("span",{class:"token punctuation"},","),t("\n setName"),a("span",{class:"token operator"},":"),t(" setName"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token comment"},"// 公共方法"),t("\n log"),a("span",{class:"token operator"},":"),t(" logName"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br")])],-1),K=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" App "),a("span",{class:"token operator"},"="),t(" App "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nApp"),a("span",{class:"token punctuation"},"."),t("utils "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" val "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getValue"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" val"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"setValue"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"newVal"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n val "),a("span",{class:"token operator"},"="),t(" newVal"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// also introduce a new sub-namespace"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("tools "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("App"),a("span",{class:"token punctuation"},"."),t("utils"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// inject new behavior into the tools namespace"),t("\n"),a("span",{class:"token comment"},"// which we defined via the utilities module"),t("\n\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"diagnose"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'diagnosis'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("App"),a("span",{class:"token punctuation"},"."),t("utils"),a("span",{class:"token punctuation"},"."),t("tools"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br")])],-1),Y=a("ul",null,[a("li",null,"jQuery Plugin Pattern: 通过给立即函数传参,注入全局变量/其他依赖")],-1),Z=a("h3",{id:"umd-pattern"},[a("a",{class:"header-anchor",href:"#umd-pattern","aria-hidden":"true"},"#"),t(" UMD Pattern")],-1),nn=a("p",null,"Universal Module Definition:",-1),sn=a("ul",null,[a("li",null,"先判断是否支持 Node.js 的模块(exports),存在则使用 Node.js 模块模式"),a("li",null,"再判断是否支持 AMD(define),存在则使用 AMD 方式加载模块")],-1),an=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("window"),a("span",{class:"token punctuation"},","),t(" factory")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" exports "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'object'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"factory"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" define "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'function'"),t(),a("span",{class:"token operator"},"&&"),t(" define"),a("span",{class:"token punctuation"},"."),t("amd"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"define"),a("span",{class:"token punctuation"},"("),t("factory"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n window"),a("span",{class:"token punctuation"},"."),t("eventUtil "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"factory"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"//module ..."),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),tn=a("h2",{id:"common-design-patterns"},[a("a",{class:"header-anchor",href:"#common-design-patterns","aria-hidden":"true"},"#"),t(" Common Design Patterns")],-1),en=a("p",null,[a("img",{src:"/awesome-notes/assets/DesignPatterns.df4b7baf.png",alt:"Common Design Patterns"})],-1),on=a("h3",{id:"classification"},[a("a",{class:"header-anchor",href:"#classification","aria-hidden":"true"},"#"),t(" Classification")],-1),cn=a("h4",{id:"creation-patterns"},[a("a",{class:"header-anchor",href:"#creation-patterns","aria-hidden":"true"},"#"),t(" Creation Patterns")],-1),pn=a("ul",null,[a("li",null,"Factory Method(工厂方法) | 通过将数据和事件接口化来构建若干个子类。"),a("li",null,"Abstract Factory(抽象工厂) | 建立若干族类的一个实例,这个实例不需要具体类的细节信息。(抽象类)"),a("li",null,"Builder(建造者) | 将对象的构建方法和其表现形式分离开来,总是构建相同类型的对象。"),a("li",null,"Prototype(原型) | 一个完全初始化的实例,用于拷贝或者克隆。"),a("li",null,"Singleton(单例) | 一个类只有唯一的一个实例,这个实例在整个程序中有一个全局的访问点。")],-1),ln=a("h4",{id:"structural-patterns"},[a("a",{class:"header-anchor",href:"#structural-patterns","aria-hidden":"true"},"#"),t(" Structural Patterns")],-1),un=a("ul",null,[a("li",null,"Adapter(适配器) : 将不同类的接口进行匹配,调整,这样尽管内部接口不兼容但是不同的类还是可以协同工作的。"),a("li",null,"Bridge(桥接模式) : 将对象的接口从其实现中分离出来,这样对象的实现和接口可以独立的变化。"),a("li",null,"Composite(组合模式) : 通过将简单可组合的对象组合起来,构成一个完整的对象,这个对象的能力将会超过这些组成部分的能力的总和,即会有新的能力产生。"),a("li",null,"Decorator(装饰器) : 动态给对象增加一些可替换的处理流程。"),a("li",null,"Facade(外观模式) : 一个类隐藏了内部子系统的复杂度,只暴露出一些简单的接口。"),a("li",null,"Flyweight(享元模式) : 一个细粒度对象,用于将包含在其它地方的信息 在不同对象之间高效地共享。"),a("li",null,"Proxy(代理模式) : 一个充当占位符的对象用来代表一个真实的对象。")],-1),rn=a("h4",{id:"behavioral-patterns"},[a("a",{class:"header-anchor",href:"#behavioral-patterns","aria-hidden":"true"},"#"),t(" Behavioral Patterns")],-1),kn=a("ul",null,[a("li",null,"Interpreter(解释器) : 将语言元素包含在一个应用中的一种方式,用于匹配目标语言的语法。"),a("li",null,"Template Method(模板方法) : 在一个方法中为某个算法建立一层外壳,将算法的具体步骤交付给子类去做。"),a("li",null,"Chain of Responsibility(响应链) : 一种将请求在一串对象中传递的方式,寻找可以处理这个请求的对象。"),a("li",null,"Command(命令) : 封装命令请求为一个对象,从而使记录日志,队列缓存请求,未处理请求进行错误处理 这些功能称为可能。"),a("li",null,"Iterator(迭代器) : 在不需要直到集合内部工作原理的情况下,顺序访问一个集合里面的元素。"),a("li",null,"Mediator(中介者模式) : 在类之间定义简化的通信方式,用于避免类之间显式的持有彼此的引用。"),a("li",null,"Observer(观察者模式) : 用于将变化通知给多个类的方式,可以保证类之间的一致性。"),a("li",null,"State(状态) : 当对象状态改变时,改变对象的行为。"),a("li",null,"Strategy(策略) : 将算法封装到类中,将选择和实现分离开来。"),a("li",null,"Visitor(访问者) : 为类增加新的操作而不改变类本身。")],-1),bn=a("h3",{id:"prototype"},[a("a",{class:"header-anchor",href:"#prototype","aria-hidden":"true"},"#"),t(" Prototype")],-1),mn=a("p",null,"关键方法 clone()",-1),dn=a("h3",{id:"bridge"},[a("a",{class:"header-anchor",href:"#bridge","aria-hidden":"true"},"#"),t(" Bridge")],-1),yn=a("p",null,"分离抽象和实现/分离对象的两种不同属性",-1),fn=a("p",null,"e.g 从 2 个不同维度上扩展对象",-1),hn=a("h3",{id:"composite"},[a("a",{class:"header-anchor",href:"#composite","aria-hidden":"true"},"#"),t(" Composite")],-1),wn=a("p",null,"树形结构:",-1),gn=a("ul",null,[a("li",null,[t("根结点 "),a("ul",null,[a("li",null,"Component 抽象对象/接口 采用最大宽接口,定义内点和叶点的操作"),a("li",null,"将内点特有的操作集设为缺省操作集(空实现)")])]),a("li",null,[t("内点 "),a("ul",null,[a("li",null,"持有父结点和子节点的引用(可使用 Flyweight 模式实现共享)"),a("li",null,"操作集:内点操作集(可添加/删除组件)")])]),a("li",null,[t("叶点 "),a("ul",null,[a("li",null,"持有父结点引用"),a("li",null,"操作集:叶点操作集(不可添加/删除组件)")])])],-1),vn=a("h3",{id:"strategy"},[a("a",{class:"header-anchor",href:"#strategy","aria-hidden":"true"},"#"),t(" Strategy")],-1),Pn=a("p",null,"改变对象的内核/算法, 一个 Strategy 对象封装一个算法",-1),Cn=a("h3",{id:"state"},[a("a",{class:"header-anchor",href:"#state","aria-hidden":"true"},"#"),t(" State")],-1),Sn=a("p",null,"一个 State 对象封装一个与状态相关的行为",-1),jn=a("h3",{id:"iterator"},[a("a",{class:"header-anchor",href:"#iterator","aria-hidden":"true"},"#"),t(" Iterator")],-1),Mn=a("p",null,"一个 Iterator 对象封装访问和遍历一个聚集对象中的各个构件的方法",-1),xn=a("h3",{id:"ioc-and-di"},[a("a",{class:"header-anchor",href:"#ioc-and-di","aria-hidden":"true"},"#"),t(" IOC and DI")],-1),Dn=a("ul",null,[a("li",null,"IOC (inversion of control) 控制反转模式;控制反转是将组件间的依赖关系从程序内部提到外部来管理"),a("li",null,"DI (dependency injection) 依赖注入模式;依赖注入是指将组件的依赖通过外部以参数或其他形式注入")],-1),An=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"DbMysql"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"query"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"Controller"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" $db"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"__construct"),a("span",{class:"token punctuation"},"("),t("$dbMysql"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("db "),a("span",{class:"token operator"},"="),t(" $dbMysql"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("db"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"query"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n$db "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"DbMysql"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$c "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Controller"),a("span",{class:"token punctuation"},"("),t("$db"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$c"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),In=a("p",null,"With IOC container:",-1),Nn=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"DbMysql"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"__construct"),a("span",{class:"token punctuation"},"("),t("$host"),a("span",{class:"token punctuation"},","),t(" $name"),a("span",{class:"token punctuation"},","),t(" $pwd"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// do something"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"query"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n echo __METHOD__ "),a("span",{class:"token punctuation"},"."),t(" PHP_EOL"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"DbRedis"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"__construct"),a("span",{class:"token punctuation"},"("),t("$host"),a("span",{class:"token punctuation"},","),t(" $name"),a("span",{class:"token punctuation"},","),t(" $pwd"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// do something"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"set"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n echo __METHOD__ "),a("span",{class:"token punctuation"},"."),t(" PHP_EOL"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"class"),t(" controller "),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" $mysql"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"public"),t(" $redis"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"__construct"),a("span",{class:"token punctuation"},"("),t("$mysql"),a("span",{class:"token punctuation"},","),t(" $redis"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("mysql "),a("span",{class:"token operator"},"="),t(" $mysql"),a("span",{class:"token punctuation"},";"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("redis "),a("span",{class:"token operator"},"="),t(" $redis"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("mysql"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"query"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("redis"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"set"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"Container"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" $bindings "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"bind"),a("span",{class:"token punctuation"},"("),t("$key"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Closure"),t(" $value"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("bindings"),a("span",{class:"token punctuation"},"["),t("$key"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" $value"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"make"),a("span",{class:"token punctuation"},"("),t("$key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token operator"},"="),t(" $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("bindings"),a("span",{class:"token punctuation"},"["),t("$key"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" $"),a("span",{class:"token keyword"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n$app "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Container"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"bind"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'mysql'"),a("span",{class:"token punctuation"},","),t(" function "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"DbMysql"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'host'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'name'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'pwd'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"bind"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'redis'"),a("span",{class:"token punctuation"},","),t(" function "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"DbRedis"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'host'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'name'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'pwd'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"bind"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'controller'"),a("span",{class:"token punctuation"},","),t(" function "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(" use "),a("span",{class:"token punctuation"},"("),t("$app"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Controller"),a("span",{class:"token punctuation"},"("),t("$app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"make"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'mysql'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t(" $app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"make"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'redis'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$controller "),a("span",{class:"token operator"},"="),t(" $app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"make"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'controller'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$controller"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"/** * 输出: * DbMysql::query * DbRedis::set */"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br")])],-1),On=a("p",null,"With dependency injection:",-1),Ln=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// dependency provider"),t("\n"),a("span",{class:"token keyword"},"export"),t(),a("span",{class:"token keyword"},"interface"),t(),a("span",{class:"token class-name"},"IProvider"),a("span",{class:"token operator"},"<"),a("span",{class:"token constant"},"T"),a("span",{class:"token operator"},">"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"provide"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token constant"},"T"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n@"),a("span",{class:"token function"},"injectable"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t("\n"),a("span",{class:"token keyword"},"export"),t(),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"NameProvider"),t(),a("span",{class:"token keyword"},"implements"),t(),a("span",{class:"token class-name"},"IProvider"),a("span",{class:"token operator"},"<"),t("string"),a("span",{class:"token operator"},">"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"provide"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},'"World"'),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// top module"),t("\n"),a("span",{class:"token keyword"},"import"),t(),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token keyword"},"as"),t(" React "),a("span",{class:"token keyword"},"from"),t(),a("span",{class:"token string"},'"react"'),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"import"),t(),a("span",{class:"token punctuation"},"{"),t(" IProvider "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"from"),t(),a("span",{class:"token string"},'"./providers"'),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"export"),t(),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"Hello"),t(),a("span",{class:"token keyword"},"extends"),t(),a("span",{class:"token class-name"},[t("React"),a("span",{class:"token punctuation"},"."),t("Component")]),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"private"),t(" readonly nameProvider"),a("span",{class:"token operator"},":"),t(" IProvider"),a("span",{class:"token operator"},"<"),t("string"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token function"},"render"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token operator"},"<"),t("h1"),a("span",{class:"token operator"},">"),t("Hello "),a("span",{class:"token punctuation"},"{"),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("nameProvider"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"provide"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token operator"},"!"),a("span",{class:"token operator"},"<"),a("span",{class:"token operator"},"/"),t("h1"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br")])],-1),$n=a("h3",{id:"class-pattern"},[a("a",{class:"header-anchor",href:"#class-pattern","aria-hidden":"true"},"#"),t(" Class Pattern")],-1),_n=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"Person"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("firstName"),a("span",{class:"token punctuation"},","),t(" lastName")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("firstName "),a("span",{class:"token operator"},"="),t(" firstName"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("lastName "),a("span",{class:"token operator"},"="),t(" lastName"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("gender "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'male'"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},'// Define a subclass constructor for for "Superhero":'),t("\n"),a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"Superhero"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("firstName"),a("span",{class:"token punctuation"},","),t(" lastName"),a("span",{class:"token punctuation"},","),t(" powers")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// Invoke the superclass constructor on the new object"),t("\n "),a("span",{class:"token comment"},"// then use .call() to invoke the constructor as a method of"),t("\n "),a("span",{class:"token comment"},"// the object to be initialized."),t("\n "),a("span",{class:"token function"},"Person"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(" firstName"),a("span",{class:"token punctuation"},","),t(" lastName"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},'// Finally, store their powers, a new array of traits not found in a normal "Person"'),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("powers "),a("span",{class:"token operator"},"="),t(" powers"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token class-name"},"SuperHero"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(" Object"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"create"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Person"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),En=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" superman "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Superhero"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Clark'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Kent'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'flight'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'heat-vision'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nconsole"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("superman"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Bn=a("h3",{id:"mix-in-pattern"},[a("a",{class:"header-anchor",href:"#mix-in-pattern","aria-hidden":"true"},"#"),t(" Mix-In Pattern")],-1),Fn=a("p",null,"将多个对象的属性混入同一个对象,达到继承/扩展/组合的效果",-1),Tn=a("ul",null,[a("li",null,"不改变原型链")],-1),Vn=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"mix"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" arg"),a("span",{class:"token punctuation"},","),t("\n prop"),a("span",{class:"token punctuation"},","),t("\n child "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("arg "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" arg "),a("span",{class:"token operator"},"<"),t(" arguments"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" arg "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("prop "),a("span",{class:"token keyword"},"in"),t(" arguments"),a("span",{class:"token punctuation"},"["),t("arg"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},"["),t("arg"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"hasOwnProperty"),a("span",{class:"token punctuation"},"("),t("prop"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n child"),a("span",{class:"token punctuation"},"["),t("prop"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" arguments"),a("span",{class:"token punctuation"},"["),t("arg"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("prop"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" child"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br")])],-1),qn=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" cake "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"mix"),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token punctuation"},"{"),t(" eggs"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" large"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token boolean"},"true"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"{"),t(" butter"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(" salted"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token boolean"},"true"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"{"),t(" flour"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'3 cups'"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"{"),t(" sugar"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'sure!'"),t(),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Rn=a("ul",null,[a("li",null,"改变原型链")],-1),Un=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// Extend an existing object with a method from another"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"mix"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("receivingClass"),a("span",{class:"token punctuation"},","),t(" givingClass")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// mix-in provide certain methods"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" len "),a("span",{class:"token operator"},"="),t(" arguments"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" len"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n receivingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("arguments"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t("\n givingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("arguments"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"// mix-in provide obj"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" methodName "),a("span",{class:"token keyword"},"in"),t(" givingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("receivingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("methodName"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n receivingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("methodName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t("\n givingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("methodName"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1),Hn=a("h3",{id:"singleton-pattern"},[a("a",{class:"header-anchor",href:"#singleton-pattern","aria-hidden":"true"},"#"),t(" Singleton Pattern")],-1),zn=a("p",null,"原型与构造函数指针运作正常",-1),Gn=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Universe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// 缓存实例"),t("\n "),a("span",{class:"token keyword"},"var"),t(" instance"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// anti-Self-Defined Function Pattern"),t("\n "),a("span",{class:"token comment"},"// 反-自定义函数模式: 先重写,再初始化"),t("\n "),a("span",{class:"token function-variable function"},"Universe"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Universe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" instance"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// 保存原型,使其一直保持于同一位置"),t("\n "),a("span",{class:"token comment"},"// (this指针指向不重要)"),t("\n "),a("span",{class:"token class-name"},"Universe"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n\n instance "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Universe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// 重定向constructor指针"),t("\n instance"),a("span",{class:"token punctuation"},"."),t("constructor "),a("span",{class:"token operator"},"="),t(" Universe"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// 功能代码"),t("\n instance"),a("span",{class:"token punctuation"},"."),t("start_time "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n instance"),a("span",{class:"token punctuation"},"."),t("bang "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'Big'"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" instance"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br")])],-1),Wn=a("h3",{id:"abstract-factory"},[a("a",{class:"header-anchor",href:"#abstract-factory","aria-hidden":"true"},"#"),t(" Abstract Factory")],-1),Qn=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" AbstractVehicleFactory "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// Storage for our vehicle types"),t("\n "),a("span",{class:"token keyword"},"var"),t(" types "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_getVehicle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("type"),a("span",{class:"token punctuation"},","),t(" customizations")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" Vehicle "),a("span",{class:"token operator"},"="),t(" types"),a("span",{class:"token punctuation"},"["),t("type"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" Vehicle "),a("span",{class:"token operator"},"?"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Vehicle"),a("span",{class:"token punctuation"},"("),t("customizations"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_registerVehicle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("type"),a("span",{class:"token punctuation"},","),t(" Vehicle")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token class-name"},"Vehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// only register classes that fulfill the vehicle contract"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("prototype"),a("span",{class:"token punctuation"},"."),t("drive "),a("span",{class:"token operator"},"&&"),t(" prototype"),a("span",{class:"token punctuation"},"."),t("breakDown"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n types"),a("span",{class:"token punctuation"},"["),t("type"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" Vehicle"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" AbstractVehicleFactory"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n getVehicle"),a("span",{class:"token operator"},":"),t(" _getVehicle"),a("span",{class:"token punctuation"},","),t("\n registerVehicle"),a("span",{class:"token operator"},":"),t(" _registerVehicle"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br")])],-1),Xn=a("h3",{id:"factory-method"},[a("a",{class:"header-anchor",href:"#factory-method","aria-hidden":"true"},"#"),t(" Factory Method")],-1),Jn=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[t("module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"VehicleFactory"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" publicVehicle "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Object"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// specific factory"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Car"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"options"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'car'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("doors "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("doors "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("state "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("state "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'brand new'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'silver'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Truck"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"options"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'truck'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("state "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("state "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'used'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("wheelSize "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("wheelSize "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'large'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'blue'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// public features of vehicle , added to __proto__"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" args "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("args"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' - run with: '"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"'km/s'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("args"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object Number]'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"="),t(" args"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_withColor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" args "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("args"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token string"},"'The color of this '"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' product is : '"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("color\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("args"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object String]'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" args"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"// provide a function to change other public features"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_reform"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("funcName"),a("span",{class:"token punctuation"},","),t(" newFunc")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token keyword"},"typeof"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'function'"),t(),a("span",{class:"token operator"},"||"),t("\n "),a("span",{class:"token keyword"},"typeof"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'function'"),t("\n "),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"delete"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" newFunc"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"// provide a function to add new public features"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_addFeature"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("funcName"),a("span",{class:"token punctuation"},","),t(" newFunc")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'undefined'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" newFunc"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" newFunc"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// private features, added to obj"),t("\n\n "),a("span",{class:"token comment"},"// core: create method"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"create"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"options"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" vehicleClass "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"''"),a("span",{class:"token punctuation"},","),t("\n newVehicle "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("options"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'car'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n vehicleClass "),a("span",{class:"token operator"},"="),t(" Car"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n vehicleClass "),a("span",{class:"token operator"},"="),t(" Truck"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// create new vehicle with options, by pre-defined specific constructor"),t("\n newVehicle "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"vehicleClass"),a("span",{class:"token punctuation"},"("),t("options"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// set up prototype"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("__proto__ "),a("span",{class:"token operator"},"="),t(" publicVehicle"),a("span",{class:"token punctuation"},";"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(" publicVehicle"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// add public feature"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),t("run "),a("span",{class:"token operator"},"="),t(" _run"),a("span",{class:"token punctuation"},";"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),t("withColor "),a("span",{class:"token operator"},"="),t(" _withColor"),a("span",{class:"token punctuation"},";"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),t("reform "),a("span",{class:"token operator"},"="),t(" _reform"),a("span",{class:"token punctuation"},";"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),t("addFeature "),a("span",{class:"token operator"},"="),t(" _addFeature"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// add private(separately) feature"),t("\n\n "),a("span",{class:"token comment"},"// return new obj"),t("\n "),a("span",{class:"token keyword"},"return"),t(" newVehicle"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// define more factory"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n vehicleFactory"),a("span",{class:"token operator"},":"),t(" VehicleFactory"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br"),a("span",{class:"line-number"},"83"),a("br"),a("span",{class:"line-number"},"84"),a("br"),a("span",{class:"line-number"},"85"),a("br"),a("span",{class:"line-number"},"86"),a("br"),a("span",{class:"line-number"},"87"),a("br"),a("span",{class:"line-number"},"88"),a("br"),a("span",{class:"line-number"},"89"),a("br"),a("span",{class:"line-number"},"90"),a("br"),a("span",{class:"line-number"},"91"),a("br"),a("span",{class:"line-number"},"92"),a("br"),a("span",{class:"line-number"},"93"),a("br"),a("span",{class:"line-number"},"94"),a("br"),a("span",{class:"line-number"},"95"),a("br"),a("span",{class:"line-number"},"96"),a("br"),a("span",{class:"line-number"},"97"),a("br")])],-1),Kn=a("h3",{id:"adapter-pattern"},[a("a",{class:"header-anchor",href:"#adapter-pattern","aria-hidden":"true"},"#"),t(" Adapter Pattern")],-1),Yn=a("p",null,"适配器通过内部使用新接口规定的属性/方法, 创建一个外观与旧接口一致 的方法",-1),Zn=a("ul",null,[a("li",null,"old.method();"),a("li",null,"adapter.method(); // 实现此 method 时,使用了新接口规定的属性/方法")],-1),ns=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// old interface"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Shipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"request"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("zipStart"),a("span",{class:"token punctuation"},","),t(" zipEnd"),a("span",{class:"token punctuation"},","),t(" weight")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// ..."),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'$49.75'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// new interface"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"AdvancedShipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"login"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"credentials"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"/* ... */"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"setStart"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"start"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"/* ... */"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"setDestination"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"destination"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"/* ... */"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"calculate"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"weight"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'$39.50'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// adapter interface"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"AdapterShipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"credentials"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" shipping "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"AdvancedShipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"login"),a("span",{class:"token punctuation"},"("),t("credentials"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"request"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("zipStart"),a("span",{class:"token punctuation"},","),t(" zipEnd"),a("span",{class:"token punctuation"},","),t(" weight")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"setStart"),a("span",{class:"token punctuation"},"("),t("zipStart"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"setDestination"),a("span",{class:"token punctuation"},"("),t("zipEnd"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"calculate"),a("span",{class:"token punctuation"},"("),t("weight"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br")])],-1),ss=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" shipping "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Shipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"var"),t(" adapterShipping "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"AdapterShipping"),a("span",{class:"token punctuation"},"("),t("credentials"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// original shipping object and interface"),t("\n"),a("span",{class:"token keyword"},"var"),t(" cost "),a("span",{class:"token operator"},"="),t(" shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'78701'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'10010'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2 lbs'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nlog"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Old cost: '"),t(),a("span",{class:"token operator"},"+"),t(" cost"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"// new shipping object with adapted interface"),t("\ncost "),a("span",{class:"token operator"},"="),t(" adapter"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'78701'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'10010'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2 lbs'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),as=a("h3",{id:"decorator-pattern"},[a("a",{class:"header-anchor",href:"#decorator-pattern","aria-hidden":"true"},"#"),t(" Decorator Pattern")],-1),ts=a("ul",null,[a("li",null,"重写/重载/扩展对象原有的行为(method),但不改变对象原有属性"),a("li",null,"可以添加新属性,并围绕新属性扩展对象的原行为 e.g 原对象只会说中文,装饰后同时说中文与英文"),a("li",null,"避免了通过继承来为类型添加新的职责的形式可取,通过继承的方式容易造成子类的膨胀"),a("li",null,"保持接口的一致性,动态改变对象的外观/职责"),a("li",null,"ConcreteDecorator 类: private ClassName component;(拥有一个对象引用)")],-1),es=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"__decorate"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("decorators"),a("span",{class:"token punctuation"},","),t(" target"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" desc")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"const"),t(" argumentsLength "),a("span",{class:"token operator"},"="),t(" arguments"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"let"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"let"),t(" decorator"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("argumentsLength "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// class decorator"),t("\n descriptorOrTarget "),a("span",{class:"token operator"},"="),t(" target"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("desc "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// method decorator"),t("\n descriptorOrTarget "),a("span",{class:"token operator"},"="),t(" Object"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getOwnPropertyDescriptor"),a("span",{class:"token punctuation"},"("),t("target"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"let"),t(" i "),a("span",{class:"token operator"},"="),t(" decorators"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"-"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},">="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("decorator "),a("span",{class:"token operator"},"="),t(" decorators"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("argumentsLength "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// if the decorator function returns a value use it;"),t("\n "),a("span",{class:"token comment"},"// otherwise use the original."),t("\n descriptorOrTarget "),a("span",{class:"token operator"},"="),t("\n "),a("span",{class:"token function"},"decorator"),a("span",{class:"token punctuation"},"("),t("descriptorOrTarget"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"||"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// if the decorator function returns a descriptor use it;"),t("\n "),a("span",{class:"token comment"},"// otherwise use the original."),t("\n descriptorOrTarget "),a("span",{class:"token operator"},"="),t("\n "),a("span",{class:"token function"},"decorator"),a("span",{class:"token punctuation"},"("),t("target"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"||"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("argumentsLength "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"3"),t(),a("span",{class:"token operator"},"&&"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n Object"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"defineProperty"),a("span",{class:"token punctuation"},"("),t("target"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br")])],-1),os=a("h4",{id:"decorator-implementation"},[a("a",{class:"header-anchor",href:"#decorator-implementation","aria-hidden":"true"},"#"),t(" Decorator Implementation")],-1),cs=a("p",null,"关键在于实现传递方式, 两种方式:",-1),ps=a("ul",null,[a("li",null,"uber 属性获得每次装饰后结果"),a("li",null,"循环叠加每次装饰后结果")],-1),ls=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 构造函数"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Sale"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price "),a("span",{class:"token operator"},"="),t(" price "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getPrice"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// 定义具体装饰器"),t("\n"),a("span",{class:"token comment"},"// 通过uber属性获得上一次装饰后的结果"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("fedTax "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" price "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("uber"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n price "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token punctuation"},"("),t("price "),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" price"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("quebec "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" price "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("uber"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n price "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token punctuation"},"("),t("price "),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token number"},"7.5"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" price"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("money "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'$'"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("uber"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"toFixed"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("cdn "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'CDN$ '"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("uber"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"toFixed"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"decorate"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"decorator"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"F"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n overrides "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("constructor"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"["),t("decorator"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t("\n i"),a("span",{class:"token punctuation"},","),t("\n newObj"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// 临时代理构造函数"),t("\n "),a("span",{class:"token class-name"},"F"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n newObj "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"F"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// 传递实现的关键"),t("\n "),a("span",{class:"token comment"},"// 通过uber属性获得上一次装饰后的结果"),t("\n newObj"),a("span",{class:"token punctuation"},"."),t("uber "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token class-name"},"F"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token keyword"},"in"),t(" overrides"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("overrides"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"hasOwnProperty"),a("span",{class:"token punctuation"},"("),t("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n newObj"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" overrides"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" newObj"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br")])],-1),us=a("h4",{id:"decorators-list"},[a("a",{class:"header-anchor",href:"#decorators-list","aria-hidden":"true"},"#"),t(" Decorators List")],-1),is=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 构造函数"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Sale"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price "),a("span",{class:"token operator"},"="),t(" price "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),t(),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("decorators_list "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getPrice"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// 定义具体装饰器"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("fedTax "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" price "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token punctuation"},"("),t("price "),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("quebec "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" price "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token punctuation"},"("),t("price "),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token number"},"7.5"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("money "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'$'"),t(),a("span",{class:"token operator"},"+"),t(" price"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"toFixed"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"decorate"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"decorator"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("decorators_list"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),t("decorator"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getPrice"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" price "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price"),a("span",{class:"token punctuation"},","),t("\n i"),a("span",{class:"token punctuation"},","),t("\n max "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("decorators_list"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},","),t("\n name"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" max"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n name "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("decorators_list"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// 传递实现的关键"),t("\n "),a("span",{class:"token comment"},"// 通过循环叠加上一次装饰后的结果"),t("\n price "),a("span",{class:"token operator"},"="),t(" Sale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"["),t("name"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),t("price"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" price"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br")])],-1),rs=a("h4",{id:"实现-2"},[a("a",{class:"header-anchor",href:"#实现-2","aria-hidden":"true"},"#"),t(" 实现 2")],-1),ks=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// The constructor to decorate"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"MacBook"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"cost"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token number"},"997"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"screenSize"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token number"},"11.6"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// Decorator 1"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Memory"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"macBook"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" v "),a("span",{class:"token operator"},"="),t(" macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"cost"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"cost"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" v "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"75"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// Decorator 2"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Engraving"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"macBook"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" v "),a("span",{class:"token operator"},"="),t(" macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"cost"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"cost"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" v "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"200"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// Decorator 3"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Insurance"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"macBook"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" v "),a("span",{class:"token operator"},"="),t(" macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"cost"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"cost"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" v "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"250"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br")])],-1),bs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" mb "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MacBook"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token function"},"Memory"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token function"},"Engraving"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token function"},"Insurance"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// Outputs: 1522"),t("\nconsole"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"cost"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// Outputs: 11.6"),t("\nconsole"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"screenSize"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),ms=a("h3",{id:"facade-pattern"},[a("a",{class:"header-anchor",href:"#facade-pattern","aria-hidden":"true"},"#"),t(" Facade Pattern")],-1),ds=a("p",null,"将多个复杂的子系统封装+合并,实现一个复杂功能,但只暴露一个简单的接口 - 封装复杂逻辑",-1),ys=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" sabertazimi "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\nsabertazimi"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"addMyEvent"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("el"),a("span",{class:"token punctuation"},","),t(" ev"),a("span",{class:"token punctuation"},","),t(" fn")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("el"),a("span",{class:"token punctuation"},"."),t("addEventListener"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n el"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"addEventListener"),a("span",{class:"token punctuation"},"("),t("ev"),a("span",{class:"token punctuation"},","),t(" fn"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("el"),a("span",{class:"token punctuation"},"."),t("attachEvent"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n el"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"attachEvent"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'on'"),t(),a("span",{class:"token operator"},"+"),t(" ev"),a("span",{class:"token punctuation"},","),t(" fn"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n el"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'on'"),t(),a("span",{class:"token operator"},"+"),t(" ev"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" fn"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),fs=a("h3",{id:"flyweight-pattern"},[a("a",{class:"header-anchor",href:"#flyweight-pattern","aria-hidden":"true"},"#"),t(" Flyweight Pattern")],-1),hs=a("p",null,"减小内存开销:",-1),ws=a("ul",null,[a("li",null,"内在信息 - 对象中的内部方法所需信息/属性, 一个单独的享元可替代大量具有相同内在信息的对象"),a("li",null,"外部状态作为方法参数:使之适应不同的外部状态(context)——对象实例差异处"),a("li",null,"某个类型的对象有大量的实例,对这些实例进行分类,合并相同分类的对象,只创建少量实例(享元)"),a("li",null,"通过享元工厂来管理一组享元,当所需享元已存在时,返回已存在享元;当所需享元不存在时,创建新享元")],-1),gs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Flyweight"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("make "),a("span",{class:"token operator"},"="),t(" make"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("model "),a("span",{class:"token operator"},"="),t(" model"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("processor "),a("span",{class:"token operator"},"="),t(" processor"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"var"),t(" FlyWeightFactory "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" flyweights "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"get"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// 不存在所需享元,新建新享元"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("flyweights"),a("span",{class:"token punctuation"},"["),t("make "),a("span",{class:"token operator"},"+"),t(" model"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n flyweights"),a("span",{class:"token punctuation"},"["),t("make "),a("span",{class:"token operator"},"+"),t(" model"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Flyweight"),a("span",{class:"token punctuation"},"("),t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" flyweights"),a("span",{class:"token punctuation"},"["),t("make "),a("span",{class:"token operator"},"+"),t(" model"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token function-variable function"},"getCount"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" count "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" f "),a("span",{class:"token keyword"},"in"),t(" flyweights"),a("span",{class:"token punctuation"},")"),t(" count"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" count"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"Computer"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},","),t(" memory"),a("span",{class:"token punctuation"},","),t(" tag")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("flyweight "),a("span",{class:"token operator"},"="),t(" FlyWeightFactory"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("memory "),a("span",{class:"token operator"},"="),t(" memory"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("tag "),a("span",{class:"token operator"},"="),t(" tag"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getMake"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("flyweight"),a("span",{class:"token punctuation"},"."),t("make"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// ..."),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"ComputerCollection"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" computers "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"var"),t(" count "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"add"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},","),t(" memory"),a("span",{class:"token punctuation"},","),t(" tag")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n computers"),a("span",{class:"token punctuation"},"["),t("tag"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Computer"),a("span",{class:"token punctuation"},"("),t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},","),t(" memory"),a("span",{class:"token punctuation"},","),t(" tag"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n count"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token function-variable function"},"get"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"tag"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" computers"),a("span",{class:"token punctuation"},"["),t("tag"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token function-variable function"},"getCount"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" count"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" computers "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"ComputerCollection"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Dell'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Studio XPS'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'5G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Y755P'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Dell'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Studio XPS'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'6G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'X997T'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Dell'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Studio XPS'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'NT777'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Dell'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Studio XPS'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'0J88A'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'HP'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Envy'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'4G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'CNU883701'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'HP'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Envy'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'TXU003283'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Computers: '"),t(),a("span",{class:"token operator"},"+"),t(" computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Flyweights: '"),t(),a("span",{class:"token operator"},"+"),t(" FlyWeightFactory"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br")])],-1),vs=a("h3",{id:"proxy-pattern"},[a("a",{class:"header-anchor",href:"#proxy-pattern","aria-hidden":"true"},"#"),t(" Proxy Pattern")],-1),Ps=a("p",null,"通过一个代理对象,临时存储原对象方法调用产生的一系列结果(新建对象),减少重复对象的产生",-1),Cs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"GeoCoder"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getLatLng"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"address"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'Amsterdam'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'52.3700° N, 4.8900° E'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'London'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'51.5171° N, 0.1062° W'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'Paris'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'48.8742° N, 2.3470° E'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'Berlin'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'52.5233° N, 13.4127° E'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"''"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"GeoProxy"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" geocoder "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"GeoCoder"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"var"),t(" geocache "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getLatLng"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"address"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("geocache"),a("span",{class:"token punctuation"},"["),t("address"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n geocache"),a("span",{class:"token punctuation"},"["),t("address"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" geocoder"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getLatLng"),a("span",{class:"token punctuation"},"("),t("address"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n log"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"': '"),t(),a("span",{class:"token operator"},"+"),t(" geocache"),a("span",{class:"token punctuation"},"["),t("address"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" geocache"),a("span",{class:"token punctuation"},"["),t("address"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"getCount"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" count "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" code "),a("span",{class:"token keyword"},"in"),t(" geocache"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n count"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" count"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br")])],-1),Ss=a("h3",{id:"command-pattern"},[a("a",{class:"header-anchor",href:"#command-pattern","aria-hidden":"true"},"#"),t(" Command Pattern")],-1),js=a("p",null,"有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁 (多个对象中的某个随机对象),也不知道被请求的操作是什么. 此时希望用一种松耦合的方式来设计程序,使得请求发送者和请求接收者能够消除彼此的耦合关系",-1),Ms=a("ul",null,[a("li",null,"将方法/动作封装成对象, 使得外部通过唯一方法 execute/run 调用内部方法/动作"),a("li",null,"客户创建命令;调用者执行该命令;接收者在命令执行时执行相应操作"),a("li",null,"客户通常被包装为一个对象,但是这不是必然的"),a("li",null,"调用者接过命令并将其保存下来, 它会在某个时候调用该命令对象的 execute 方法"),a("li",null,[t("调用者进行 "),a("code",null,"commandObject.execute"),t(" 这种调用时, 它所调用的方法将转而以 "),a("code",null,"receiver.action()"),t(" 这种形式调用恰当的方法")])],-1),xs=a("p",null,"client and receiver",-1),Ds=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"SimpleCommand"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"receiving"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("receiving "),a("span",{class:"token operator"},"="),t(" receiving"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"SimpleCommand"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"execute"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("receiving"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),As=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[t("module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" manager "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// command to be encapsulated"),t("\n manager"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"isNull"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"nu"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("nu"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object Null]'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n manager"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"isArray"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"arr"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("arr"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object Array]'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n manager"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"isString"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"str"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("str"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object String]'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// public api"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"execute"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"command"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"("),t("\n manager"),a("span",{class:"token punctuation"},"["),t("command"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"&&"),t("\n manager"),a("span",{class:"token punctuation"},"["),t("command"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("manager"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"command"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"("),t("\n manager"),a("span",{class:"token punctuation"},"["),t("command"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"&&"),t("\n manager"),a("span",{class:"token punctuation"},"["),t("command"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("manager"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n execute"),a("span",{class:"token operator"},":"),t(" execute"),a("span",{class:"token punctuation"},","),t("\n run"),a("span",{class:"token operator"},":"),t(" run"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br")])],-1),Is=a("p",null,"command pattern in UI development, bind command to UI components:",-1),Ns=a("ul",null,[a("li",null,"executor: UI components"),a("li",null,"client and receiver: background tasks or other UI components"),a("li",null,"executor -> client: command.execute() -> receiver: receiver.action()")],-1),Os=a("p",null,[t("e.g click "),a("code",null,"button"),t(" -> refresh "),a("code",null,"menu")],-1),Ls=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// receiver"),t("\n"),a("span",{class:"token keyword"},"const"),t(" MenuBar "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"refresh"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function"},"refresh"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'refresh menu pages'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// client: command object"),t("\n"),a("span",{class:"token comment"},"// command: object with `action` implemented"),t("\n"),a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"Command"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"receiver"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n receiver"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" RefreshMenuBarCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"Command"),a("span",{class:"token punctuation"},"("),t("MenuBar"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// executor"),t("\nbutton"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"setCommand"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"command"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(),a("span",{class:"token punctuation"},"{"),t("\n button"),a("span",{class:"token punctuation"},"."),t("command "),a("span",{class:"token operator"},"="),t(" command"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nbutton"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"setCommand"),a("span",{class:"token punctuation"},"("),t("RefreshMenuBarCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\nbutton"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"addEventLister"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'click'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"event"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(),a("span",{class:"token punctuation"},"{"),t("\n button"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"command"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br")])],-1),$s=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"MenuCommand"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"action"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("action "),a("span",{class:"token operator"},"="),t(" action"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"execute"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"const"),t(" fileActions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"FileActions"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" EditActions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"EditActions"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" InsertActions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"InsertActions"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" HelpActions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"HelpActions"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"const"),t(" appMenuBar "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuBar"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"//-----------"),t("\n"),a("span",{class:"token keyword"},"const"),t(" fileMenu "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Menu"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'File'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" openCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("fileActions"),a("span",{class:"token punctuation"},"."),t("open"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" closeCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("fileActions"),a("span",{class:"token punctuation"},"."),t("close"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" saveCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("fileActions"),a("span",{class:"token punctuation"},"."),t("save"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" saveAsCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("fileActions"),a("span",{class:"token punctuation"},"."),t("saveAs"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\nfileMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'open'"),a("span",{class:"token punctuation"},","),t(" openCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nfileMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Close'"),a("span",{class:"token punctuation"},","),t(" closeCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nfileMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Save'"),a("span",{class:"token punctuation"},","),t(" saveCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nfileMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Close'"),a("span",{class:"token punctuation"},","),t(" saveAsCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("fileMenu"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"//--------------"),t("\n"),a("span",{class:"token keyword"},"const"),t(" editMenu "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Menu"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Edit'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" cutCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("EditActions"),a("span",{class:"token punctuation"},"."),t("cut"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" copyCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("EditActions"),a("span",{class:"token punctuation"},"."),t("copy"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" pasteCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("EditActions"),a("span",{class:"token punctuation"},"."),t("paste"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" deleteCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("EditActions"),a("span",{class:"token punctuation"},"."),t("delete"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\neditMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Cut'"),a("span",{class:"token punctuation"},","),t(" cutCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\neditMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Copy'"),a("span",{class:"token punctuation"},","),t(" copyCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\neditMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Paste'"),a("span",{class:"token punctuation"},","),t(" pasteCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\neditMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Delete'"),a("span",{class:"token punctuation"},","),t(" deleteCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("editMenu"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"//------------"),t("\n"),a("span",{class:"token keyword"},"const"),t(" insertMenu "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Menu"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Insert'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" textBlockCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("InsertActions"),a("span",{class:"token punctuation"},"."),t("textBlock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\ninsertMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Text Block'"),a("span",{class:"token punctuation"},","),t(" textBlockCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("insertMenu"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"//------------"),t("\n"),a("span",{class:"token keyword"},"const"),t(" helpMenu "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Menu"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Help'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" showHelpCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("HelpActions"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"showHelp"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nhelpMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Show Help'"),a("span",{class:"token punctuation"},","),t(" showHelpCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("helpMenu"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\ndocument"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getElementsByTagName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'body'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"appendChild"),a("span",{class:"token punctuation"},"("),t("appMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getElement"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"show"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br")])],-1),_s=a("p",null,"Command sequences to implement Macro/Batch/Undo command:",-1),Es=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"Cursor"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("width"),a("span",{class:"token punctuation"},","),t(" height"),a("span",{class:"token punctuation"},","),t(" parent")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("width "),a("span",{class:"token operator"},"="),t(" width"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("height "),a("span",{class:"token operator"},"="),t(" height"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas "),a("span",{class:"token operator"},"="),t(" document"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"createElement"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'canvas'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas"),a("span",{class:"token punctuation"},"."),t("width "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("width"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas"),a("span",{class:"token punctuation"},"."),t("height "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("height"),a("span",{class:"token punctuation"},";"),t("\n parent"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"appendChild"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getContext"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'2d'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),t("fillStyle "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'#CCC000'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"move"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Cursor"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"move"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("x"),a("span",{class:"token punctuation"},","),t(" y")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" _this "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n _this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"lineTo"),a("span",{class:"token punctuation"},"("),t("x"),a("span",{class:"token punctuation"},","),t(" y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"lineTo"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("x"),a("span",{class:"token punctuation"},","),t(" y")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("x "),a("span",{class:"token operator"},"+="),t(" x"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("y "),a("span",{class:"token operator"},"+="),t(" y"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"lineTo"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("x"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"executeCommands"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t(" x"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("width "),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" y"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("height "),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"2"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"clearRect"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("width"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("height"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"beginPath"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"moveTo"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("x"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"stroke"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"undo"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"pop"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"executeCommands"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br")])],-1),Bs=a("h3",{id:"mediator-pattern"},[a("a",{class:"header-anchor",href:"#mediator-pattern","aria-hidden":"true"},"#"),t(" Mediator Pattern")],-1),Fs=a("p",null,"一个 Mediator 对象封装对象间的协议: 中央集权的控制中心 - 所有观察者共享一个共有的被观察者(所有订阅者订阅同一个节点).",-1),Ts=a("h3",{id:"observer-pattern"},[a("a",{class:"header-anchor",href:"#observer-pattern","aria-hidden":"true"},"#"),t(" Observer Pattern")],-1),Vs=a("ul",null,[a("li",null,"被观察者(Subject)维护一组观察者列表,每当被观察者状态改变时,调用 notify 函数,此函数中调用观察者(Observer)的 update 函数(可自定义)"),a("li",null,[t("decouple subject and observer: each depends on "),a("code",null,"Abstraction"),t(" not "),a("code",null,"Implementation")])],-1),qs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"ObserverList"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Add"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"obj"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),t("obj"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Empty"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Count"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Get"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"index"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),t(),a("span",{class:"token operator"},"&&"),t(" index "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"["),t("index"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Insert"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("obj"),a("span",{class:"token punctuation"},","),t(" index")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" pointer "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"unshift"),a("span",{class:"token punctuation"},"("),t("obj"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n pointer "),a("span",{class:"token operator"},"="),t(" index"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),t("obj"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n pointer "),a("span",{class:"token operator"},"="),t(" index"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" pointer"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"IndexOf"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("obj"),a("span",{class:"token punctuation"},","),t(" startIndex")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(" startIndex"),a("span",{class:"token punctuation"},","),t("\n pointer "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(" obj"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n pointer "),a("span",{class:"token operator"},"="),t(" i"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" pointer"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"RemoveAt"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"index"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"shift"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"-"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"pop"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// 被观察者维护一个观察者列表"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Subject"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token class-name"},"Subject"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"AddObserver"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"observer"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"Add"),a("span",{class:"token punctuation"},"("),t("observer"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Subject"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"RemoveObserver"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"observer"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"RemoveAt"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"IndexOf"),a("span",{class:"token punctuation"},"("),t("observer"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Subject"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Notify"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"context"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" observerCount "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"Count"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" observerCount"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"Get"),a("span",{class:"token punctuation"},"("),t("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"Update"),a("span",{class:"token punctuation"},"("),t("context"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// The Observer"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Observer"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Update"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// ..."),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// Extend an object with an extension"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"extend"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("extension"),a("span",{class:"token punctuation"},","),t(" obj")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" key "),a("span",{class:"token keyword"},"in"),t(" extension"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n obj"),a("span",{class:"token punctuation"},"["),t("key"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" extension"),a("span",{class:"token punctuation"},"["),t("key"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br"),a("span",{class:"line-number"},"83"),a("br"),a("span",{class:"line-number"},"84"),a("br"),a("span",{class:"line-number"},"85"),a("br"),a("span",{class:"line-number"},"86"),a("br"),a("span",{class:"line-number"},"87"),a("br"),a("span",{class:"line-number"},"88"),a("br"),a("span",{class:"line-number"},"89"),a("br"),a("span",{class:"line-number"},"90"),a("br"),a("span",{class:"line-number"},"91"),a("br")])],-1),Rs=a("h3",{id:"pub-sub-pattern"},[a("a",{class:"header-anchor",href:"#pub-sub-pattern","aria-hidden":"true"},"#"),t(" Pub-Sub Pattern")],-1),Us=a("ul",null,[a("li",null,"观察者模式中主体和观察者是互相感知"),a("li",null,"发布-订阅模式是借助第三方来实现调度, 发布者和订阅者之间互不感知")],-1),Hs=a("h4",{id:"implementation"},[a("a",{class:"header-anchor",href:"#implementation","aria-hidden":"true"},"#"),t(" Implementation")],-1),zs=a("ul",null,[a("li",null,"pubSub.js")],-1),Gs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[t("module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("window"),a("span",{class:"token punctuation"},","),t(" doc"),a("span",{class:"token punctuation"},","),t(" undef")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" pubSub "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"var"),t(" topics "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n subUid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n pubSub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"publish"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("topic"),a("span",{class:"token punctuation"},","),t(" args")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// undefined check"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"setTimeout"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" subscribers "),a("span",{class:"token operator"},"="),t(" topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t("\n len "),a("span",{class:"token operator"},"="),t(" subscribers "),a("span",{class:"token operator"},"?"),t(" subscribers"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},":"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("len"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n subscribers"),a("span",{class:"token punctuation"},"["),t("len"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"func"),a("span",{class:"token punctuation"},"("),t("topic"),a("span",{class:"token punctuation"},","),t(" args"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token boolean"},"true"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n pubSub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"subscribe"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("topic"),a("span",{class:"token punctuation"},","),t(" func")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// undefined check"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// add observer to observerList (topics)"),t("\n "),a("span",{class:"token keyword"},"var"),t(" token "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),t("subUid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"{"),t("\n token"),a("span",{class:"token operator"},":"),t(" token"),a("span",{class:"token punctuation"},","),t("\n func"),a("span",{class:"token operator"},":"),t(" func"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" token"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n pubSub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"unsubscribe"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"token"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" m "),a("span",{class:"token keyword"},"in"),t(" topics"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("topics"),a("span",{class:"token punctuation"},"["),t("m"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(" j "),a("span",{class:"token operator"},"="),t(" topics"),a("span",{class:"token punctuation"},"["),t("m"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" j"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("topics"),a("span",{class:"token punctuation"},"["),t("m"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),t("token "),a("span",{class:"token operator"},"==="),t(" token"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n topics"),a("span",{class:"token punctuation"},"["),t("m"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"splice"),a("span",{class:"token punctuation"},"("),t("i"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" token"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" pubSub"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("document"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"undefined"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br")])],-1),Ws=a("ul",null,[a("li",null,"test.js")],-1),Qs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" pubsub "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"require"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'./pubSub.js'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// add observer to observerList"),t("\n"),a("span",{class:"token keyword"},"var"),t(" testFirstSub "),a("span",{class:"token operator"},"="),t(" pubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("topic"),a("span",{class:"token punctuation"},","),t(" data")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("topic "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"': '"),t(),a("span",{class:"token operator"},"+"),t(" data"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// subject broadcast/notify, observer update"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'hello world!'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'test'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'a'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'b'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'c'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"{"),t(" color"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'blue'"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"{"),t(" text"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'hello'"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token function"},"setTimeout"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n pubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"unsubscribe"),a("span",{class:"token punctuation"},"("),t("testFirstSub"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// permanent subscribe"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'sum'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("topic"),a("span",{class:"token punctuation"},","),t(" data")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("data"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"!=="),t(),a("span",{class:"token string"},"'[object Array]'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Please input array: * '"),t(),a("span",{class:"token operator"},"+"),t(" data "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' * is not array!'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" tmp "),a("span",{class:"token operator"},"="),t(" data"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"item"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("item"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object Number]'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("tmp"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" sum "),a("span",{class:"token operator"},"="),t(" tmp"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"reduce"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("previous"),a("span",{class:"token punctuation"},","),t(" current")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" previous "),a("span",{class:"token operator"},"+"),t(" current"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Sum of '"),t(),a("span",{class:"token operator"},"+"),t(" data "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' : '"),t(),a("span",{class:"token operator"},"+"),t(" sum"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token string"},"'Please input number array: * '"),t(),a("span",{class:"token operator"},"+"),t(" data "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' * is not number array!'"),t("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'hello again!'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'sum'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'hello again!'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'sum'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'sum'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'a'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'b'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'c'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'d'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'e'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br")])],-1),Xs=a("ul",null,[a("li",null,"in jQuery")],-1),Js=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// Equivalent to subscribe(topicName, callback)"),t("\n"),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t("document"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"on"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'topicName'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"//..perform some behavior"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// Equivalent to publish(topicName)"),t("\n"),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t("document"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"trigger"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'topicName'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// Equivalent to unsubscribe(topicName)"),t("\n"),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t("document"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"off"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'topicName'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),Ks=a("ul",null,[a("li",null,"MicroEvent.js")],-1),Ys=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * MicroEvent - to make any js object an event emitter (server or browser)\n *\n * - pure javascript - server compatible, browser compatible\n * - don't rely on the browser doms\n * - super simple - you get it immediately, no mystery, no magic involved\n *\n * - create a MicroEventDebug with goodies to debug\n * - make it safer to use\n */"),t("\n\n"),a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"MicroEvent"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token class-name"},"MicroEvent"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"bind"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("event"),a("span",{class:"token punctuation"},","),t(" fct")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),t("fct"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"unbind"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("event"),a("span",{class:"token punctuation"},","),t(" fct")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("event "),a("span",{class:"token keyword"},"in"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"splice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"indexOf"),a("span",{class:"token punctuation"},"("),t("fct"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"trigger"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),t("event "),a("span",{class:"token comment"},"/* , args... */"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("event "),a("span",{class:"token keyword"},"in"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token class-name"},"Array"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"/**\n * mixin will delegate all MicroEvent.js function in the destination object\n *\n * - require('MicroEvent').mixin(Foobar) will make Foobar able to use MicroEvent\n *\n * @param {Object} the object which will support MicroEvent\n */"),t("\nMicroEvent"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"mixin"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"destObject"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" props "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'bind'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'unbind'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'trigger'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" props"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" destObject "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'function'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n destObject"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("props"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token class-name"},"MicroEvent"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("props"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n destObject"),a("span",{class:"token punctuation"},"["),t("props"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token class-name"},"MicroEvent"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("props"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" destObject"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// export in common js"),t("\n"),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" module "),a("span",{class:"token operator"},"!=="),t(),a("span",{class:"token string"},"'undefined'"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token string"},"'exports'"),t(),a("span",{class:"token keyword"},"in"),t(" module"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(" MicroEvent"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br")])],-1),Zs=a("h4",{id:"sample"},[a("a",{class:"header-anchor",href:"#sample","aria-hidden":"true"},"#"),t(" Sample")],-1),na=a("h5",{id:"ajax-callback"},[a("a",{class:"header-anchor",href:"#ajax-callback","aria-hidden":"true"},"#"),t(" Ajax Callback")],-1),sa=a("ul",null,[a("li",null,"当请求返回,并且实际的数据可用的时候,会生成一个通知"),a("li",null,"如何使用这些事件(或者返回的数据),都是由订阅者自己决定的"),a("li",null,"可以有多个不同的订阅者,以不同的方式使用返回的数据"),a("li",null,"Ajax 层: 唯一的责任 - 请求和返回数据,接着将数据发送给所有想要使用数据的地方")],-1),aa=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"$"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token comment"},'// Pre-compile template and "cache" it using closure'),t("\n "),a("span",{class:"token keyword"},"var"),t(" resultTemplate "),a("span",{class:"token operator"},"="),t(" _"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"template"),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#resultTemplate"'),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"html"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// Subscribe to the new search tags topic"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"/search/tags"'),t(),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"tags"),t(),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#searchResults"'),t(),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"html"),a("span",{class:"token punctuation"},"("),t('"\n\n\n Searched '),a("span",{class:"token keyword"},"for"),a("span",{class:"token operator"},":"),a("span",{class:"token string"},'" + tags + "'),t('\n\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// Subscribe to the new results topic"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"/search/resultSet"'),t(),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"results"),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#searchResults"'),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"append"),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"resultTemplate"),a("span",{class:"token punctuation"},"("),t(" results "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// Submit a search query and publish tags on the /search/tags topic"),t("\n "),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#flickrSearch"'),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"submit"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"e"),t(),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n\n e"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"preventDefault"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"var"),t(" tags "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"find"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#query"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"val"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token operator"},"!"),t("tags "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"/search/tags"'),t(),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),t(" $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"trim"),a("span",{class:"token punctuation"},"("),t("tags"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n\n "),a("span",{class:"token comment"},"// Subscribe to new tags being published and perform"),t("\n "),a("span",{class:"token comment"},"// a search query using them. Once data has returned"),t("\n "),a("span",{class:"token comment"},"// publish this data for the rest of the application"),t("\n "),a("span",{class:"token comment"},"// to consume"),t("\n\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"/search/tags"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"tags"),t(),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// Ajax Request"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getJSON"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"http://api.flickr.com/services/feeds/"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"{"),t("\n tags"),a("span",{class:"token operator"},":"),t(" tags"),a("span",{class:"token punctuation"},","),t("\n tagMode"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},'"any"'),a("span",{class:"token punctuation"},","),t("\n format"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},'"json"'),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"data"),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token operator"},"!"),t("data"),a("span",{class:"token punctuation"},"."),t("items"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"/search/resultSet"'),t(),a("span",{class:"token punctuation"},","),t(" data"),a("span",{class:"token punctuation"},"."),t("items "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br")])],-1),ta=a("h2",{id:"mvc-pattern"},[a("a",{class:"header-anchor",href:"#mvc-pattern","aria-hidden":"true"},"#"),t(" MVC Pattern")],-1),ea=a("p",null,"在 MVC 中,视图位于我们架构的顶部,其背后是控制器. 模型在控制器后面,而因此我们的视图了解得到我们的控制器,而控制器了解得到模型. 这里,我们的视图有对模型的直接访问. 然而将整个模型完全暴露给视图可能会有安全和性能损失, 这取决于我们应用程序的复杂性. MVVM 则尝试去避免这些问题.",-1),oa=a("p",null,"在 MVP 中,控制器的角色被代理器所取代,代理器和视图处于同样的地位, 视图和模型的事件都被它侦听着并且接受它的调解. 不同于 MVVM,没有一个将视图绑定到视图模型的机制,因此我们转而依赖于每一个视图都实现一个允许代理器同视图去交互的接口.",-1),ca=a("p",null,"MVVM 进一步允许我们创建一个模型的特定视图子集,包含了状态和逻辑信息, 避免了将模型完全暴露给视图的必要。 不同于 MVP 的代理器,视图模型并不需要去引用一个视图。 视图可以绑定到视图模型的属性上面,视图模型则去将包含在模型中的数据暴露给视图。 像我们所提到过的,对视图的抽象意味着其背后的代码需要较少的逻辑。",-1),pa=a("h2",{id:"jquery-pattern"},[a("a",{class:"header-anchor",href:"#jquery-pattern","aria-hidden":"true"},"#"),t(" jQuery Pattern")],-1),la=a("h3",{id:"plugin-pattern"},[a("a",{class:"header-anchor",href:"#plugin-pattern","aria-hidden":"true"},"#"),t(" Plugin Pattern")],-1),ua=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"$"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"extend"),a("span",{class:"token punctuation"},"("),t("$"),a("span",{class:"token punctuation"},"."),t("fn"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"myPlugin"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// your plugin logic"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),t("jQuery"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),ia=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// the semi-colon before the function invocation is a safety"),t("\n"),a("span",{class:"token comment"},"// net against concatenated scripts and/or other plugins"),t("\n"),a("span",{class:"token comment"},"// that are not closed properly."),t("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("$"),a("span",{class:"token punctuation"},","),t(" window"),a("span",{class:"token punctuation"},","),t(" document"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"undefined")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// undefined is used here as the undefined global"),t("\n "),a("span",{class:"token comment"},"// variable in ECMAScript 3 and is mutable (i.e. it can"),t("\n "),a("span",{class:"token comment"},"// be changed by someone else). undefined isn't really"),t("\n "),a("span",{class:"token comment"},"// being passed in so we can ensure that its value is"),t("\n "),a("span",{class:"token comment"},"// truly undefined. In ES5, undefined can no longer be"),t("\n "),a("span",{class:"token comment"},"// modified."),t("\n\n "),a("span",{class:"token comment"},"// window and document are passed through as local"),t("\n "),a("span",{class:"token comment"},"// variables rather than as globals, because this (slightly)"),t("\n "),a("span",{class:"token comment"},"// quickens the resolution process and can be more"),t("\n "),a("span",{class:"token comment"},"// efficiently minified (especially when both are"),t("\n "),a("span",{class:"token comment"},"// regularly referenced in our plugin)."),t("\n\n "),a("span",{class:"token comment"},"// Create the defaults once"),t("\n "),a("span",{class:"token keyword"},"var"),t(" pluginName "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'defaultPluginName'"),a("span",{class:"token punctuation"},","),t("\n defaults "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n propertyName"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'value'"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// The actual plugin constructor"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Plugin"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("element"),a("span",{class:"token punctuation"},","),t(" options")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("element "),a("span",{class:"token operator"},"="),t(" element"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// jQuery has an extend method that merges the"),t("\n "),a("span",{class:"token comment"},"// contents of two or more objects, storing the"),t("\n "),a("span",{class:"token comment"},"// result in the first object. The first object"),t("\n "),a("span",{class:"token comment"},"// is generally empty because we don't want to alter"),t("\n "),a("span",{class:"token comment"},"// the default options for future instances of the plugin"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("options "),a("span",{class:"token operator"},"="),t(" $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"extend"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(" defaults"),a("span",{class:"token punctuation"},","),t(" options"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_defaults "),a("span",{class:"token operator"},"="),t(" defaults"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_name "),a("span",{class:"token operator"},"="),t(" pluginName"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"init"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token class-name"},"Plugin"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"init"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// Place initialization logic here"),t("\n "),a("span",{class:"token comment"},"// We already have access to the DOM element and"),t("\n "),a("span",{class:"token comment"},"// the options via the instance, e.g. this.element"),t("\n "),a("span",{class:"token comment"},"// and this.options"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// A really lightweight plugin wrapper around the constructor,"),t("\n "),a("span",{class:"token comment"},"// preventing against multiple instantiations"),t("\n $"),a("span",{class:"token punctuation"},"."),t("fn"),a("span",{class:"token punctuation"},"["),t("pluginName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"options"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"each"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("$"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'plugin_'"),t(),a("span",{class:"token operator"},"+"),t(" pluginName"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'plugin_'"),t(),a("span",{class:"token operator"},"+"),t(" pluginName"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Plugin"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(" options"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),t("jQuery"),a("span",{class:"token punctuation"},","),t(" window"),a("span",{class:"token punctuation"},","),t(" document"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br")])],-1),ra=a("h2",{id:"scalability-design"},[a("a",{class:"header-anchor",href:"#scalability-design","aria-hidden":"true"},"#"),t(" Scalability Design")],-1),ka=a("ul",null,[a("li",null,"Prefer composites over mixins."),a("li",null,"Always clone objects between components."),a("li",null,"Use namespaced state store modules."),a("li",null,"Write robust tests."),a("li",null,"Interact with REST API via services/SDK."),a("li",null,[t("Wrap third-party libraries other using them directly: "),a("ul",null,[a("li",null,"Abstract: changing dependencies without changing interface."),a("li",null,"Extendability: More obvious route to extending functionality.")])])],-1),ba=a("h2",{id:"domain-driven-design"},[a("a",{class:"header-anchor",href:"#domain-driven-design","aria-hidden":"true"},"#"),t(" Domain Driven Design")],-1),ma=a("h3",{id:"interface-layer"},[a("a",{class:"header-anchor",href:"#interface-layer","aria-hidden":"true"},"#"),t(" Interface Layer")],-1),da=a("p",null,"主要负责与外部系统进行交互与通信:",-1),ya=a("ul",null,[a("li",null,"做参数的基本处理, 比如入参校验, 回参 DTO 转换 (拆包, 组包)"),a("li",null,"Dubbo Services"),a("li",null,"RESTful API")],-1),fa=a("h3",{id:"application-layer"},[a("a",{class:"header-anchor",href:"#application-layer","aria-hidden":"true"},"#"),t(" Application Layer")],-1),ha=a("p",null,"Application Service 层只是很薄的一层, 它内部并不实现任何逻辑, 只是负责协调和转发 (流程编排), 委派业务动作给更下层的领域层.",-1),wa=a("h3",{id:"domain-layer"},[a("a",{class:"header-anchor",href:"#domain-layer","aria-hidden":"true"},"#"),t(" Domain Layer")],-1),ga=a("p",null,"Domain 层是领域模型系统的核心, 负责维护面向对象的领域模型, 几乎全部的业务逻辑都会在这一层实现. 内部主要包含 Entity, ValueObject, Domain Event, Repository.",-1),va=a("h3",{id:"infrastructure-layer"},[a("a",{class:"header-anchor",href:"#infrastructure-layer","aria-hidden":"true"},"#"),t(" Infrastructure Layer")],-1),Pa=a("p",null,"主要为 Interface, Application 和 Domain 三层提供支撑:",-1),Ca=a("ul",null,[a("li",null,"封装基础资源服务, 通过依赖注入方式解耦."),a("li",null,"Third-party tools, Message Queue, File, Cache, Database, Search etc."),a("li",null,"实现仓储接口 DB, 通常真正读写 DB.")],-1),Sa=a("h3",{id:"ddd-layout"},[a("a",{class:"header-anchor",href:"#ddd-layout","aria-hidden":"true"},"#"),t(" DDD Layout")],-1),ja=a("ul",null,[a("li",null,[a("a",{href:"https://github.com/lupguo/ddd-layout",target:"_blank",rel:"noopener noreferrer"},"DDD Layout in Golang")])],-1),Ma=a("h2",{id:"高并发系统设计"},[a("a",{class:"header-anchor",href:"#高并发系统设计","aria-hidden":"true"},"#"),t(" 高并发系统设计")],-1),xa=a("h3",{id:"concurrent-code-layer"},[a("a",{class:"header-anchor",href:"#concurrent-code-layer","aria-hidden":"true"},"#"),t(" Concurrent Code Layer")],-1),Da=a("ul",null,[a("li",null,"Mutex Performance"),a("li",null,"Database Caches"),a("li",null,"Update Merge"),a("li",null,"BloomFilter"),a("li",null,"Asynchronous"),a("li",null,"Multi-Thread")],-1),Aa=a("h3",{id:"concurrent-database-layer"},[a("a",{class:"header-anchor",href:"#concurrent-database-layer","aria-hidden":"true"},"#"),t(" Concurrent DataBase Layer")],-1),Ia=a("ul",null,[a("li",null,"DataBase Type: RDBMS -> NoSQL -> NewSQL"),a("li",null,"Table Structure Design"),a("li",null,"Index Design"),a("li",null,"Split Table"),a("li",null,"Read and Write Separation"),a("li",null,"Data Slice and Data Partition"),a("li",null,"Hot Data Cache")],-1),Na=a("h3",{id:"concurrent-architecture-layer"},[a("a",{class:"header-anchor",href:"#concurrent-architecture-layer","aria-hidden":"true"},"#"),t(" Concurrent Architecture Layer")],-1),Oa=a("ul",null,[a("li",null,"Microservices"),a("li",null,"Scale Friendly"),a("li",null,"FailFast"),a("li",null,"Data PreFetch"),a("li",null,"Multi-Level Caches")],-1),La=a("h2",{id:"高可用系统设计"},[a("a",{class:"header-anchor",href:"#高可用系统设计","aria-hidden":"true"},"#"),t(" 高可用系统设计")],-1),$a=a("h3",{id:"resource-isolation"},[a("a",{class:"header-anchor",href:"#resource-isolation","aria-hidden":"true"},"#"),t(" Resource Isolation")],-1),_a=a("h3",{id:"load-balance-design"},[a("a",{class:"header-anchor",href:"#load-balance-design","aria-hidden":"true"},"#"),t(" Load Balance Design")],-1),Ea=a("ul",null,[a("li",null,"Hardware Load Balance"),a("li",null,"Software Load Balance"),a("li",null,"Load Balance Algorithms: Random, RoundRobin, WeightRoundRobin, ConsistentHash"),a("li",null,"Error Machines Auto Detection"),a("li",null,"Error Services Auto Retirement"),a("li",null,"Services Retry Automation"),a("li",null,"Recovery Services Auto Detection")],-1),Ba=a("h3",{id:"idempotence-design"},[a("a",{class:"header-anchor",href:"#idempotence-design","aria-hidden":"true"},"#"),t(" Idempotence Design")],-1),Fa=a("p",null,"在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同.",-1),Ta=a("h4",{id:"write-idempotence-design"},[a("a",{class:"header-anchor",href:"#write-idempotence-design","aria-hidden":"true"},"#"),t(" Write Idempotence Design")],-1),Va=a("ul",null,[a("li",null,"Mutex"),a("li",null,"Key Index"),a("li",null,"Token"),a("li",null,"Data Version"),a("li",null,"State Machine")],-1),qa=a("h3",{id:"cap-theory"},[a("a",{class:"header-anchor",href:"#cap-theory","aria-hidden":"true"},"#"),t(" CAP Theory")],-1),Ra=a("p",null,"A distributed system to simultaneously provide more than two out of the following three guarantees:",-1),Ua=a("ul",null,[a("li",null,"Consistency: Every read receives the most recent write or an error."),a("li",null,"Availability: Every request receives a (non-error) response, without the guarantee that it contains the most recent write."),a("li",null,"Partition tolerance: The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.")],-1),Ha=a("h3",{id:"服务熔断"},[a("a",{class:"header-anchor",href:"#服务熔断","aria-hidden":"true"},"#"),t(" 服务熔断")],-1),za=a("h3",{id:"服务限流"},[a("a",{class:"header-anchor",href:"#服务限流","aria-hidden":"true"},"#"),t(" 服务限流")],-1),Ga=a("h4",{id:"服务限流算法"},[a("a",{class:"header-anchor",href:"#服务限流算法","aria-hidden":"true"},"#"),t(" 服务限流算法")],-1),Wa=a("p",null,"一定程度上可以参考计算机网络拥塞控制算法:",-1),Qa=a("ul",null,[a("li",null,"计数器固定窗口限流: 单位时间内达到阈值后开始限流, 单位时间后重新计数. 窗口临界处流量过大, 导致服务不可用."),a("li",null,"滑动窗口限流: 在固定窗口限流基础上, 将窗口向右滑动."),a("li",null,"漏斗限流: 未满前可进入, 满则拒绝. 可以平滑流量, 无法解决突发流量."),a("li",null,"令牌桶限流: 在漏斗限流基础上, 以恒定速率产生令牌. 拥有令牌可进入, 无则拒绝. 可以平滑流量, 可以容忍突发流量.")],-1),Xa=a("h4",{id:"服务限流策略"},[a("a",{class:"header-anchor",href:"#服务限流策略","aria-hidden":"true"},"#"),t(" 服务限流策略")],-1),Ja=a("ul",null,[a("li",null,"服务拒绝"),a("li",null,"延时处理"),a("li",null,"请求分级"),a("li",null,"监控预警"),a("li",null,"动态限流"),a("li",null,"动态扩容")],-1),Ka=a("h4",{id:"服务限流位置"},[a("a",{class:"header-anchor",href:"#服务限流位置","aria-hidden":"true"},"#"),t(" 服务限流位置")],-1),Ya=a("ul",null,[a("li",null,"接入层限流: 通过 Nginx/API Router 对 DNS/IP 限流."),a("li",null,"应用限流: 每个服务拥有自己的集群限流服务."),a("li",null,"基础服务限流: 对消息队列/数据库限流.")],-1),Za=a("h3",{id:"服务降级"},[a("a",{class:"header-anchor",href:"#服务降级","aria-hidden":"true"},"#"),t(" 服务降级")],-1),nt=a("h2",{id:"reference"},[a("a",{class:"header-anchor",href:"#reference","aria-hidden":"true"},"#"),t(" Reference")],-1),st=a("ul",null,[a("li",null,[a("a",{href:"http://www.dofactory.com/javascript/design-patterns",target:"_blank",rel:"noopener noreferrer"},"JavaScript Design Patterns")])],-1);o.render=function(a,t,e,o,at,tt){return n(),s("div",null,[c,p,l,u,i,r,k,b,m,d,y,f,h,w,g,v,P,C,S,j,M,x,D,A,I,N,O,L,$,_,E,B,F,T,V,q,R,U,H,z,G,W,Q,X,J,K,Y,Z,nn,sn,an,tn,en,on,cn,pn,ln,un,rn,kn,bn,mn,dn,yn,fn,hn,wn,gn,vn,Pn,Cn,Sn,jn,Mn,xn,Dn,An,In,Nn,On,Ln,$n,_n,En,Bn,Fn,Tn,Vn,qn,Rn,Un,Hn,zn,Gn,Wn,Qn,Xn,Jn,Kn,Yn,Zn,ns,ss,as,ts,es,os,cs,ps,ls,us,is,rs,ks,bs,ms,ds,ys,fs,hs,ws,gs,vs,Ps,Cs,Ss,js,Ms,xs,Ds,As,Is,Ns,Os,Ls,$s,_s,Es,Bs,Fs,Ts,Vs,qs,Rs,Us,Hs,zs,Gs,Ws,Qs,Xs,Js,Ks,Ys,Zs,na,sa,aa,ta,ea,oa,ca,pa,la,ua,ia,ra,ka,ba,ma,da,ya,fa,ha,wa,ga,va,Pa,Ca,Sa,ja,Ma,xa,Da,Aa,Ia,Na,Oa,La,$a,_a,Ea,Ba,Fa,Ta,Va,qa,Ra,Ua,Ha,za,Ga,Wa,Qa,Xa,Ja,Ka,Ya,Za,nt,st])};export default o;export{e as __pageData}; diff --git a/assets/programming_devops_designPatternsBasicNotes.md.0d5c71a8.lean.js b/assets/programming_devops_designPatternsBasicNotes.md.0d5c71a8.lean.js new file mode 100644 index 00000000000..32c8b5911ba --- /dev/null +++ b/assets/programming_devops_designPatternsBasicNotes.md.0d5c71a8.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,b as a,d as t}from"./app.6dd2a1b7.js";const e='{"title":"Design Patterns Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Patterns","slug":"basic-patterns"},{"level":3,"title":"SOLID Principles","slug":"solid-principles"},{"level":3,"title":"Literal Pattern","slug":"literal-pattern"},{"level":3,"title":"Closure and IIFE","slug":"closure-and-iife"},{"level":3,"title":"Check Pattern","slug":"check-pattern"},{"level":3,"title":"函数(function)","slug":"函数-function"},{"level":3,"title":"解耦","slug":"解耦"},{"level":3,"title":"Env and Config","slug":"env-and-config"},{"level":3,"title":"Stand Library Idioms","slug":"stand-library-idioms"},{"level":3,"title":"Other","slug":"other"},{"level":2,"title":"Modular Patterns","slug":"modular-patterns"},{"level":3,"title":"Object Literal","slug":"object-literal"},{"level":3,"title":"IIFE Pattern","slug":"iife-pattern"},{"level":3,"title":"UMD Pattern","slug":"umd-pattern"},{"level":2,"title":"Common Design Patterns","slug":"common-design-patterns"},{"level":3,"title":"Classification","slug":"classification"},{"level":3,"title":"Prototype","slug":"prototype"},{"level":3,"title":"Bridge","slug":"bridge"},{"level":3,"title":"Composite","slug":"composite"},{"level":3,"title":"Strategy","slug":"strategy"},{"level":3,"title":"State","slug":"state"},{"level":3,"title":"Iterator","slug":"iterator"},{"level":3,"title":"IOC and DI","slug":"ioc-and-di"},{"level":3,"title":"Class Pattern","slug":"class-pattern"},{"level":3,"title":"Mix-In Pattern","slug":"mix-in-pattern"},{"level":3,"title":"Singleton Pattern","slug":"singleton-pattern"},{"level":3,"title":"Abstract Factory","slug":"abstract-factory"},{"level":3,"title":"Factory Method","slug":"factory-method"},{"level":3,"title":"Adapter Pattern","slug":"adapter-pattern"},{"level":3,"title":"Decorator Pattern","slug":"decorator-pattern"},{"level":3,"title":"Facade Pattern","slug":"facade-pattern"},{"level":3,"title":"Flyweight Pattern","slug":"flyweight-pattern"},{"level":3,"title":"Proxy Pattern","slug":"proxy-pattern"},{"level":3,"title":"Command Pattern","slug":"command-pattern"},{"level":3,"title":"Mediator Pattern","slug":"mediator-pattern"},{"level":3,"title":"Observer Pattern","slug":"observer-pattern"},{"level":3,"title":"Pub-Sub Pattern","slug":"pub-sub-pattern"},{"level":2,"title":"MVC Pattern","slug":"mvc-pattern"},{"level":2,"title":"jQuery Pattern","slug":"jquery-pattern"},{"level":3,"title":"Plugin Pattern","slug":"plugin-pattern"},{"level":2,"title":"Scalability Design","slug":"scalability-design"},{"level":2,"title":"Domain Driven Design","slug":"domain-driven-design"},{"level":3,"title":"Interface Layer","slug":"interface-layer"},{"level":3,"title":"Application Layer","slug":"application-layer"},{"level":3,"title":"Domain Layer","slug":"domain-layer"},{"level":3,"title":"Infrastructure Layer","slug":"infrastructure-layer"},{"level":3,"title":"DDD Layout","slug":"ddd-layout"},{"level":2,"title":"高并发系统设计","slug":"高并发系统设计"},{"level":3,"title":"Concurrent Code Layer","slug":"concurrent-code-layer"},{"level":3,"title":"Concurrent DataBase Layer","slug":"concurrent-database-layer"},{"level":3,"title":"Concurrent Architecture Layer","slug":"concurrent-architecture-layer"},{"level":2,"title":"高可用系统设计","slug":"高可用系统设计"},{"level":3,"title":"Resource Isolation","slug":"resource-isolation"},{"level":3,"title":"Load Balance Design","slug":"load-balance-design"},{"level":3,"title":"Idempotence Design","slug":"idempotence-design"},{"level":3,"title":"CAP Theory","slug":"cap-theory"},{"level":3,"title":"服务熔断","slug":"服务熔断"},{"level":3,"title":"服务限流","slug":"服务限流"},{"level":3,"title":"服务降级","slug":"服务降级"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"programming/devops/designPatternsBasicNotes.md","lastUpdated":1627735972000}',o={},c=a("h1",{id:"design-patterns-basic-notes"},[a("a",{class:"header-anchor",href:"#design-patterns-basic-notes","aria-hidden":"true"},"#"),t(" Design Patterns Basic Notes")],-1),p=a("p",null,[a("div",{class:"table-of-contents"},[a("ul",null,[a("li",null,[a("a",{href:"#design-patterns-basic-notes"},"Design Patterns Basic Notes"),a("ul",null,[a("li",null,[a("a",{href:"#basic-patterns"},"Basic Patterns")]),a("li",null,[a("a",{href:"#modular-patterns"},"Modular Patterns")]),a("li",null,[a("a",{href:"#common-design-patterns"},"Common Design Patterns")]),a("li",null,[a("a",{href:"#mvc-pattern"},"MVC Pattern")]),a("li",null,[a("a",{href:"#jquery-pattern"},"jQuery Pattern")]),a("li",null,[a("a",{href:"#scalability-design"},"Scalability Design")]),a("li",null,[a("a",{href:"#domain-driven-design"},"Domain Driven Design")]),a("li",null,[a("a",{href:"#高并发系统设计"},"高并发系统设计")]),a("li",null,[a("a",{href:"#高可用系统设计"},"高可用系统设计")]),a("li",null,[a("a",{href:"#reference"},"Reference")])])])])])],-1),l=a("h2",{id:"basic-patterns"},[a("a",{class:"header-anchor",href:"#basic-patterns","aria-hidden":"true"},"#"),t(" Basic Patterns")],-1),u=a("h3",{id:"solid-principles"},[a("a",{class:"header-anchor",href:"#solid-principles","aria-hidden":"true"},"#"),t(" SOLID Principles")],-1),i=a("ul",null,[a("li",null,"Single Responsibility Principle 单一功能原则"),a("li",null,"Open-closed Principle 开闭原则"),a("li",null,"Liskov Substitution Principle 里氏替换原则"),a("li",null,"Interface Segregation Principle 接口隔离原则"),a("li",null,"Dependency Inversion Principle 依赖反转原则")],-1),r=a("blockquote",null,[a("p",null,"SOLID Principles")],-1),k=a("ul",null,[a("li",null,"单一职责是所有设计原则的基础"),a("li",null,"开闭原则是设计的终极目标"),a("li",null,"里氏替换原则强调的是子类替换父类后程序运行时的正确性, 它用来帮助实现开闭原则"),a("li",null,"接口隔离原则用来帮助实现里氏替换原则, 同时它也体现了单一职责"),a("li",null,"依赖倒置原则是过程式设计与面向对象设计的分水岭, 同时它也被用来指导接口隔离原则")],-1),b=a("h4",{id:"single-responsibility-principle"},[a("a",{class:"header-anchor",href:"#single-responsibility-principle","aria-hidden":"true"},"#"),t(" Single Responsibility Principle")],-1),m=a("p",null,"Too much functionality is in one class and you modify a piece of it, it can be difficult to understand how that will affect other dependent modules.",-1),d=a("h4",{id:"open-closed-principle"},[a("a",{class:"header-anchor",href:"#open-closed-principle","aria-hidden":"true"},"#"),t(" Open-Closed Principle")],-1),y=a("p",null,"Allow users to add new functionalities without changing existing code, open for extension, close for modification.",-1),f=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"Coder"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"constructor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("fullName"),a("span",{class:"token punctuation"},","),t(" language"),a("span",{class:"token punctuation"},","),t(" hobby"),a("span",{class:"token punctuation"},","),t(" education"),a("span",{class:"token punctuation"},","),t(" workplace"),a("span",{class:"token punctuation"},","),t(" position")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("fullName "),a("span",{class:"token operator"},"="),t(" fullName"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("language "),a("span",{class:"token operator"},"="),t(" language"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("hobby "),a("span",{class:"token operator"},"="),t(" hobby"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("education "),a("span",{class:"token operator"},"="),t(" education"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("workplace "),a("span",{class:"token operator"},"="),t(" workplace"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position "),a("span",{class:"token operator"},"="),t(" position"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// BAD: filter by any other new property have to change CodeFilter's code."),t("\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"CoderFilter"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"filterByName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("coders"),a("span",{class:"token punctuation"},","),t(" fullName")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" coders"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"coder"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(" coder"),a("span",{class:"token punctuation"},"."),t("fullName "),a("span",{class:"token operator"},"==="),t(" fullName"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"filterByLang"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("coders"),a("span",{class:"token punctuation"},","),t(" language")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" coders"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"coder"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(" coder"),a("span",{class:"token punctuation"},"."),t("language "),a("span",{class:"token operator"},"==="),t(" language"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"filterByHobby"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("coders"),a("span",{class:"token punctuation"},","),t(" hobby")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" coders"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"coder"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(" coder"),a("span",{class:"token punctuation"},"."),t("hobby "),a("span",{class:"token operator"},"==="),t(" hobby"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// GOOD"),t("\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"CoderFilter"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"filterByProp"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("array"),a("span",{class:"token punctuation"},","),t(" propName"),a("span",{class:"token punctuation"},","),t(" value")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t("\n array"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"element"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(" element"),a("span",{class:"token punctuation"},"["),t("propName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(" value"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br")])],-1),h=a("h4",{id:"liskov-substitution-principle"},[a("a",{class:"header-anchor",href:"#liskov-substitution-principle","aria-hidden":"true"},"#"),t(" Liskov Substitution Principle")],-1),w=a("p",null,"Objects of ParentType can be replaced with objects of SubType without altering. Altering shows that SubType should not be subtype of ParentType (break Open Closed Principle), you should re-design ParentType and SubType.",-1),g=a("h4",{id:"interface-segregation-principle"},[a("a",{class:"header-anchor",href:"#interface-segregation-principle","aria-hidden":"true"},"#"),t(" Interface Segregation Principle")],-1),v=a("p",null,"Clients should not be forced to depend upon interfaces that they do not use.",-1),P=a("h4",{id:"dependency-inversion-principle"},[a("a",{class:"header-anchor",href:"#dependency-inversion-principle","aria-hidden":"true"},"#"),t(" Dependency Inversion Principle")],-1),C=a("ul",null,[a("li",null,"High-level modules should not depend on low-level modules. Both should depend on abstractions."),a("li",null,"Abstractions should not depend upon details. Details should depend on abstractions."),a("li",null,[t("Pros: "),a("ul",null,[a("li",null,"Loosely coupled modules."),a("li",null,"Better reusability."),a("li",null,"Better testability.")])])],-1),S=a("h3",{id:"literal-pattern"},[a("a",{class:"header-anchor",href:"#literal-pattern","aria-hidden":"true"},"#"),t(" Literal Pattern")],-1),j=a("ul",null,[a("li",null,"不要使用 new Boolean()/new Number()/new String()"),a("li",null,"避免使用 new Object()/new Array()")],-1),M=a("h3",{id:"closure-and-iife"},[a("a",{class:"header-anchor",href:"#closure-and-iife","aria-hidden":"true"},"#"),t(" Closure and IIFE")],-1),x=a("h3",{id:"check-pattern"},[a("a",{class:"header-anchor",href:"#check-pattern","aria-hidden":"true"},"#"),t(" Check Pattern")],-1),D=a("ul",null,[a("li",null,[a("code",null,"O || {}"),t(),a("code",null,"O || (O = {})")]),a("li",null,[a("code",null,"if (O && O.property)")]),a("li",null,[a("code",null,'if (typeof v === " ")')]),a("li",null,[a("code",null,"toString. apply(var)")])],-1),A=a("h3",{id:"函数-function"},[a("a",{class:"header-anchor",href:"#函数-function","aria-hidden":"true"},"#"),t(" 函数(function)")],-1),I=a("h4",{id:"参数"},[a("a",{class:"header-anchor",href:"#参数","aria-hidden":"true"},"#"),t(" 参数")],-1),N=a("ul",null,[a("li",null,"函数不应依赖于全局变量,实现与执行全局环境的的解耦"),a("li",null,"全局变量应以函数参数/依赖的形式,注入函数内部")],-1),O=a("h3",{id:"解耦"},[a("a",{class:"header-anchor",href:"#解耦","aria-hidden":"true"},"#"),t(" 解耦")],-1),L=a("h4",{id:"事件处理与-ui-逻辑"},[a("a",{class:"header-anchor",href:"#事件处理与-ui-逻辑","aria-hidden":"true"},"#"),t(" 事件处理与 UI 逻辑")],-1),$=a("ul",null,[a("li",null,"事件处理函数与应用逻辑函数分开成单独函数,提高代码重用率"),a("li",null,"应用逻辑函数不依赖于 event 对象,其属性值作为参数传入,易于解耦与测试")],-1),_=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(" MyApp "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token comment"},"// 事件处理函数"),t("\n "),a("span",{class:"token function-variable function"},"handleClick"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"event"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token comment"},"/* 将事件的属性作为参数,传递给应用逻辑函数\n * 使得应用逻辑函数不依赖于 event 对象,易于解耦与测试\n */"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"showPopup"),a("span",{class:"token punctuation"},"("),t("event"),a("span",{class:"token punctuation"},"."),t("clientX"),a("span",{class:"token punctuation"},","),t(" event"),a("span",{class:"token punctuation"},"."),t("clientY"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// 应用逻辑函数"),t("\n "),a("span",{class:"token function-variable function"},"showPopup"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("x"),a("span",{class:"token punctuation"},","),t(" y")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"const"),t(" popup "),a("span",{class:"token operator"},"="),t(" document"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getElementById"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'popup'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n popup"),a("span",{class:"token punctuation"},"."),t("style"),a("span",{class:"token punctuation"},"."),t("left "),a("span",{class:"token operator"},"="),t(" x "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"'px'"),a("span",{class:"token punctuation"},";"),t("\n popup"),a("span",{class:"token punctuation"},"."),t("style"),a("span",{class:"token punctuation"},"."),t("top "),a("span",{class:"token operator"},"="),t(" y "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"'px'"),a("span",{class:"token punctuation"},";"),t("\n popup"),a("span",{class:"token punctuation"},"."),t("className "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'reveal'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1),E=a("h3",{id:"env-and-config"},[a("a",{class:"header-anchor",href:"#env-and-config","aria-hidden":"true"},"#"),t(" Env and Config")],-1),B=a("p",null,"配置文件以 .env/JS(Object)/JSON/JSONP/XML/YML 格式单独存放,方便读取",-1),F=a("h3",{id:"stand-library-idioms"},[a("a",{class:"header-anchor",href:"#stand-library-idioms","aria-hidden":"true"},"#"),t(" Stand Library Idioms")],-1),T=a("ul",null,[a("li",null,[t("use "),a("code",null,"Number.isNaN"),t(" not "),a("code",null,"isNaN")]),a("li",null,[t("use "),a("code",null,"Number.isFinite"),t(" not "),a("code",null,"isFinite")])],-1),V=a("h3",{id:"other"},[a("a",{class:"header-anchor",href:"#other","aria-hidden":"true"},"#"),t(" Other")],-1),q=a("p",null,"!!result 转化成 Boolean",-1),R=a("h2",{id:"modular-patterns"},[a("a",{class:"header-anchor",href:"#modular-patterns","aria-hidden":"true"},"#"),t(" Modular Patterns")],-1),U=a("h3",{id:"object-literal"},[a("a",{class:"header-anchor",href:"#object-literal","aria-hidden":"true"},"#"),t(" Object Literal")],-1),H=a("p",null,"通过对象字面量创建命名空间",-1),z=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"namespace"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"namespaceString"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" parts "),a("span",{class:"token operator"},"="),t(" namespaceString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"split"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'.'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t("\n parent "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},","),t("\n i"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// strip redundant leading global"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("parts"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'APP'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// remove leading global"),t("\n parts "),a("span",{class:"token operator"},"="),t(" parts"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" parts"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// create a property if it doesn't exist"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" parent"),a("span",{class:"token punctuation"},"["),t("parts"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'undefined'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n parent"),a("span",{class:"token punctuation"},"["),t("parts"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"//关键: 向内嵌套"),t("\n parent "),a("span",{class:"token operator"},"="),t(" parent"),a("span",{class:"token punctuation"},"["),t("parts"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"// 返回最内层模块"),t("\n "),a("span",{class:"token keyword"},"return"),t(" parent"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br")])],-1),G=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// assign returned value to a local var"),t("\n"),a("span",{class:"token keyword"},"var"),t(" module2 "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"namespace"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'APP.modules.module2'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nmodule2 "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),t("modules"),a("span",{class:"token punctuation"},"."),t("module2"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token comment"},"// true"),t("\n"),a("span",{class:"token comment"},"// skip initial `APP`"),t("\n"),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"namespace"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'modules.module51'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"// long namespace"),t("\n"),a("span",{class:"token constant"},"APP"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"namespace"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'once.upon.a.time.there.was.this.long.nested.property'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),W=a("h3",{id:"iife-pattern"},[a("a",{class:"header-anchor",href:"#iife-pattern","aria-hidden":"true"},"#"),t(" IIFE Pattern")],-1),Q=a("p",null,"立即函数模式, 通过调用立即函数,返回一个对象,暴露(exposed to public)公共接口(特权/公共方法):",-1),X=a("ul",null,[a("li",null,"闭包: 定义私有变量与特权方法"),a("li",null,"返回对象: 即使通过外部代码改变返回对象的接口,也不会影响原接口")],-1),J=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" obj "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// private member"),t("\n "),a("span",{class:"token keyword"},"var"),t(" name "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},'"tazimi"'),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token comment"},"// private method"),t("\n "),a("span",{class:"token comment"},"// excluded in return object"),t("\n\n "),a("span",{class:"token comment"},"// privileged method"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"getName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" name"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"setName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"n"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" n "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'string'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n name "),a("span",{class:"token operator"},"="),t(" n"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token comment"},"// public method"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"logName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("name"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// 闭包"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// 公共接口: 特权/公共方法"),t("\n\n "),a("span",{class:"token comment"},"// 特权方法"),t("\n getName"),a("span",{class:"token operator"},":"),t(" getName"),a("span",{class:"token punctuation"},","),t("\n setName"),a("span",{class:"token operator"},":"),t(" setName"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token comment"},"// 公共方法"),t("\n log"),a("span",{class:"token operator"},":"),t(" logName"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br")])],-1),K=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" App "),a("span",{class:"token operator"},"="),t(" App "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nApp"),a("span",{class:"token punctuation"},"."),t("utils "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" val "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getValue"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" val"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"setValue"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"newVal"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n val "),a("span",{class:"token operator"},"="),t(" newVal"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// also introduce a new sub-namespace"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("tools "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("App"),a("span",{class:"token punctuation"},"."),t("utils"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// inject new behavior into the tools namespace"),t("\n"),a("span",{class:"token comment"},"// which we defined via the utilities module"),t("\n\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"diagnose"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'diagnosis'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("App"),a("span",{class:"token punctuation"},"."),t("utils"),a("span",{class:"token punctuation"},"."),t("tools"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br")])],-1),Y=a("ul",null,[a("li",null,"jQuery Plugin Pattern: 通过给立即函数传参,注入全局变量/其他依赖")],-1),Z=a("h3",{id:"umd-pattern"},[a("a",{class:"header-anchor",href:"#umd-pattern","aria-hidden":"true"},"#"),t(" UMD Pattern")],-1),nn=a("p",null,"Universal Module Definition:",-1),sn=a("ul",null,[a("li",null,"先判断是否支持 Node.js 的模块(exports),存在则使用 Node.js 模块模式"),a("li",null,"再判断是否支持 AMD(define),存在则使用 AMD 方式加载模块")],-1),an=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("window"),a("span",{class:"token punctuation"},","),t(" factory")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" exports "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'object'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"factory"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" define "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'function'"),t(),a("span",{class:"token operator"},"&&"),t(" define"),a("span",{class:"token punctuation"},"."),t("amd"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"define"),a("span",{class:"token punctuation"},"("),t("factory"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n window"),a("span",{class:"token punctuation"},"."),t("eventUtil "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"factory"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"//module ..."),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),tn=a("h2",{id:"common-design-patterns"},[a("a",{class:"header-anchor",href:"#common-design-patterns","aria-hidden":"true"},"#"),t(" Common Design Patterns")],-1),en=a("p",null,[a("img",{src:"/awesome-notes/assets/DesignPatterns.df4b7baf.png",alt:"Common Design Patterns"})],-1),on=a("h3",{id:"classification"},[a("a",{class:"header-anchor",href:"#classification","aria-hidden":"true"},"#"),t(" Classification")],-1),cn=a("h4",{id:"creation-patterns"},[a("a",{class:"header-anchor",href:"#creation-patterns","aria-hidden":"true"},"#"),t(" Creation Patterns")],-1),pn=a("ul",null,[a("li",null,"Factory Method(工厂方法) | 通过将数据和事件接口化来构建若干个子类。"),a("li",null,"Abstract Factory(抽象工厂) | 建立若干族类的一个实例,这个实例不需要具体类的细节信息。(抽象类)"),a("li",null,"Builder(建造者) | 将对象的构建方法和其表现形式分离开来,总是构建相同类型的对象。"),a("li",null,"Prototype(原型) | 一个完全初始化的实例,用于拷贝或者克隆。"),a("li",null,"Singleton(单例) | 一个类只有唯一的一个实例,这个实例在整个程序中有一个全局的访问点。")],-1),ln=a("h4",{id:"structural-patterns"},[a("a",{class:"header-anchor",href:"#structural-patterns","aria-hidden":"true"},"#"),t(" Structural Patterns")],-1),un=a("ul",null,[a("li",null,"Adapter(适配器) : 将不同类的接口进行匹配,调整,这样尽管内部接口不兼容但是不同的类还是可以协同工作的。"),a("li",null,"Bridge(桥接模式) : 将对象的接口从其实现中分离出来,这样对象的实现和接口可以独立的变化。"),a("li",null,"Composite(组合模式) : 通过将简单可组合的对象组合起来,构成一个完整的对象,这个对象的能力将会超过这些组成部分的能力的总和,即会有新的能力产生。"),a("li",null,"Decorator(装饰器) : 动态给对象增加一些可替换的处理流程。"),a("li",null,"Facade(外观模式) : 一个类隐藏了内部子系统的复杂度,只暴露出一些简单的接口。"),a("li",null,"Flyweight(享元模式) : 一个细粒度对象,用于将包含在其它地方的信息 在不同对象之间高效地共享。"),a("li",null,"Proxy(代理模式) : 一个充当占位符的对象用来代表一个真实的对象。")],-1),rn=a("h4",{id:"behavioral-patterns"},[a("a",{class:"header-anchor",href:"#behavioral-patterns","aria-hidden":"true"},"#"),t(" Behavioral Patterns")],-1),kn=a("ul",null,[a("li",null,"Interpreter(解释器) : 将语言元素包含在一个应用中的一种方式,用于匹配目标语言的语法。"),a("li",null,"Template Method(模板方法) : 在一个方法中为某个算法建立一层外壳,将算法的具体步骤交付给子类去做。"),a("li",null,"Chain of Responsibility(响应链) : 一种将请求在一串对象中传递的方式,寻找可以处理这个请求的对象。"),a("li",null,"Command(命令) : 封装命令请求为一个对象,从而使记录日志,队列缓存请求,未处理请求进行错误处理 这些功能称为可能。"),a("li",null,"Iterator(迭代器) : 在不需要直到集合内部工作原理的情况下,顺序访问一个集合里面的元素。"),a("li",null,"Mediator(中介者模式) : 在类之间定义简化的通信方式,用于避免类之间显式的持有彼此的引用。"),a("li",null,"Observer(观察者模式) : 用于将变化通知给多个类的方式,可以保证类之间的一致性。"),a("li",null,"State(状态) : 当对象状态改变时,改变对象的行为。"),a("li",null,"Strategy(策略) : 将算法封装到类中,将选择和实现分离开来。"),a("li",null,"Visitor(访问者) : 为类增加新的操作而不改变类本身。")],-1),bn=a("h3",{id:"prototype"},[a("a",{class:"header-anchor",href:"#prototype","aria-hidden":"true"},"#"),t(" Prototype")],-1),mn=a("p",null,"关键方法 clone()",-1),dn=a("h3",{id:"bridge"},[a("a",{class:"header-anchor",href:"#bridge","aria-hidden":"true"},"#"),t(" Bridge")],-1),yn=a("p",null,"分离抽象和实现/分离对象的两种不同属性",-1),fn=a("p",null,"e.g 从 2 个不同维度上扩展对象",-1),hn=a("h3",{id:"composite"},[a("a",{class:"header-anchor",href:"#composite","aria-hidden":"true"},"#"),t(" Composite")],-1),wn=a("p",null,"树形结构:",-1),gn=a("ul",null,[a("li",null,[t("根结点 "),a("ul",null,[a("li",null,"Component 抽象对象/接口 采用最大宽接口,定义内点和叶点的操作"),a("li",null,"将内点特有的操作集设为缺省操作集(空实现)")])]),a("li",null,[t("内点 "),a("ul",null,[a("li",null,"持有父结点和子节点的引用(可使用 Flyweight 模式实现共享)"),a("li",null,"操作集:内点操作集(可添加/删除组件)")])]),a("li",null,[t("叶点 "),a("ul",null,[a("li",null,"持有父结点引用"),a("li",null,"操作集:叶点操作集(不可添加/删除组件)")])])],-1),vn=a("h3",{id:"strategy"},[a("a",{class:"header-anchor",href:"#strategy","aria-hidden":"true"},"#"),t(" Strategy")],-1),Pn=a("p",null,"改变对象的内核/算法, 一个 Strategy 对象封装一个算法",-1),Cn=a("h3",{id:"state"},[a("a",{class:"header-anchor",href:"#state","aria-hidden":"true"},"#"),t(" State")],-1),Sn=a("p",null,"一个 State 对象封装一个与状态相关的行为",-1),jn=a("h3",{id:"iterator"},[a("a",{class:"header-anchor",href:"#iterator","aria-hidden":"true"},"#"),t(" Iterator")],-1),Mn=a("p",null,"一个 Iterator 对象封装访问和遍历一个聚集对象中的各个构件的方法",-1),xn=a("h3",{id:"ioc-and-di"},[a("a",{class:"header-anchor",href:"#ioc-and-di","aria-hidden":"true"},"#"),t(" IOC and DI")],-1),Dn=a("ul",null,[a("li",null,"IOC (inversion of control) 控制反转模式;控制反转是将组件间的依赖关系从程序内部提到外部来管理"),a("li",null,"DI (dependency injection) 依赖注入模式;依赖注入是指将组件的依赖通过外部以参数或其他形式注入")],-1),An=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"DbMysql"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"query"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"Controller"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" $db"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"__construct"),a("span",{class:"token punctuation"},"("),t("$dbMysql"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("db "),a("span",{class:"token operator"},"="),t(" $dbMysql"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("db"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"query"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n$db "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"DbMysql"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$c "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Controller"),a("span",{class:"token punctuation"},"("),t("$db"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$c"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),In=a("p",null,"With IOC container:",-1),Nn=a("div",{class:"language-java line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"DbMysql"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"__construct"),a("span",{class:"token punctuation"},"("),t("$host"),a("span",{class:"token punctuation"},","),t(" $name"),a("span",{class:"token punctuation"},","),t(" $pwd"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// do something"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"query"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n echo __METHOD__ "),a("span",{class:"token punctuation"},"."),t(" PHP_EOL"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"DbRedis"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"__construct"),a("span",{class:"token punctuation"},"("),t("$host"),a("span",{class:"token punctuation"},","),t(" $name"),a("span",{class:"token punctuation"},","),t(" $pwd"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// do something"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"set"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n echo __METHOD__ "),a("span",{class:"token punctuation"},"."),t(" PHP_EOL"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"class"),t(" controller "),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" $mysql"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"public"),t(" $redis"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"__construct"),a("span",{class:"token punctuation"},"("),t("$mysql"),a("span",{class:"token punctuation"},","),t(" $redis"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("mysql "),a("span",{class:"token operator"},"="),t(" $mysql"),a("span",{class:"token punctuation"},";"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("redis "),a("span",{class:"token operator"},"="),t(" $redis"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("mysql"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"query"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("redis"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"set"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n\n"),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"Container"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"public"),t(" $bindings "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"bind"),a("span",{class:"token punctuation"},"("),t("$key"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token class-name"},"Closure"),t(" $value"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("bindings"),a("span",{class:"token punctuation"},"["),t("$key"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" $value"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"public"),t(" function "),a("span",{class:"token function"},"make"),a("span",{class:"token punctuation"},"("),t("$key"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token operator"},"="),t(" $"),a("span",{class:"token keyword"},"this"),a("span",{class:"token operator"},"->"),t("bindings"),a("span",{class:"token punctuation"},"["),t("$key"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" $"),a("span",{class:"token keyword"},"new"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n$app "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Container"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"bind"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'mysql'"),a("span",{class:"token punctuation"},","),t(" function "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"DbMysql"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'host'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'name'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'pwd'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"bind"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'redis'"),a("span",{class:"token punctuation"},","),t(" function "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t(),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"DbRedis"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'host'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'name'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'pwd'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"bind"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'controller'"),a("span",{class:"token punctuation"},","),t(" function "),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(" use "),a("span",{class:"token punctuation"},"("),t("$app"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Controller"),a("span",{class:"token punctuation"},"("),t("$app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"make"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'mysql'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t(" $app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"make"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'redis'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$controller "),a("span",{class:"token operator"},"="),t(" $app"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"make"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'controller'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n$controller"),a("span",{class:"token operator"},"->"),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"/** * 输出: * DbMysql::query * DbRedis::set */"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br")])],-1),On=a("p",null,"With dependency injection:",-1),Ln=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// dependency provider"),t("\n"),a("span",{class:"token keyword"},"export"),t(),a("span",{class:"token keyword"},"interface"),t(),a("span",{class:"token class-name"},"IProvider"),a("span",{class:"token operator"},"<"),a("span",{class:"token constant"},"T"),a("span",{class:"token operator"},">"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"provide"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token constant"},"T"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n@"),a("span",{class:"token function"},"injectable"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t("\n"),a("span",{class:"token keyword"},"export"),t(),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"NameProvider"),t(),a("span",{class:"token keyword"},"implements"),t(),a("span",{class:"token class-name"},"IProvider"),a("span",{class:"token operator"},"<"),t("string"),a("span",{class:"token operator"},">"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"provide"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},'"World"'),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// top module"),t("\n"),a("span",{class:"token keyword"},"import"),t(),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token keyword"},"as"),t(" React "),a("span",{class:"token keyword"},"from"),t(),a("span",{class:"token string"},'"react"'),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"import"),t(),a("span",{class:"token punctuation"},"{"),t(" IProvider "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"from"),t(),a("span",{class:"token string"},'"./providers"'),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"export"),t(),a("span",{class:"token keyword"},"class"),t(),a("span",{class:"token class-name"},"Hello"),t(),a("span",{class:"token keyword"},"extends"),t(),a("span",{class:"token class-name"},[t("React"),a("span",{class:"token punctuation"},"."),t("Component")]),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"private"),t(" readonly nameProvider"),a("span",{class:"token operator"},":"),t(" IProvider"),a("span",{class:"token operator"},"<"),t("string"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token function"},"render"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token operator"},"<"),t("h1"),a("span",{class:"token operator"},">"),t("Hello "),a("span",{class:"token punctuation"},"{"),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("nameProvider"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"provide"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token operator"},"!"),a("span",{class:"token operator"},"<"),a("span",{class:"token operator"},"/"),t("h1"),a("span",{class:"token operator"},">"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br")])],-1),$n=a("h3",{id:"class-pattern"},[a("a",{class:"header-anchor",href:"#class-pattern","aria-hidden":"true"},"#"),t(" Class Pattern")],-1),_n=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"Person"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("firstName"),a("span",{class:"token punctuation"},","),t(" lastName")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("firstName "),a("span",{class:"token operator"},"="),t(" firstName"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("lastName "),a("span",{class:"token operator"},"="),t(" lastName"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("gender "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'male'"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},'// Define a subclass constructor for for "Superhero":'),t("\n"),a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"Superhero"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("firstName"),a("span",{class:"token punctuation"},","),t(" lastName"),a("span",{class:"token punctuation"},","),t(" powers")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// Invoke the superclass constructor on the new object"),t("\n "),a("span",{class:"token comment"},"// then use .call() to invoke the constructor as a method of"),t("\n "),a("span",{class:"token comment"},"// the object to be initialized."),t("\n "),a("span",{class:"token function"},"Person"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(" firstName"),a("span",{class:"token punctuation"},","),t(" lastName"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},'// Finally, store their powers, a new array of traits not found in a normal "Person"'),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("powers "),a("span",{class:"token operator"},"="),t(" powers"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token class-name"},"SuperHero"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(" Object"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"create"),a("span",{class:"token punctuation"},"("),a("span",{class:"token class-name"},"Person"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br")])],-1),En=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" superman "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Superhero"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Clark'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Kent'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'flight'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'heat-vision'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nconsole"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("superman"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br")])],-1),Bn=a("h3",{id:"mix-in-pattern"},[a("a",{class:"header-anchor",href:"#mix-in-pattern","aria-hidden":"true"},"#"),t(" Mix-In Pattern")],-1),Fn=a("p",null,"将多个对象的属性混入同一个对象,达到继承/扩展/组合的效果",-1),Tn=a("ul",null,[a("li",null,"不改变原型链")],-1),Vn=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"mix"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" arg"),a("span",{class:"token punctuation"},","),t("\n prop"),a("span",{class:"token punctuation"},","),t("\n child "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("arg "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" arg "),a("span",{class:"token operator"},"<"),t(" arguments"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" arg "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("prop "),a("span",{class:"token keyword"},"in"),t(" arguments"),a("span",{class:"token punctuation"},"["),t("arg"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},"["),t("arg"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"hasOwnProperty"),a("span",{class:"token punctuation"},"("),t("prop"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n child"),a("span",{class:"token punctuation"},"["),t("prop"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" arguments"),a("span",{class:"token punctuation"},"["),t("arg"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("prop"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" child"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br")])],-1),qn=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" cake "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"mix"),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token punctuation"},"{"),t(" eggs"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" large"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token boolean"},"true"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"{"),t(" butter"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(" salted"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token boolean"},"true"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"{"),t(" flour"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'3 cups'"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"{"),t(" sugar"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'sure!'"),t(),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br")])],-1),Rn=a("ul",null,[a("li",null,"改变原型链")],-1),Un=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// Extend an existing object with a method from another"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"mix"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("receivingClass"),a("span",{class:"token punctuation"},","),t(" givingClass")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// mix-in provide certain methods"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" len "),a("span",{class:"token operator"},"="),t(" arguments"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" len"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n receivingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("arguments"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t("\n givingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("arguments"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"// mix-in provide obj"),t("\n "),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" methodName "),a("span",{class:"token keyword"},"in"),t(" givingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("receivingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("methodName"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n receivingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("methodName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t("\n givingClass"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("methodName"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br")])],-1),Hn=a("h3",{id:"singleton-pattern"},[a("a",{class:"header-anchor",href:"#singleton-pattern","aria-hidden":"true"},"#"),t(" Singleton Pattern")],-1),zn=a("p",null,"原型与构造函数指针运作正常",-1),Gn=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Universe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// 缓存实例"),t("\n "),a("span",{class:"token keyword"},"var"),t(" instance"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// anti-Self-Defined Function Pattern"),t("\n "),a("span",{class:"token comment"},"// 反-自定义函数模式: 先重写,再初始化"),t("\n "),a("span",{class:"token function-variable function"},"Universe"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Universe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" instance"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// 保存原型,使其一直保持于同一位置"),t("\n "),a("span",{class:"token comment"},"// (this指针指向不重要)"),t("\n "),a("span",{class:"token class-name"},"Universe"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n\n instance "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Universe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// 重定向constructor指针"),t("\n instance"),a("span",{class:"token punctuation"},"."),t("constructor "),a("span",{class:"token operator"},"="),t(" Universe"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// 功能代码"),t("\n instance"),a("span",{class:"token punctuation"},"."),t("start_time "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n instance"),a("span",{class:"token punctuation"},"."),t("bang "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'Big'"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" instance"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br")])],-1),Wn=a("h3",{id:"abstract-factory"},[a("a",{class:"header-anchor",href:"#abstract-factory","aria-hidden":"true"},"#"),t(" Abstract Factory")],-1),Qn=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" AbstractVehicleFactory "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// Storage for our vehicle types"),t("\n "),a("span",{class:"token keyword"},"var"),t(" types "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_getVehicle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("type"),a("span",{class:"token punctuation"},","),t(" customizations")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" Vehicle "),a("span",{class:"token operator"},"="),t(" types"),a("span",{class:"token punctuation"},"["),t("type"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" Vehicle "),a("span",{class:"token operator"},"?"),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Vehicle"),a("span",{class:"token punctuation"},"("),t("customizations"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_registerVehicle"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("type"),a("span",{class:"token punctuation"},","),t(" Vehicle")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token class-name"},"Vehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// only register classes that fulfill the vehicle contract"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("prototype"),a("span",{class:"token punctuation"},"."),t("drive "),a("span",{class:"token operator"},"&&"),t(" prototype"),a("span",{class:"token punctuation"},"."),t("breakDown"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n types"),a("span",{class:"token punctuation"},"["),t("type"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" Vehicle"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" AbstractVehicleFactory"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n getVehicle"),a("span",{class:"token operator"},":"),t(" _getVehicle"),a("span",{class:"token punctuation"},","),t("\n registerVehicle"),a("span",{class:"token operator"},":"),t(" _registerVehicle"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br")])],-1),Xn=a("h3",{id:"factory-method"},[a("a",{class:"header-anchor",href:"#factory-method","aria-hidden":"true"},"#"),t(" Factory Method")],-1),Jn=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[t("module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"VehicleFactory"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" publicVehicle "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Object"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// specific factory"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Car"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"options"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'car'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("doors "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("doors "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("state "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("state "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'brand new'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'silver'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"10"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Truck"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"options"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'truck'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("state "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("state "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'used'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("wheelSize "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("wheelSize "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'large'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token string"},"'blue'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"="),t(" options"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"8"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// public features of vehicle , added to __proto__"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" args "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("args"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' - run with: '"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"'km/s'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("args"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object Number]'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("speed "),a("span",{class:"token operator"},"="),t(" args"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_withColor"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" args "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("args"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token string"},"'The color of this '"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' product is : '"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("color\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("args"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object String]'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("color "),a("span",{class:"token operator"},"="),t(" args"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"// provide a function to change other public features"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_reform"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("funcName"),a("span",{class:"token punctuation"},","),t(" newFunc")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token keyword"},"typeof"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'function'"),t(),a("span",{class:"token operator"},"||"),t("\n "),a("span",{class:"token keyword"},"typeof"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'function'"),t("\n "),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"delete"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" newFunc"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token comment"},"// provide a function to add new public features"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"_addFeature"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("funcName"),a("span",{class:"token punctuation"},","),t(" newFunc")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'undefined'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" newFunc"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("funcName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" newFunc"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// private features, added to obj"),t("\n\n "),a("span",{class:"token comment"},"// core: create method"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"create"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"options"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" vehicleClass "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"''"),a("span",{class:"token punctuation"},","),t("\n newVehicle "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("options"),a("span",{class:"token punctuation"},"."),t("type "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'car'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n vehicleClass "),a("span",{class:"token operator"},"="),t(" Car"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n vehicleClass "),a("span",{class:"token operator"},"="),t(" Truck"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// create new vehicle with options, by pre-defined specific constructor"),t("\n newVehicle "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"vehicleClass"),a("span",{class:"token punctuation"},"("),t("options"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// set up prototype"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("__proto__ "),a("span",{class:"token operator"},"="),t(" publicVehicle"),a("span",{class:"token punctuation"},";"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(" publicVehicle"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// add public feature"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),t("run "),a("span",{class:"token operator"},"="),t(" _run"),a("span",{class:"token punctuation"},";"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),t("withColor "),a("span",{class:"token operator"},"="),t(" _withColor"),a("span",{class:"token punctuation"},";"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),t("reform "),a("span",{class:"token operator"},"="),t(" _reform"),a("span",{class:"token punctuation"},";"),t("\n newVehicle"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),t("addFeature "),a("span",{class:"token operator"},"="),t(" _addFeature"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// add private(separately) feature"),t("\n\n "),a("span",{class:"token comment"},"// return new obj"),t("\n "),a("span",{class:"token keyword"},"return"),t(" newVehicle"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// define more factory"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n vehicleFactory"),a("span",{class:"token operator"},":"),t(" VehicleFactory"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br"),a("span",{class:"line-number"},"83"),a("br"),a("span",{class:"line-number"},"84"),a("br"),a("span",{class:"line-number"},"85"),a("br"),a("span",{class:"line-number"},"86"),a("br"),a("span",{class:"line-number"},"87"),a("br"),a("span",{class:"line-number"},"88"),a("br"),a("span",{class:"line-number"},"89"),a("br"),a("span",{class:"line-number"},"90"),a("br"),a("span",{class:"line-number"},"91"),a("br"),a("span",{class:"line-number"},"92"),a("br"),a("span",{class:"line-number"},"93"),a("br"),a("span",{class:"line-number"},"94"),a("br"),a("span",{class:"line-number"},"95"),a("br"),a("span",{class:"line-number"},"96"),a("br"),a("span",{class:"line-number"},"97"),a("br")])],-1),Kn=a("h3",{id:"adapter-pattern"},[a("a",{class:"header-anchor",href:"#adapter-pattern","aria-hidden":"true"},"#"),t(" Adapter Pattern")],-1),Yn=a("p",null,"适配器通过内部使用新接口规定的属性/方法, 创建一个外观与旧接口一致 的方法",-1),Zn=a("ul",null,[a("li",null,"old.method();"),a("li",null,"adapter.method(); // 实现此 method 时,使用了新接口规定的属性/方法")],-1),ns=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// old interface"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Shipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"request"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("zipStart"),a("span",{class:"token punctuation"},","),t(" zipEnd"),a("span",{class:"token punctuation"},","),t(" weight")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// ..."),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'$49.75'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// new interface"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"AdvancedShipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"login"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"credentials"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"/* ... */"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"setStart"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"start"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"/* ... */"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"setDestination"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"destination"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"/* ... */"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"calculate"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"weight"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'$39.50'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// adapter interface"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"AdapterShipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"credentials"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" shipping "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"AdvancedShipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"login"),a("span",{class:"token punctuation"},"("),t("credentials"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"request"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("zipStart"),a("span",{class:"token punctuation"},","),t(" zipEnd"),a("span",{class:"token punctuation"},","),t(" weight")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"setStart"),a("span",{class:"token punctuation"},"("),t("zipStart"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"setDestination"),a("span",{class:"token punctuation"},"("),t("zipEnd"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"calculate"),a("span",{class:"token punctuation"},"("),t("weight"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br")])],-1),ss=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" shipping "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Shipping"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"var"),t(" adapterShipping "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"AdapterShipping"),a("span",{class:"token punctuation"},"("),t("credentials"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// original shipping object and interface"),t("\n"),a("span",{class:"token keyword"},"var"),t(" cost "),a("span",{class:"token operator"},"="),t(" shipping"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'78701'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'10010'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2 lbs'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nlog"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Old cost: '"),t(),a("span",{class:"token operator"},"+"),t(" cost"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"// new shipping object with adapted interface"),t("\ncost "),a("span",{class:"token operator"},"="),t(" adapter"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"request"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'78701'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'10010'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2 lbs'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br")])],-1),as=a("h3",{id:"decorator-pattern"},[a("a",{class:"header-anchor",href:"#decorator-pattern","aria-hidden":"true"},"#"),t(" Decorator Pattern")],-1),ts=a("ul",null,[a("li",null,"重写/重载/扩展对象原有的行为(method),但不改变对象原有属性"),a("li",null,"可以添加新属性,并围绕新属性扩展对象的原行为 e.g 原对象只会说中文,装饰后同时说中文与英文"),a("li",null,"避免了通过继承来为类型添加新的职责的形式可取,通过继承的方式容易造成子类的膨胀"),a("li",null,"保持接口的一致性,动态改变对象的外观/职责"),a("li",null,"ConcreteDecorator 类: private ClassName component;(拥有一个对象引用)")],-1),es=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"__decorate"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("decorators"),a("span",{class:"token punctuation"},","),t(" target"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" desc")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"const"),t(" argumentsLength "),a("span",{class:"token operator"},"="),t(" arguments"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"let"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"let"),t(" decorator"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("argumentsLength "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// class decorator"),t("\n descriptorOrTarget "),a("span",{class:"token operator"},"="),t(" target"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("desc "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token keyword"},"null"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// method decorator"),t("\n descriptorOrTarget "),a("span",{class:"token operator"},"="),t(" Object"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getOwnPropertyDescriptor"),a("span",{class:"token punctuation"},"("),t("target"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"let"),t(" i "),a("span",{class:"token operator"},"="),t(" decorators"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"-"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},">="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"("),t("decorator "),a("span",{class:"token operator"},"="),t(" decorators"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("argumentsLength "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// if the decorator function returns a value use it;"),t("\n "),a("span",{class:"token comment"},"// otherwise use the original."),t("\n descriptorOrTarget "),a("span",{class:"token operator"},"="),t("\n "),a("span",{class:"token function"},"decorator"),a("span",{class:"token punctuation"},"("),t("descriptorOrTarget"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"||"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// if the decorator function returns a descriptor use it;"),t("\n "),a("span",{class:"token comment"},"// otherwise use the original."),t("\n descriptorOrTarget "),a("span",{class:"token operator"},"="),t("\n "),a("span",{class:"token function"},"decorator"),a("span",{class:"token punctuation"},"("),t("target"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"||"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("argumentsLength "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"3"),t(),a("span",{class:"token operator"},"&&"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n Object"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"defineProperty"),a("span",{class:"token punctuation"},"("),t("target"),a("span",{class:"token punctuation"},","),t(" key"),a("span",{class:"token punctuation"},","),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" descriptorOrTarget"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br")])],-1),os=a("h4",{id:"decorator-implementation"},[a("a",{class:"header-anchor",href:"#decorator-implementation","aria-hidden":"true"},"#"),t(" Decorator Implementation")],-1),cs=a("p",null,"关键在于实现传递方式, 两种方式:",-1),ps=a("ul",null,[a("li",null,"uber 属性获得每次装饰后结果"),a("li",null,"循环叠加每次装饰后结果")],-1),ls=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 构造函数"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Sale"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price "),a("span",{class:"token operator"},"="),t(" price "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getPrice"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// 定义具体装饰器"),t("\n"),a("span",{class:"token comment"},"// 通过uber属性获得上一次装饰后的结果"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("fedTax "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" price "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("uber"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n price "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token punctuation"},"("),t("price "),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" price"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("quebec "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" price "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("uber"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n price "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token punctuation"},"("),t("price "),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token number"},"7.5"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" price"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("money "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'$'"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("uber"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"toFixed"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("cdn "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'CDN$ '"),t(),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("uber"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"toFixed"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"decorate"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"decorator"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"F"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n overrides "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("constructor"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"["),t("decorator"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t("\n i"),a("span",{class:"token punctuation"},","),t("\n newObj"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// 临时代理构造函数"),t("\n "),a("span",{class:"token class-name"},"F"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n newObj "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"F"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// 传递实现的关键"),t("\n "),a("span",{class:"token comment"},"// 通过uber属性获得上一次装饰后的结果"),t("\n newObj"),a("span",{class:"token punctuation"},"."),t("uber "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token class-name"},"F"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token keyword"},"in"),t(" overrides"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("overrides"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"hasOwnProperty"),a("span",{class:"token punctuation"},"("),t("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n newObj"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" overrides"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" newObj"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br")])],-1),us=a("h4",{id:"decorators-list"},[a("a",{class:"header-anchor",href:"#decorators-list","aria-hidden":"true"},"#"),t(" Decorators List")],-1),is=a("div",{class:"language-javascript line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// 构造函数"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Sale"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price "),a("span",{class:"token operator"},"="),t(" price "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token number"},"0"),t(),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("decorators_list "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getPrice"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// 定义具体装饰器"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("fedTax "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" price "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token punctuation"},"("),t("price "),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("quebec "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" price "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token punctuation"},"("),t("price "),a("span",{class:"token operator"},"*"),t(),a("span",{class:"token number"},"7.5"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"100"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nSale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"."),t("money "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getPrice"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"price"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'$'"),t(),a("span",{class:"token operator"},"+"),t(" price"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"toFixed"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"decorate"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"decorator"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("decorators_list"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),t("decorator"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token class-name"},"Sale"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getPrice"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" price "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("price"),a("span",{class:"token punctuation"},","),t("\n i"),a("span",{class:"token punctuation"},","),t("\n max "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("decorators_list"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},","),t("\n name"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" max"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"+="),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n name "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("decorators_list"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// 传递实现的关键"),t("\n "),a("span",{class:"token comment"},"// 通过循环叠加上一次装饰后的结果"),t("\n price "),a("span",{class:"token operator"},"="),t(" Sale"),a("span",{class:"token punctuation"},"."),t("decorators"),a("span",{class:"token punctuation"},"["),t("name"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getPrice"),a("span",{class:"token punctuation"},"("),t("price"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" price"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br")])],-1),rs=a("h4",{id:"实现-2"},[a("a",{class:"header-anchor",href:"#实现-2","aria-hidden":"true"},"#"),t(" 实现 2")],-1),ks=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// The constructor to decorate"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"MacBook"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"cost"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token number"},"997"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"screenSize"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token number"},"11.6"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// Decorator 1"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Memory"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"macBook"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" v "),a("span",{class:"token operator"},"="),t(" macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"cost"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"cost"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" v "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"75"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// Decorator 2"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Engraving"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"macBook"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" v "),a("span",{class:"token operator"},"="),t(" macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"cost"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"cost"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" v "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"200"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// Decorator 3"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Insurance"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"macBook"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" v "),a("span",{class:"token operator"},"="),t(" macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"cost"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n macBook"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"cost"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" v "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token number"},"250"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br")])],-1),bs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" mb "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MacBook"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token function"},"Memory"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token function"},"Engraving"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token function"},"Insurance"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// Outputs: 1522"),t("\nconsole"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"cost"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// Outputs: 11.6"),t("\nconsole"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("mb"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"screenSize"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),ms=a("h3",{id:"facade-pattern"},[a("a",{class:"header-anchor",href:"#facade-pattern","aria-hidden":"true"},"#"),t(" Facade Pattern")],-1),ds=a("p",null,"将多个复杂的子系统封装+合并,实现一个复杂功能,但只暴露一个简单的接口 - 封装复杂逻辑",-1),ys=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" sabertazimi "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\nsabertazimi"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"addMyEvent"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("el"),a("span",{class:"token punctuation"},","),t(" ev"),a("span",{class:"token punctuation"},","),t(" fn")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("el"),a("span",{class:"token punctuation"},"."),t("addEventListener"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n el"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"addEventListener"),a("span",{class:"token punctuation"},"("),t("ev"),a("span",{class:"token punctuation"},","),t(" fn"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("el"),a("span",{class:"token punctuation"},"."),t("attachEvent"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n el"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"attachEvent"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'on'"),t(),a("span",{class:"token operator"},"+"),t(" ev"),a("span",{class:"token punctuation"},","),t(" fn"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n el"),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'on'"),t(),a("span",{class:"token operator"},"+"),t(" ev"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" fn"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br")])],-1),fs=a("h3",{id:"flyweight-pattern"},[a("a",{class:"header-anchor",href:"#flyweight-pattern","aria-hidden":"true"},"#"),t(" Flyweight Pattern")],-1),hs=a("p",null,"减小内存开销:",-1),ws=a("ul",null,[a("li",null,"内在信息 - 对象中的内部方法所需信息/属性, 一个单独的享元可替代大量具有相同内在信息的对象"),a("li",null,"外部状态作为方法参数:使之适应不同的外部状态(context)——对象实例差异处"),a("li",null,"某个类型的对象有大量的实例,对这些实例进行分类,合并相同分类的对象,只创建少量实例(享元)"),a("li",null,"通过享元工厂来管理一组享元,当所需享元已存在时,返回已存在享元;当所需享元不存在时,创建新享元")],-1),gs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Flyweight"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("make "),a("span",{class:"token operator"},"="),t(" make"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("model "),a("span",{class:"token operator"},"="),t(" model"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("processor "),a("span",{class:"token operator"},"="),t(" processor"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"var"),t(" FlyWeightFactory "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" flyweights "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"get"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// 不存在所需享元,新建新享元"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("flyweights"),a("span",{class:"token punctuation"},"["),t("make "),a("span",{class:"token operator"},"+"),t(" model"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n flyweights"),a("span",{class:"token punctuation"},"["),t("make "),a("span",{class:"token operator"},"+"),t(" model"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Flyweight"),a("span",{class:"token punctuation"},"("),t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" flyweights"),a("span",{class:"token punctuation"},"["),t("make "),a("span",{class:"token operator"},"+"),t(" model"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token function-variable function"},"getCount"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" count "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" f "),a("span",{class:"token keyword"},"in"),t(" flyweights"),a("span",{class:"token punctuation"},")"),t(" count"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" count"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"Computer"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},","),t(" memory"),a("span",{class:"token punctuation"},","),t(" tag")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("flyweight "),a("span",{class:"token operator"},"="),t(" FlyWeightFactory"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"get"),a("span",{class:"token punctuation"},"("),t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("memory "),a("span",{class:"token operator"},"="),t(" memory"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("tag "),a("span",{class:"token operator"},"="),t(" tag"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getMake"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("flyweight"),a("span",{class:"token punctuation"},"."),t("make"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token comment"},"// ..."),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"ComputerCollection"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" computers "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"var"),t(" count "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"add"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},","),t(" memory"),a("span",{class:"token punctuation"},","),t(" tag")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n computers"),a("span",{class:"token punctuation"},"["),t("tag"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Computer"),a("span",{class:"token punctuation"},"("),t("make"),a("span",{class:"token punctuation"},","),t(" model"),a("span",{class:"token punctuation"},","),t(" processor"),a("span",{class:"token punctuation"},","),t(" memory"),a("span",{class:"token punctuation"},","),t(" tag"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n count"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token function-variable function"},"get"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"tag"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" computers"),a("span",{class:"token punctuation"},"["),t("tag"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token function-variable function"},"getCount"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" count"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" computers "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"ComputerCollection"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Dell'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Studio XPS'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'5G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Y755P'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Dell'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Studio XPS'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'6G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'X997T'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Dell'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Studio XPS'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'NT777'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Dell'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Studio XPS'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'0J88A'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'HP'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Envy'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'4G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'CNU883701'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'HP'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Envy'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'Intel'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'2G'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'TXU003283'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Computers: '"),t(),a("span",{class:"token operator"},"+"),t(" computers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Flyweights: '"),t(),a("span",{class:"token operator"},"+"),t(" FlyWeightFactory"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getCount"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br")])],-1),vs=a("h3",{id:"proxy-pattern"},[a("a",{class:"header-anchor",href:"#proxy-pattern","aria-hidden":"true"},"#"),t(" Proxy Pattern")],-1),Ps=a("p",null,"通过一个代理对象,临时存储原对象方法调用产生的一系列结果(新建对象),减少重复对象的产生",-1),Cs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"GeoCoder"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"getLatLng"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"address"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'Amsterdam'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'52.3700° N, 4.8900° E'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'London'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'51.5171° N, 0.1062° W'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'Paris'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'48.8742° N, 2.3470° E'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'Berlin'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"'52.5233° N, 13.4127° E'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token string"},"''"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"GeoProxy"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" geocoder "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"GeoCoder"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"var"),t(" geocache "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"getLatLng"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"address"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("geocache"),a("span",{class:"token punctuation"},"["),t("address"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n geocache"),a("span",{class:"token punctuation"},"["),t("address"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" geocoder"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getLatLng"),a("span",{class:"token punctuation"},"("),t("address"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n log"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("address "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"': '"),t(),a("span",{class:"token operator"},"+"),t(" geocache"),a("span",{class:"token punctuation"},"["),t("address"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" geocache"),a("span",{class:"token punctuation"},"["),t("address"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"getCount"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" count "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" code "),a("span",{class:"token keyword"},"in"),t(" geocache"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n count"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" count"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br")])],-1),Ss=a("h3",{id:"command-pattern"},[a("a",{class:"header-anchor",href:"#command-pattern","aria-hidden":"true"},"#"),t(" Command Pattern")],-1),js=a("p",null,"有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁 (多个对象中的某个随机对象),也不知道被请求的操作是什么. 此时希望用一种松耦合的方式来设计程序,使得请求发送者和请求接收者能够消除彼此的耦合关系",-1),Ms=a("ul",null,[a("li",null,"将方法/动作封装成对象, 使得外部通过唯一方法 execute/run 调用内部方法/动作"),a("li",null,"客户创建命令;调用者执行该命令;接收者在命令执行时执行相应操作"),a("li",null,"客户通常被包装为一个对象,但是这不是必然的"),a("li",null,"调用者接过命令并将其保存下来, 它会在某个时候调用该命令对象的 execute 方法"),a("li",null,[t("调用者进行 "),a("code",null,"commandObject.execute"),t(" 这种调用时, 它所调用的方法将转而以 "),a("code",null,"receiver.action()"),t(" 这种形式调用恰当的方法")])],-1),xs=a("p",null,"client and receiver",-1),Ds=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"SimpleCommand"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"receiving"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("receiving "),a("span",{class:"token operator"},"="),t(" receiving"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"SimpleCommand"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"execute"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("receiving"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),As=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[t("module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" manager "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// command to be encapsulated"),t("\n manager"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"isNull"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"nu"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("nu"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object Null]'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n manager"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"isArray"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"arr"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("arr"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object Array]'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n manager"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"isString"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"str"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("str"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object String]'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// public api"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"execute"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"command"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"("),t("\n manager"),a("span",{class:"token punctuation"},"["),t("command"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"&&"),t("\n manager"),a("span",{class:"token punctuation"},"["),t("command"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("manager"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"run"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"command"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"("),t("\n manager"),a("span",{class:"token punctuation"},"["),t("command"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"&&"),t("\n manager"),a("span",{class:"token punctuation"},"["),t("command"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("manager"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token punctuation"},"{"),t("\n execute"),a("span",{class:"token operator"},":"),t(" execute"),a("span",{class:"token punctuation"},","),t("\n run"),a("span",{class:"token operator"},":"),t(" run"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br")])],-1),Is=a("p",null,"command pattern in UI development, bind command to UI components:",-1),Ns=a("ul",null,[a("li",null,"executor: UI components"),a("li",null,"client and receiver: background tasks or other UI components"),a("li",null,"executor -> client: command.execute() -> receiver: receiver.action()")],-1),Os=a("p",null,[t("e.g click "),a("code",null,"button"),t(" -> refresh "),a("code",null,"menu")],-1),Ls=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// receiver"),t("\n"),a("span",{class:"token keyword"},"const"),t(" MenuBar "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"refresh"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function"},"refresh"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'refresh menu pages'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// client: command object"),t("\n"),a("span",{class:"token comment"},"// command: object with `action` implemented"),t("\n"),a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"Command"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"receiver"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n receiver"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" RefreshMenuBarCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"Command"),a("span",{class:"token punctuation"},"("),t("MenuBar"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// executor"),t("\nbutton"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"setCommand"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"command"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(),a("span",{class:"token punctuation"},"{"),t("\n button"),a("span",{class:"token punctuation"},"."),t("command "),a("span",{class:"token operator"},"="),t(" command"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\nbutton"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"setCommand"),a("span",{class:"token punctuation"},"("),t("RefreshMenuBarCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\nbutton"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"addEventLister"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'click'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"event"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"=>"),t(),a("span",{class:"token punctuation"},"{"),t("\n button"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"command"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br")])],-1),$s=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"MenuCommand"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"action"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("action "),a("span",{class:"token operator"},"="),t(" action"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"execute"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"action"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"const"),t(" fileActions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"FileActions"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" EditActions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"EditActions"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" InsertActions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"InsertActions"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" HelpActions "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"HelpActions"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token keyword"},"const"),t(" appMenuBar "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuBar"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"//-----------"),t("\n"),a("span",{class:"token keyword"},"const"),t(" fileMenu "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Menu"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'File'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" openCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("fileActions"),a("span",{class:"token punctuation"},"."),t("open"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" closeCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("fileActions"),a("span",{class:"token punctuation"},"."),t("close"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" saveCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("fileActions"),a("span",{class:"token punctuation"},"."),t("save"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" saveAsCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("fileActions"),a("span",{class:"token punctuation"},"."),t("saveAs"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\nfileMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'open'"),a("span",{class:"token punctuation"},","),t(" openCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nfileMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Close'"),a("span",{class:"token punctuation"},","),t(" closeCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nfileMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Save'"),a("span",{class:"token punctuation"},","),t(" saveCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nfileMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Close'"),a("span",{class:"token punctuation"},","),t(" saveAsCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("fileMenu"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token comment"},"//--------------"),t("\n"),a("span",{class:"token keyword"},"const"),t(" editMenu "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Menu"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Edit'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" cutCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("EditActions"),a("span",{class:"token punctuation"},"."),t("cut"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" copyCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("EditActions"),a("span",{class:"token punctuation"},"."),t("copy"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" pasteCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("EditActions"),a("span",{class:"token punctuation"},"."),t("paste"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" deleteCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("EditActions"),a("span",{class:"token punctuation"},"."),t("delete"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\neditMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Cut'"),a("span",{class:"token punctuation"},","),t(" cutCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\neditMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Copy'"),a("span",{class:"token punctuation"},","),t(" copyCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\neditMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Paste'"),a("span",{class:"token punctuation"},","),t(" pasteCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\neditMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Delete'"),a("span",{class:"token punctuation"},","),t(" deleteCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("editMenu"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"//------------"),t("\n"),a("span",{class:"token keyword"},"const"),t(" insertMenu "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Menu"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Insert'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" textBlockCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("InsertActions"),a("span",{class:"token punctuation"},"."),t("textBlock"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\ninsertMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Text Block'"),a("span",{class:"token punctuation"},","),t(" textBlockCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("insertMenu"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"//------------"),t("\n"),a("span",{class:"token keyword"},"const"),t(" helpMenu "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Menu"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Help'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token keyword"},"const"),t(" showHelpCommand "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuCommand"),a("span",{class:"token punctuation"},"("),t("HelpActions"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"showHelp"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nhelpMenu"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"MenuItem"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Show Help'"),a("span",{class:"token punctuation"},","),t(" showHelpCommand"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"add"),a("span",{class:"token punctuation"},"("),t("helpMenu"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\ndocument"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getElementsByTagName"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'body'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"appendChild"),a("span",{class:"token punctuation"},"("),t("appMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getElement"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\nappMenuBar"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"show"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br")])],-1),_s=a("p",null,"Command sequences to implement Macro/Batch/Undo command:",-1),Es=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"const"),t(),a("span",{class:"token function-variable function"},"Cursor"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("width"),a("span",{class:"token punctuation"},","),t(" height"),a("span",{class:"token punctuation"},","),t(" parent")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("width "),a("span",{class:"token operator"},"="),t(" width"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("height "),a("span",{class:"token operator"},"="),t(" height"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas "),a("span",{class:"token operator"},"="),t(" document"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"createElement"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'canvas'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas"),a("span",{class:"token punctuation"},"."),t("width "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("width"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas"),a("span",{class:"token punctuation"},"."),t("height "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("height"),a("span",{class:"token punctuation"},";"),t("\n parent"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"appendChild"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("canvas"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getContext"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'2d'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),t("fillStyle "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'#CCC000'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"move"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Cursor"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"move"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("x"),a("span",{class:"token punctuation"},","),t(" y")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" _this "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n _this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"lineTo"),a("span",{class:"token punctuation"},"("),t("x"),a("span",{class:"token punctuation"},","),t(" y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"lineTo"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("x"),a("span",{class:"token punctuation"},","),t(" y")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("x "),a("span",{class:"token operator"},"+="),t(" x"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("y "),a("span",{class:"token operator"},"+="),t(" y"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"lineTo"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("x"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"executeCommands"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t(" x"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("width "),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(" y"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("height "),a("span",{class:"token operator"},"/"),t(),a("span",{class:"token number"},"2"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"clearRect"),a("span",{class:"token punctuation"},"("),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("width"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("height"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"beginPath"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"moveTo"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("x"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("position"),a("span",{class:"token punctuation"},"."),t("y"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("ctx"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"stroke"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"undo"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("commandStack"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"pop"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"executeCommands"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br")])],-1),Bs=a("h3",{id:"mediator-pattern"},[a("a",{class:"header-anchor",href:"#mediator-pattern","aria-hidden":"true"},"#"),t(" Mediator Pattern")],-1),Fs=a("p",null,"一个 Mediator 对象封装对象间的协议: 中央集权的控制中心 - 所有观察者共享一个共有的被观察者(所有订阅者订阅同一个节点).",-1),Ts=a("h3",{id:"observer-pattern"},[a("a",{class:"header-anchor",href:"#observer-pattern","aria-hidden":"true"},"#"),t(" Observer Pattern")],-1),Vs=a("ul",null,[a("li",null,"被观察者(Subject)维护一组观察者列表,每当被观察者状态改变时,调用 notify 函数,此函数中调用观察者(Observer)的 update 函数(可自定义)"),a("li",null,[t("decouple subject and observer: each depends on "),a("code",null,"Abstraction"),t(" not "),a("code",null,"Implementation")])],-1),qs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"ObserverList"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Add"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"obj"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),t("obj"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Empty"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Count"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Get"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"index"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},">"),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),t(),a("span",{class:"token operator"},"&&"),t(" index "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"["),t("index"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Insert"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("obj"),a("span",{class:"token punctuation"},","),t(" index")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" pointer "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"unshift"),a("span",{class:"token punctuation"},"("),t("obj"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n pointer "),a("span",{class:"token operator"},"="),t(" index"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),t("obj"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n pointer "),a("span",{class:"token operator"},"="),t(" index"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" pointer"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"IndexOf"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("obj"),a("span",{class:"token punctuation"},","),t(" startIndex")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(" startIndex"),a("span",{class:"token punctuation"},","),t("\n pointer "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("i "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"==="),t(" obj"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n pointer "),a("span",{class:"token operator"},"="),t(" i"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" pointer"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"RemoveAt"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"index"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"shift"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("index "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},"-"),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observerList"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"pop"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// 被观察者维护一个观察者列表"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Subject"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"ObserverList"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token class-name"},"Subject"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"AddObserver"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"observer"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"Add"),a("span",{class:"token punctuation"},"("),t("observer"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Subject"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"RemoveObserver"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"observer"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"RemoveAt"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"IndexOf"),a("span",{class:"token punctuation"},"("),t("observer"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token class-name"},"Subject"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Notify"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"context"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" observerCount "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"Count"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" observerCount"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("observers"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"Get"),a("span",{class:"token punctuation"},"("),t("i"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"Update"),a("span",{class:"token punctuation"},"("),t("context"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// The Observer"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Observer"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"Update"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// ..."),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n\n"),a("span",{class:"token comment"},"// Extend an object with an extension"),t("\n"),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"extend"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("extension"),a("span",{class:"token punctuation"},","),t(" obj")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" key "),a("span",{class:"token keyword"},"in"),t(" extension"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n obj"),a("span",{class:"token punctuation"},"["),t("key"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(" extension"),a("span",{class:"token punctuation"},"["),t("key"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br"),a("span",{class:"line-number"},"63"),a("br"),a("span",{class:"line-number"},"64"),a("br"),a("span",{class:"line-number"},"65"),a("br"),a("span",{class:"line-number"},"66"),a("br"),a("span",{class:"line-number"},"67"),a("br"),a("span",{class:"line-number"},"68"),a("br"),a("span",{class:"line-number"},"69"),a("br"),a("span",{class:"line-number"},"70"),a("br"),a("span",{class:"line-number"},"71"),a("br"),a("span",{class:"line-number"},"72"),a("br"),a("span",{class:"line-number"},"73"),a("br"),a("span",{class:"line-number"},"74"),a("br"),a("span",{class:"line-number"},"75"),a("br"),a("span",{class:"line-number"},"76"),a("br"),a("span",{class:"line-number"},"77"),a("br"),a("span",{class:"line-number"},"78"),a("br"),a("span",{class:"line-number"},"79"),a("br"),a("span",{class:"line-number"},"80"),a("br"),a("span",{class:"line-number"},"81"),a("br"),a("span",{class:"line-number"},"82"),a("br"),a("span",{class:"line-number"},"83"),a("br"),a("span",{class:"line-number"},"84"),a("br"),a("span",{class:"line-number"},"85"),a("br"),a("span",{class:"line-number"},"86"),a("br"),a("span",{class:"line-number"},"87"),a("br"),a("span",{class:"line-number"},"88"),a("br"),a("span",{class:"line-number"},"89"),a("br"),a("span",{class:"line-number"},"90"),a("br"),a("span",{class:"line-number"},"91"),a("br")])],-1),Rs=a("h3",{id:"pub-sub-pattern"},[a("a",{class:"header-anchor",href:"#pub-sub-pattern","aria-hidden":"true"},"#"),t(" Pub-Sub Pattern")],-1),Us=a("ul",null,[a("li",null,"观察者模式中主体和观察者是互相感知"),a("li",null,"发布-订阅模式是借助第三方来实现调度, 发布者和订阅者之间互不感知")],-1),Hs=a("h4",{id:"implementation"},[a("a",{class:"header-anchor",href:"#implementation","aria-hidden":"true"},"#"),t(" Implementation")],-1),zs=a("ul",null,[a("li",null,"pubSub.js")],-1),Gs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[t("module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("window"),a("span",{class:"token punctuation"},","),t(" doc"),a("span",{class:"token punctuation"},","),t(" undef")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" pubSub "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"var"),t(" topics "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n subUid "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token operator"},"-"),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},";"),t("\n\n pubSub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"publish"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("topic"),a("span",{class:"token punctuation"},","),t(" args")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// undefined check"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token function"},"setTimeout"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" subscribers "),a("span",{class:"token operator"},"="),t(" topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},","),t("\n len "),a("span",{class:"token operator"},"="),t(" subscribers "),a("span",{class:"token operator"},"?"),t(" subscribers"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token operator"},":"),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"while"),t(),a("span",{class:"token punctuation"},"("),t("len"),a("span",{class:"token operator"},"--"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n subscribers"),a("span",{class:"token punctuation"},"["),t("len"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"func"),a("span",{class:"token punctuation"},"("),t("topic"),a("span",{class:"token punctuation"},","),t(" args"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token boolean"},"true"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n pubSub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"subscribe"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("topic"),a("span",{class:"token punctuation"},","),t(" func")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// undefined check"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token comment"},"// add observer to observerList (topics)"),t("\n "),a("span",{class:"token keyword"},"var"),t(" token "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"++"),t("subUid"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n topics"),a("span",{class:"token punctuation"},"["),t("topic"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"{"),t("\n token"),a("span",{class:"token operator"},":"),t(" token"),a("span",{class:"token punctuation"},","),t("\n func"),a("span",{class:"token operator"},":"),t(" func"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" token"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n pubSub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"unsubscribe"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"token"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" m "),a("span",{class:"token keyword"},"in"),t(" topics"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("topics"),a("span",{class:"token punctuation"},"["),t("m"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},","),t(" j "),a("span",{class:"token operator"},"="),t(" topics"),a("span",{class:"token punctuation"},"["),t("m"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" j"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("topics"),a("span",{class:"token punctuation"},"["),t("m"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),t("token "),a("span",{class:"token operator"},"==="),t(" token"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n topics"),a("span",{class:"token punctuation"},"["),t("m"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"splice"),a("span",{class:"token punctuation"},"("),t("i"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"return"),t(" token"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(" pubSub"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("document"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"undefined"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br")])],-1),Ws=a("ul",null,[a("li",null,"test.js")],-1),Qs=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token keyword"},"var"),t(" pubsub "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"require"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'./pubSub.js'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// add observer to observerList"),t("\n"),a("span",{class:"token keyword"},"var"),t(" testFirstSub "),a("span",{class:"token operator"},"="),t(" pubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("topic"),a("span",{class:"token punctuation"},","),t(" data")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("topic "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"': '"),t(),a("span",{class:"token operator"},"+"),t(" data"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// subject broadcast/notify, observer update"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'hello world!'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'test'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'a'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'b'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'c'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"{"),t(" color"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'blue'"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"{"),t(" text"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'hello'"),t(),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token function"},"setTimeout"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n pubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"unsubscribe"),a("span",{class:"token punctuation"},"("),t("testFirstSub"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// permanent subscribe"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'sum'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("topic"),a("span",{class:"token punctuation"},","),t(" data")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("data"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"!=="),t(),a("span",{class:"token string"},"'[object Array]'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Please input array: * '"),t(),a("span",{class:"token operator"},"+"),t(" data "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' * is not array!'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" tmp "),a("span",{class:"token operator"},"="),t(" data"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"filter"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"item"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token function"},"toString"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("item"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'[object Number]'"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("tmp"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" sum "),a("span",{class:"token operator"},"="),t(" tmp"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"reduce"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("previous"),a("span",{class:"token punctuation"},","),t(" current")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(" previous "),a("span",{class:"token operator"},"+"),t(" current"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'Sum of '"),t(),a("span",{class:"token operator"},"+"),t(" data "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' : '"),t(),a("span",{class:"token operator"},"+"),t(" sum"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n console"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"log"),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token string"},"'Please input number array: * '"),t(),a("span",{class:"token operator"},"+"),t(" data "),a("span",{class:"token operator"},"+"),t(),a("span",{class:"token string"},"' * is not number array!'"),t("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'login'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'hello again!'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'sum'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'hello again!'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'sum'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"2"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"3"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"4"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"5"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\npubsub"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'sum'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'a'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'b'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'c'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'d'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'e'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br")])],-1),Xs=a("ul",null,[a("li",null,"in jQuery")],-1),Js=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// Equivalent to subscribe(topicName, callback)"),t("\n"),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t("document"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"on"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'topicName'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"//..perform some behavior"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// Equivalent to publish(topicName)"),t("\n"),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t("document"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"trigger"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'topicName'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// Equivalent to unsubscribe(topicName)"),t("\n"),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t("document"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"off"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},"'topicName'"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br")])],-1),Ks=a("ul",null,[a("li",null,"MicroEvent.js")],-1),Ys=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"/**\n * MicroEvent - to make any js object an event emitter (server or browser)\n *\n * - pure javascript - server compatible, browser compatible\n * - don't rely on the browser doms\n * - super simple - you get it immediately, no mystery, no magic involved\n *\n * - create a MicroEventDebug with goodies to debug\n * - make it safer to use\n */"),t("\n\n"),a("span",{class:"token keyword"},"var"),t(),a("span",{class:"token function-variable function"},"MicroEvent"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token class-name"},"MicroEvent"),a("span",{class:"token punctuation"},"."),t("prototype "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"bind"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("event"),a("span",{class:"token punctuation"},","),t(" fct")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"push"),a("span",{class:"token punctuation"},"("),t("fct"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"unbind"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("event"),a("span",{class:"token punctuation"},","),t(" fct")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("event "),a("span",{class:"token keyword"},"in"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"splice"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"indexOf"),a("span",{class:"token punctuation"},"("),t("fct"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token function-variable function"},"trigger"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),t("event "),a("span",{class:"token comment"},"/* , args... */"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"||"),t(),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t("event "),a("span",{class:"token keyword"},"in"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token boolean"},"false"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_events"),a("span",{class:"token punctuation"},"["),t("event"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"apply"),a("span",{class:"token punctuation"},"("),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token class-name"},"Array"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"slice"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"call"),a("span",{class:"token punctuation"},"("),t("arguments"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token number"},"1"),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"/**\n * mixin will delegate all MicroEvent.js function in the destination object\n *\n * - require('MicroEvent').mixin(Foobar) will make Foobar able to use MicroEvent\n *\n * @param {Object} the object which will support MicroEvent\n */"),t("\nMicroEvent"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"mixin"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"destObject"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"var"),t(" props "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"["),a("span",{class:"token string"},"'bind'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'unbind'"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'trigger'"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"for"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"var"),t(" i "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token number"},"0"),a("span",{class:"token punctuation"},";"),t(" i "),a("span",{class:"token operator"},"<"),t(" props"),a("span",{class:"token punctuation"},"."),t("length"),a("span",{class:"token punctuation"},";"),t(" i"),a("span",{class:"token operator"},"++"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" destObject "),a("span",{class:"token operator"},"==="),t(),a("span",{class:"token string"},"'function'"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n destObject"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("props"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token class-name"},"MicroEvent"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("props"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t(),a("span",{class:"token keyword"},"else"),t(),a("span",{class:"token punctuation"},"{"),t("\n destObject"),a("span",{class:"token punctuation"},"["),t("props"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token class-name"},"MicroEvent"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"["),t("props"),a("span",{class:"token punctuation"},"["),t("i"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token keyword"},"return"),t(" destObject"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n"),a("span",{class:"token comment"},"// export in common js"),t("\n"),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"typeof"),t(" module "),a("span",{class:"token operator"},"!=="),t(),a("span",{class:"token string"},"'undefined'"),t(),a("span",{class:"token operator"},"&&"),t(),a("span",{class:"token string"},"'exports'"),t(),a("span",{class:"token keyword"},"in"),t(" module"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n module"),a("span",{class:"token punctuation"},"."),t("exports "),a("span",{class:"token operator"},"="),t(" MicroEvent"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br")])],-1),Zs=a("h4",{id:"sample"},[a("a",{class:"header-anchor",href:"#sample","aria-hidden":"true"},"#"),t(" Sample")],-1),na=a("h5",{id:"ajax-callback"},[a("a",{class:"header-anchor",href:"#ajax-callback","aria-hidden":"true"},"#"),t(" Ajax Callback")],-1),sa=a("ul",null,[a("li",null,"当请求返回,并且实际的数据可用的时候,会生成一个通知"),a("li",null,"如何使用这些事件(或者返回的数据),都是由订阅者自己决定的"),a("li",null,"可以有多个不同的订阅者,以不同的方式使用返回的数据"),a("li",null,"Ajax 层: 唯一的责任 - 请求和返回数据,接着将数据发送给所有想要使用数据的地方")],-1),aa=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"$"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token comment"},'// Pre-compile template and "cache" it using closure'),t("\n "),a("span",{class:"token keyword"},"var"),t(" resultTemplate "),a("span",{class:"token operator"},"="),t(" _"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"template"),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#resultTemplate"'),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"html"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// Subscribe to the new search tags topic"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"/search/tags"'),t(),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"tags"),t(),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#searchResults"'),t(),a("span",{class:"token punctuation"},")"),t("\n "),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"html"),a("span",{class:"token punctuation"},"("),t('"\n\n\n Searched '),a("span",{class:"token keyword"},"for"),a("span",{class:"token operator"},":"),a("span",{class:"token string"},'" + tags + "'),t('\n\n"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// Subscribe to the new results topic"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"/search/resultSet"'),t(),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"results"),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#searchResults"'),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"append"),a("span",{class:"token punctuation"},"("),a("span",{class:"token function"},"resultTemplate"),a("span",{class:"token punctuation"},"("),t(" results "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// Submit a search query and publish tags on the /search/tags topic"),t("\n "),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#flickrSearch"'),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"submit"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"e"),t(),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n\n e"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"preventDefault"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"var"),t(" tags "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token function"},"$"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"find"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"#query"'),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"val"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token operator"},"!"),t("tags "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"/search/tags"'),t(),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"["),t(" $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"trim"),a("span",{class:"token punctuation"},"("),t("tags"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"]"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n\n "),a("span",{class:"token comment"},"// Subscribe to new tags being published and perform"),t("\n "),a("span",{class:"token comment"},"// a search query using them. Once data has returned"),t("\n "),a("span",{class:"token comment"},"// publish this data for the rest of the application"),t("\n "),a("span",{class:"token comment"},"// to consume"),t("\n\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"subscribe"),a("span",{class:"token punctuation"},"("),a("span",{class:"token string"},'"/search/tags"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"tags"),t(),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// Ajax Request"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"getJSON"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"http://api.flickr.com/services/feeds/"'),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"{"),t("\n tags"),a("span",{class:"token operator"},":"),t(" tags"),a("span",{class:"token punctuation"},","),t("\n tagMode"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},'"any"'),a("span",{class:"token punctuation"},","),t("\n format"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},'"json"'),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n\n "),a("span",{class:"token keyword"},"function"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token parameter"},"data"),t(),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"{"),t("\n\n "),a("span",{class:"token keyword"},"if"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token operator"},"!"),t("data"),a("span",{class:"token punctuation"},"."),t("items"),a("span",{class:"token punctuation"},"."),t("length "),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"publish"),a("span",{class:"token punctuation"},"("),t(),a("span",{class:"token string"},'"/search/resultSet"'),t(),a("span",{class:"token punctuation"},","),t(" data"),a("span",{class:"token punctuation"},"."),t("items "),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br"),a("span",{class:"line-number"},"58"),a("br"),a("span",{class:"line-number"},"59"),a("br"),a("span",{class:"line-number"},"60"),a("br"),a("span",{class:"line-number"},"61"),a("br"),a("span",{class:"line-number"},"62"),a("br")])],-1),ta=a("h2",{id:"mvc-pattern"},[a("a",{class:"header-anchor",href:"#mvc-pattern","aria-hidden":"true"},"#"),t(" MVC Pattern")],-1),ea=a("p",null,"在 MVC 中,视图位于我们架构的顶部,其背后是控制器. 模型在控制器后面,而因此我们的视图了解得到我们的控制器,而控制器了解得到模型. 这里,我们的视图有对模型的直接访问. 然而将整个模型完全暴露给视图可能会有安全和性能损失, 这取决于我们应用程序的复杂性. MVVM 则尝试去避免这些问题.",-1),oa=a("p",null,"在 MVP 中,控制器的角色被代理器所取代,代理器和视图处于同样的地位, 视图和模型的事件都被它侦听着并且接受它的调解. 不同于 MVVM,没有一个将视图绑定到视图模型的机制,因此我们转而依赖于每一个视图都实现一个允许代理器同视图去交互的接口.",-1),ca=a("p",null,"MVVM 进一步允许我们创建一个模型的特定视图子集,包含了状态和逻辑信息, 避免了将模型完全暴露给视图的必要。 不同于 MVP 的代理器,视图模型并不需要去引用一个视图。 视图可以绑定到视图模型的属性上面,视图模型则去将包含在模型中的数据暴露给视图。 像我们所提到过的,对视图的抽象意味着其背后的代码需要较少的逻辑。",-1),pa=a("h2",{id:"jquery-pattern"},[a("a",{class:"header-anchor",href:"#jquery-pattern","aria-hidden":"true"},"#"),t(" jQuery Pattern")],-1),la=a("h3",{id:"plugin-pattern"},[a("a",{class:"header-anchor",href:"#plugin-pattern","aria-hidden":"true"},"#"),t(" Plugin Pattern")],-1),ua=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"$"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"extend"),a("span",{class:"token punctuation"},"("),t("$"),a("span",{class:"token punctuation"},"."),t("fn"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token function-variable function"},"myPlugin"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// your plugin logic"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),t("jQuery"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br")])],-1),ia=a("div",{class:"language-js line-numbers-mode"},[a("pre",null,[a("code",null,[a("span",{class:"token comment"},"// the semi-colon before the function invocation is a safety"),t("\n"),a("span",{class:"token comment"},"// net against concatenated scripts and/or other plugins"),t("\n"),a("span",{class:"token comment"},"// that are not closed properly."),t("\n"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("$"),a("span",{class:"token punctuation"},","),t(" window"),a("span",{class:"token punctuation"},","),t(" document"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"undefined")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// undefined is used here as the undefined global"),t("\n "),a("span",{class:"token comment"},"// variable in ECMAScript 3 and is mutable (i.e. it can"),t("\n "),a("span",{class:"token comment"},"// be changed by someone else). undefined isn't really"),t("\n "),a("span",{class:"token comment"},"// being passed in so we can ensure that its value is"),t("\n "),a("span",{class:"token comment"},"// truly undefined. In ES5, undefined can no longer be"),t("\n "),a("span",{class:"token comment"},"// modified."),t("\n\n "),a("span",{class:"token comment"},"// window and document are passed through as local"),t("\n "),a("span",{class:"token comment"},"// variables rather than as globals, because this (slightly)"),t("\n "),a("span",{class:"token comment"},"// quickens the resolution process and can be more"),t("\n "),a("span",{class:"token comment"},"// efficiently minified (especially when both are"),t("\n "),a("span",{class:"token comment"},"// regularly referenced in our plugin)."),t("\n\n "),a("span",{class:"token comment"},"// Create the defaults once"),t("\n "),a("span",{class:"token keyword"},"var"),t(" pluginName "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token string"},"'defaultPluginName'"),a("span",{class:"token punctuation"},","),t("\n defaults "),a("span",{class:"token operator"},"="),t(),a("span",{class:"token punctuation"},"{"),t("\n propertyName"),a("span",{class:"token operator"},":"),t(),a("span",{class:"token string"},"'value'"),a("span",{class:"token punctuation"},","),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// The actual plugin constructor"),t("\n "),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token function"},"Plugin"),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},[t("element"),a("span",{class:"token punctuation"},","),t(" options")]),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("element "),a("span",{class:"token operator"},"="),t(" element"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// jQuery has an extend method that merges the"),t("\n "),a("span",{class:"token comment"},"// contents of two or more objects, storing the"),t("\n "),a("span",{class:"token comment"},"// result in the first object. The first object"),t("\n "),a("span",{class:"token comment"},"// is generally empty because we don't want to alter"),t("\n "),a("span",{class:"token comment"},"// the default options for future instances of the plugin"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("options "),a("span",{class:"token operator"},"="),t(" $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"extend"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},"{"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},","),t(" defaults"),a("span",{class:"token punctuation"},","),t(" options"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_defaults "),a("span",{class:"token operator"},"="),t(" defaults"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),t("_name "),a("span",{class:"token operator"},"="),t(" pluginName"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"init"),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n\n "),a("span",{class:"token class-name"},"Plugin"),a("span",{class:"token punctuation"},"."),t("prototype"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function-variable function"},"init"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token comment"},"// Place initialization logic here"),t("\n "),a("span",{class:"token comment"},"// We already have access to the DOM element and"),t("\n "),a("span",{class:"token comment"},"// the options via the instance, e.g. this.element"),t("\n "),a("span",{class:"token comment"},"// and this.options"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n\n "),a("span",{class:"token comment"},"// A really lightweight plugin wrapper around the constructor,"),t("\n "),a("span",{class:"token comment"},"// preventing against multiple instantiations"),t("\n $"),a("span",{class:"token punctuation"},"."),t("fn"),a("span",{class:"token punctuation"},"["),t("pluginName"),a("span",{class:"token punctuation"},"]"),t(),a("span",{class:"token operator"},"="),t(),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token parameter"},"options"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"return"),t(),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"each"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"function"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n "),a("span",{class:"token keyword"},"if"),t(),a("span",{class:"token punctuation"},"("),a("span",{class:"token operator"},"!"),t("$"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'plugin_'"),t(),a("span",{class:"token operator"},"+"),t(" pluginName"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),t(),a("span",{class:"token punctuation"},"{"),t("\n $"),a("span",{class:"token punctuation"},"."),a("span",{class:"token function"},"data"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token string"},"'plugin_'"),t(),a("span",{class:"token operator"},"+"),t(" pluginName"),a("span",{class:"token punctuation"},","),t(),a("span",{class:"token keyword"},"new"),t(),a("span",{class:"token class-name"},"Plugin"),a("span",{class:"token punctuation"},"("),a("span",{class:"token keyword"},"this"),a("span",{class:"token punctuation"},","),t(" options"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n "),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},";"),t("\n"),a("span",{class:"token punctuation"},"}"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},"("),t("jQuery"),a("span",{class:"token punctuation"},","),t(" window"),a("span",{class:"token punctuation"},","),t(" document"),a("span",{class:"token punctuation"},")"),a("span",{class:"token punctuation"},";"),t("\n")])]),a("div",{class:"line-numbers-wrapper"},[a("span",{class:"line-number"},"1"),a("br"),a("span",{class:"line-number"},"2"),a("br"),a("span",{class:"line-number"},"3"),a("br"),a("span",{class:"line-number"},"4"),a("br"),a("span",{class:"line-number"},"5"),a("br"),a("span",{class:"line-number"},"6"),a("br"),a("span",{class:"line-number"},"7"),a("br"),a("span",{class:"line-number"},"8"),a("br"),a("span",{class:"line-number"},"9"),a("br"),a("span",{class:"line-number"},"10"),a("br"),a("span",{class:"line-number"},"11"),a("br"),a("span",{class:"line-number"},"12"),a("br"),a("span",{class:"line-number"},"13"),a("br"),a("span",{class:"line-number"},"14"),a("br"),a("span",{class:"line-number"},"15"),a("br"),a("span",{class:"line-number"},"16"),a("br"),a("span",{class:"line-number"},"17"),a("br"),a("span",{class:"line-number"},"18"),a("br"),a("span",{class:"line-number"},"19"),a("br"),a("span",{class:"line-number"},"20"),a("br"),a("span",{class:"line-number"},"21"),a("br"),a("span",{class:"line-number"},"22"),a("br"),a("span",{class:"line-number"},"23"),a("br"),a("span",{class:"line-number"},"24"),a("br"),a("span",{class:"line-number"},"25"),a("br"),a("span",{class:"line-number"},"26"),a("br"),a("span",{class:"line-number"},"27"),a("br"),a("span",{class:"line-number"},"28"),a("br"),a("span",{class:"line-number"},"29"),a("br"),a("span",{class:"line-number"},"30"),a("br"),a("span",{class:"line-number"},"31"),a("br"),a("span",{class:"line-number"},"32"),a("br"),a("span",{class:"line-number"},"33"),a("br"),a("span",{class:"line-number"},"34"),a("br"),a("span",{class:"line-number"},"35"),a("br"),a("span",{class:"line-number"},"36"),a("br"),a("span",{class:"line-number"},"37"),a("br"),a("span",{class:"line-number"},"38"),a("br"),a("span",{class:"line-number"},"39"),a("br"),a("span",{class:"line-number"},"40"),a("br"),a("span",{class:"line-number"},"41"),a("br"),a("span",{class:"line-number"},"42"),a("br"),a("span",{class:"line-number"},"43"),a("br"),a("span",{class:"line-number"},"44"),a("br"),a("span",{class:"line-number"},"45"),a("br"),a("span",{class:"line-number"},"46"),a("br"),a("span",{class:"line-number"},"47"),a("br"),a("span",{class:"line-number"},"48"),a("br"),a("span",{class:"line-number"},"49"),a("br"),a("span",{class:"line-number"},"50"),a("br"),a("span",{class:"line-number"},"51"),a("br"),a("span",{class:"line-number"},"52"),a("br"),a("span",{class:"line-number"},"53"),a("br"),a("span",{class:"line-number"},"54"),a("br"),a("span",{class:"line-number"},"55"),a("br"),a("span",{class:"line-number"},"56"),a("br"),a("span",{class:"line-number"},"57"),a("br")])],-1),ra=a("h2",{id:"scalability-design"},[a("a",{class:"header-anchor",href:"#scalability-design","aria-hidden":"true"},"#"),t(" Scalability Design")],-1),ka=a("ul",null,[a("li",null,"Prefer composites over mixins."),a("li",null,"Always clone objects between components."),a("li",null,"Use namespaced state store modules."),a("li",null,"Write robust tests."),a("li",null,"Interact with REST API via services/SDK."),a("li",null,[t("Wrap third-party libraries other using them directly: "),a("ul",null,[a("li",null,"Abstract: changing dependencies without changing interface."),a("li",null,"Extendability: More obvious route to extending functionality.")])])],-1),ba=a("h2",{id:"domain-driven-design"},[a("a",{class:"header-anchor",href:"#domain-driven-design","aria-hidden":"true"},"#"),t(" Domain Driven Design")],-1),ma=a("h3",{id:"interface-layer"},[a("a",{class:"header-anchor",href:"#interface-layer","aria-hidden":"true"},"#"),t(" Interface Layer")],-1),da=a("p",null,"主要负责与外部系统进行交互与通信:",-1),ya=a("ul",null,[a("li",null,"做参数的基本处理, 比如入参校验, 回参 DTO 转换 (拆包, 组包)"),a("li",null,"Dubbo Services"),a("li",null,"RESTful API")],-1),fa=a("h3",{id:"application-layer"},[a("a",{class:"header-anchor",href:"#application-layer","aria-hidden":"true"},"#"),t(" Application Layer")],-1),ha=a("p",null,"Application Service 层只是很薄的一层, 它内部并不实现任何逻辑, 只是负责协调和转发 (流程编排), 委派业务动作给更下层的领域层.",-1),wa=a("h3",{id:"domain-layer"},[a("a",{class:"header-anchor",href:"#domain-layer","aria-hidden":"true"},"#"),t(" Domain Layer")],-1),ga=a("p",null,"Domain 层是领域模型系统的核心, 负责维护面向对象的领域模型, 几乎全部的业务逻辑都会在这一层实现. 内部主要包含 Entity, ValueObject, Domain Event, Repository.",-1),va=a("h3",{id:"infrastructure-layer"},[a("a",{class:"header-anchor",href:"#infrastructure-layer","aria-hidden":"true"},"#"),t(" Infrastructure Layer")],-1),Pa=a("p",null,"主要为 Interface, Application 和 Domain 三层提供支撑:",-1),Ca=a("ul",null,[a("li",null,"封装基础资源服务, 通过依赖注入方式解耦."),a("li",null,"Third-party tools, Message Queue, File, Cache, Database, Search etc."),a("li",null,"实现仓储接口 DB, 通常真正读写 DB.")],-1),Sa=a("h3",{id:"ddd-layout"},[a("a",{class:"header-anchor",href:"#ddd-layout","aria-hidden":"true"},"#"),t(" DDD Layout")],-1),ja=a("ul",null,[a("li",null,[a("a",{href:"https://github.com/lupguo/ddd-layout",target:"_blank",rel:"noopener noreferrer"},"DDD Layout in Golang")])],-1),Ma=a("h2",{id:"高并发系统设计"},[a("a",{class:"header-anchor",href:"#高并发系统设计","aria-hidden":"true"},"#"),t(" 高并发系统设计")],-1),xa=a("h3",{id:"concurrent-code-layer"},[a("a",{class:"header-anchor",href:"#concurrent-code-layer","aria-hidden":"true"},"#"),t(" Concurrent Code Layer")],-1),Da=a("ul",null,[a("li",null,"Mutex Performance"),a("li",null,"Database Caches"),a("li",null,"Update Merge"),a("li",null,"BloomFilter"),a("li",null,"Asynchronous"),a("li",null,"Multi-Thread")],-1),Aa=a("h3",{id:"concurrent-database-layer"},[a("a",{class:"header-anchor",href:"#concurrent-database-layer","aria-hidden":"true"},"#"),t(" Concurrent DataBase Layer")],-1),Ia=a("ul",null,[a("li",null,"DataBase Type: RDBMS -> NoSQL -> NewSQL"),a("li",null,"Table Structure Design"),a("li",null,"Index Design"),a("li",null,"Split Table"),a("li",null,"Read and Write Separation"),a("li",null,"Data Slice and Data Partition"),a("li",null,"Hot Data Cache")],-1),Na=a("h3",{id:"concurrent-architecture-layer"},[a("a",{class:"header-anchor",href:"#concurrent-architecture-layer","aria-hidden":"true"},"#"),t(" Concurrent Architecture Layer")],-1),Oa=a("ul",null,[a("li",null,"Microservices"),a("li",null,"Scale Friendly"),a("li",null,"FailFast"),a("li",null,"Data PreFetch"),a("li",null,"Multi-Level Caches")],-1),La=a("h2",{id:"高可用系统设计"},[a("a",{class:"header-anchor",href:"#高可用系统设计","aria-hidden":"true"},"#"),t(" 高可用系统设计")],-1),$a=a("h3",{id:"resource-isolation"},[a("a",{class:"header-anchor",href:"#resource-isolation","aria-hidden":"true"},"#"),t(" Resource Isolation")],-1),_a=a("h3",{id:"load-balance-design"},[a("a",{class:"header-anchor",href:"#load-balance-design","aria-hidden":"true"},"#"),t(" Load Balance Design")],-1),Ea=a("ul",null,[a("li",null,"Hardware Load Balance"),a("li",null,"Software Load Balance"),a("li",null,"Load Balance Algorithms: Random, RoundRobin, WeightRoundRobin, ConsistentHash"),a("li",null,"Error Machines Auto Detection"),a("li",null,"Error Services Auto Retirement"),a("li",null,"Services Retry Automation"),a("li",null,"Recovery Services Auto Detection")],-1),Ba=a("h3",{id:"idempotence-design"},[a("a",{class:"header-anchor",href:"#idempotence-design","aria-hidden":"true"},"#"),t(" Idempotence Design")],-1),Fa=a("p",null,"在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同.",-1),Ta=a("h4",{id:"write-idempotence-design"},[a("a",{class:"header-anchor",href:"#write-idempotence-design","aria-hidden":"true"},"#"),t(" Write Idempotence Design")],-1),Va=a("ul",null,[a("li",null,"Mutex"),a("li",null,"Key Index"),a("li",null,"Token"),a("li",null,"Data Version"),a("li",null,"State Machine")],-1),qa=a("h3",{id:"cap-theory"},[a("a",{class:"header-anchor",href:"#cap-theory","aria-hidden":"true"},"#"),t(" CAP Theory")],-1),Ra=a("p",null,"A distributed system to simultaneously provide more than two out of the following three guarantees:",-1),Ua=a("ul",null,[a("li",null,"Consistency: Every read receives the most recent write or an error."),a("li",null,"Availability: Every request receives a (non-error) response, without the guarantee that it contains the most recent write."),a("li",null,"Partition tolerance: The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.")],-1),Ha=a("h3",{id:"服务熔断"},[a("a",{class:"header-anchor",href:"#服务熔断","aria-hidden":"true"},"#"),t(" 服务熔断")],-1),za=a("h3",{id:"服务限流"},[a("a",{class:"header-anchor",href:"#服务限流","aria-hidden":"true"},"#"),t(" 服务限流")],-1),Ga=a("h4",{id:"服务限流算法"},[a("a",{class:"header-anchor",href:"#服务限流算法","aria-hidden":"true"},"#"),t(" 服务限流算法")],-1),Wa=a("p",null,"一定程度上可以参考计算机网络拥塞控制算法:",-1),Qa=a("ul",null,[a("li",null,"计数器固定窗口限流: 单位时间内达到阈值后开始限流, 单位时间后重新计数. 窗口临界处流量过大, 导致服务不可用."),a("li",null,"滑动窗口限流: 在固定窗口限流基础上, 将窗口向右滑动."),a("li",null,"漏斗限流: 未满前可进入, 满则拒绝. 可以平滑流量, 无法解决突发流量."),a("li",null,"令牌桶限流: 在漏斗限流基础上, 以恒定速率产生令牌. 拥有令牌可进入, 无则拒绝. 可以平滑流量, 可以容忍突发流量.")],-1),Xa=a("h4",{id:"服务限流策略"},[a("a",{class:"header-anchor",href:"#服务限流策略","aria-hidden":"true"},"#"),t(" 服务限流策略")],-1),Ja=a("ul",null,[a("li",null,"服务拒绝"),a("li",null,"延时处理"),a("li",null,"请求分级"),a("li",null,"监控预警"),a("li",null,"动态限流"),a("li",null,"动态扩容")],-1),Ka=a("h4",{id:"服务限流位置"},[a("a",{class:"header-anchor",href:"#服务限流位置","aria-hidden":"true"},"#"),t(" 服务限流位置")],-1),Ya=a("ul",null,[a("li",null,"接入层限流: 通过 Nginx/API Router 对 DNS/IP 限流."),a("li",null,"应用限流: 每个服务拥有自己的集群限流服务."),a("li",null,"基础服务限流: 对消息队列/数据库限流.")],-1),Za=a("h3",{id:"服务降级"},[a("a",{class:"header-anchor",href:"#服务降级","aria-hidden":"true"},"#"),t(" 服务降级")],-1),nt=a("h2",{id:"reference"},[a("a",{class:"header-anchor",href:"#reference","aria-hidden":"true"},"#"),t(" Reference")],-1),st=a("ul",null,[a("li",null,[a("a",{href:"http://www.dofactory.com/javascript/design-patterns",target:"_blank",rel:"noopener noreferrer"},"JavaScript Design Patterns")])],-1);o.render=function(a,t,e,o,at,tt){return n(),s("div",null,[c,p,l,u,i,r,k,b,m,d,y,f,h,w,g,v,P,C,S,j,M,x,D,A,I,N,O,L,$,_,E,B,F,T,V,q,R,U,H,z,G,W,Q,X,J,K,Y,Z,nn,sn,an,tn,en,on,cn,pn,ln,un,rn,kn,bn,mn,dn,yn,fn,hn,wn,gn,vn,Pn,Cn,Sn,jn,Mn,xn,Dn,An,In,Nn,On,Ln,$n,_n,En,Bn,Fn,Tn,Vn,qn,Rn,Un,Hn,zn,Gn,Wn,Qn,Xn,Jn,Kn,Yn,Zn,ns,ss,as,ts,es,os,cs,ps,ls,us,is,rs,ks,bs,ms,ds,ys,fs,hs,ws,gs,vs,Ps,Cs,Ss,js,Ms,xs,Ds,As,Is,Ns,Os,Ls,$s,_s,Es,Bs,Fs,Ts,Vs,qs,Rs,Us,Hs,zs,Gs,Ws,Qs,Xs,Js,Ks,Ys,Zs,na,sa,aa,ta,ea,oa,ca,pa,la,ua,ia,ra,ka,ba,ma,da,ya,fa,ha,wa,ga,va,Pa,Ca,Sa,ja,Ma,xa,Da,Aa,Ia,Na,Oa,La,$a,_a,Ea,Ba,Fa,Ta,Va,qa,Ra,Ua,Ha,za,Ga,Wa,Qa,Xa,Ja,Ka,Ya,Za,nt,st])};export default o;export{e as __pageData}; diff --git a/assets/programming_devops_softwareTestingBasicNotes.md.a3d46251.js b/assets/programming_devops_softwareTestingBasicNotes.md.a3d46251.js new file mode 100644 index 00000000000..081fc4069de --- /dev/null +++ b/assets/programming_devops_softwareTestingBasicNotes.md.a3d46251.js @@ -0,0 +1 @@ +import{o as l,c as i,a as e}from"./app.6dd2a1b7.js";const a='{"title":"Software Testing Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"测试模型","slug":"测试模型"},{"level":3,"title":"PIE 模型","slug":"pie-模型"},{"level":3,"title":"启发式测试策略模型","slug":"启发式测试策略模型"},{"level":3,"title":"用户体验测试","slug":"用户体验测试"},{"level":2,"title":"测试规模","slug":"测试规模"},{"level":2,"title":"基础概念","slug":"基础概念"},{"level":2,"title":"测试方法","slug":"测试方法"},{"level":3,"title":"图结构覆盖方法","slug":"图结构覆盖方法"},{"level":3,"title":"数据流覆盖方法","slug":"数据流覆盖方法"},{"level":3,"title":"逻辑覆盖方法","slug":"逻辑覆盖方法"},{"level":3,"title":"随机测试方法","slug":"随机测试方法"},{"level":3,"title":"黑盒测试方法","slug":"黑盒测试方法"},{"level":3,"title":"Fault Location","slug":"fault-location"},{"level":3,"title":"模糊测试","slug":"模糊测试"},{"level":2,"title":"移动设备","slug":"移动设备"},{"level":2,"title":"Useful Tools","slug":"useful-tools"},{"level":2,"title":"Bug List","slug":"bug-list"},{"level":3,"title":"Basic Bug","slug":"basic-bug"},{"level":3,"title":"C Bug","slug":"c-bug"},{"level":3,"title":"偶发性 Bug","slug":"偶发性-bug"}],"relativePath":"programming/devops/softwareTestingBasicNotes.md","lastUpdated":1627196177000}',t={},r=e('

Software Testing Basic Notes

测试模型

PIE 模型

(Execution)Fault,(Infection)Error,(Propagation)Failure

启发式测试策略模型

Heuristic Test Strategy Model - 软件功能测试 (难以实现完全自动化):

  • 关注价值(Value):用户得到价值
  • 风险驱动(Risk):降低价值/用户体验的风险项
  • 产品元素(Product Elements)
  • 结构(Structure):产品物理元素(如代码、硬件、配置文件)
  • 功能(Function):产品功能
  • 数据(Data):产品所操作的数据(如输入、输出)
  • 接口(Interface):产品所使用/暴露出的接口
  • 平台(Platform):产品所依赖的外部元素(如操作系统、输入/输出设备)
  • 操作(Operation):产品被使用的方式(如键盘、鼠标、触摸等命令操作)
  • 时间(Time):影响产品的时间因素
  • 组合元素:测试产品功能间协作

用户体验测试

对软件功能测试的有益补充:

  • 功能性体验
  • 易用性体验
  • 性能体验
  • 可靠性体验(如软件兼容性)

测试规模

Unit/Module/Integration/System Testing

基础概念

测试路径:起始顶点至终止顶点

测试方法

图结构覆盖方法

  • 顶点覆盖,边覆盖,边对覆盖(三顶点,两邻边)
  • VC/EC/EPC 法

数据流覆盖方法

  • 数据流覆盖:定义处覆盖,使用处覆盖
  • DU 法(Data&Use)

逻辑覆盖方法

  • 逻辑覆盖:条件处覆盖,判定处覆盖
  • DC(Decision),CC(Condition)法
  • MC/DC 法
  • MCC 法(完全覆盖)

随机测试方法

  • ART(随机测试):每个用例间"距离"尽可能远

黑盒测试方法

  • 等价类划分:合法输入(软件功能),非法输入(异常处理)
  • 等价类的边界值分析:合法 MIN,MIN+,MAX-,MAX 非法:MIN-,MAX+
  • 决策表+组合测试:简化决策表(考虑输入相关性)
    • 组合用例:维度与测试准度成正比
    • 约束用例:需避开约束输入(输入相关性,同时输入会成为无效输入)
    • 测试用例约简+测试用例优先级:额外贪心算法进行化简+排序测试用例求得近似解

Fault Location

  • 代码可疑度= (失败测试用例经过数/失败测试用例总数)/ (成功测试用例经过数/成功测试用例总数+失败测试用例经过数/失败测试用例总数)

即在执行过目标代码的测试用例中失败测试用例占比

模糊测试

Fuzzing - 是一种通过向目标系统提供非预期的输入并监视异常结果来发现软件漏洞的方法

移动设备

  • 机型碎片化
  • 屏幕碎片化
  • 环境碎片化
  • 耗电量

Useful Tools

  • JUnit
  • SeleniumIDE
  • PICT
  • GuiTar
  • Randoop
  • 压力测试(Apache Jmeter)
  • 移动测试(Monkey:Random Testing)

Bug List

Basic Bug

  • 必须进行输入验证 - 永远不要相信用户输入
  • 永不使用未经验证的数值的长度或大小
  • 必须返回正确的错误状态
  • 注意(隐式)类型转换

C Bug

  • 栈缓冲区溢出
  • 空指针解引用
  • (隐式)类型转换
  • GOT 覆写(Global Offset Table)

偶发性 Bug

  • 多进程完全异步编程的复杂性
  • 逐渐地内存泄漏
',42);t.render=function(e,a,t,h,s,u){return l(),i("div",null,[r])};export default t;export{a as __pageData}; diff --git a/assets/programming_devops_softwareTestingBasicNotes.md.a3d46251.lean.js b/assets/programming_devops_softwareTestingBasicNotes.md.a3d46251.lean.js new file mode 100644 index 00000000000..ecb2b482643 --- /dev/null +++ b/assets/programming_devops_softwareTestingBasicNotes.md.a3d46251.lean.js @@ -0,0 +1 @@ +import{o as l,c as i,a as e}from"./app.6dd2a1b7.js";const a='{"title":"Software Testing Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"测试模型","slug":"测试模型"},{"level":3,"title":"PIE 模型","slug":"pie-模型"},{"level":3,"title":"启发式测试策略模型","slug":"启发式测试策略模型"},{"level":3,"title":"用户体验测试","slug":"用户体验测试"},{"level":2,"title":"测试规模","slug":"测试规模"},{"level":2,"title":"基础概念","slug":"基础概念"},{"level":2,"title":"测试方法","slug":"测试方法"},{"level":3,"title":"图结构覆盖方法","slug":"图结构覆盖方法"},{"level":3,"title":"数据流覆盖方法","slug":"数据流覆盖方法"},{"level":3,"title":"逻辑覆盖方法","slug":"逻辑覆盖方法"},{"level":3,"title":"随机测试方法","slug":"随机测试方法"},{"level":3,"title":"黑盒测试方法","slug":"黑盒测试方法"},{"level":3,"title":"Fault Location","slug":"fault-location"},{"level":3,"title":"模糊测试","slug":"模糊测试"},{"level":2,"title":"移动设备","slug":"移动设备"},{"level":2,"title":"Useful Tools","slug":"useful-tools"},{"level":2,"title":"Bug List","slug":"bug-list"},{"level":3,"title":"Basic Bug","slug":"basic-bug"},{"level":3,"title":"C Bug","slug":"c-bug"},{"level":3,"title":"偶发性 Bug","slug":"偶发性-bug"}],"relativePath":"programming/devops/softwareTestingBasicNotes.md","lastUpdated":1627196177000}',t={},r=e('',42);t.render=function(e,a,t,h,s,u){return l(),i("div",null,[r])};export default t;export{a as __pageData}; diff --git a/assets/programming_functionalProgramming_functionalProgrammingBasicNotes.md.140216fc.js b/assets/programming_functionalProgramming_functionalProgrammingBasicNotes.md.140216fc.js new file mode 100644 index 00000000000..68704f1b2c7 --- /dev/null +++ b/assets/programming_functionalProgramming_functionalProgrammingBasicNotes.md.140216fc.js @@ -0,0 +1 @@ +import{o as a,c as s,a as n}from"./app.6dd2a1b7.js";const e='{"title":"Functional Programming Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Lambda Calculus","slug":"lambda-calculus"},{"level":3,"title":"Lambda Expression (Lambda-Term)","slug":"lambda-expression-lambda-term"},{"level":3,"title":"Lambda Reduction","slug":"lambda-reduction"},{"level":3,"title":"Church Numerals","slug":"church-numerals"},{"level":2,"title":"Definition for Functional Programming","slug":"definition-for-functional-programming"},{"level":2,"title":"Datatype","slug":"datatype"},{"level":3,"title":"Datatype Binding","slug":"datatype-binding"},{"level":3,"title":"Built-in Tagged Constructor","slug":"built-in-tagged-constructor"},{"level":3,"title":"Type Constructor","slug":"type-constructor"},{"level":2,"title":"Pattern Matching","slug":"pattern-matching"},{"level":2,"title":"Tail Position, Tail expression, Tail Call and Tail Recursion","slug":"tail-position-tail-expression-tail-call-and-tail-recursion"},{"level":2,"title":"Rules for expressions","slug":"rules-for-expressions"},{"level":3,"title":"samples","slug":"samples"},{"level":2,"title":"Standard ML","slug":"standard-ml"},{"level":3,"title":"functions","slug":"functions"},{"level":3,"title":"tuples","slug":"tuples"},{"level":3,"title":"lists","slug":"lists"},{"level":3,"title":"let expressions","slug":"let-expressions"},{"level":3,"title":"options","slug":"options"},{"level":3,"title":"boolean operations","slug":"boolean-operations"},{"level":3,"title":"closure","slug":"closure"},{"level":2,"title":"Type inference","slug":"type-inference"}],"relativePath":"programming/functionalProgramming/functionalProgrammingBasicNotes.md","lastUpdated":1627196177000}',p={},l=n('

Functional Programming Basic Notes

Lambda Calculus

Lambda Expression (Lambda-Term)

  • Variable: x
  • Abstraction: λx.M
  • Application: M N

e.g. λx.y λx.(λy.xy)

  • 变量 x 本身就是一个有效的 lambda 项
  • 如果 t 是一个 lambda 项,而 x 是一个变量,则 λx.t 是一个 lambda 项(称为 lambda 抽象)
  • 如果 t 和 s 是 lambda 项,那么 (ts) 是一个 lambda 项(称为应用)

Lambda Reduction

α 转换

α: λx.x ≡ λy.y 等价变量替换

β 归约

β: ((λV.E) E′) ≡ E[V := E′] 函数抽象应用(apply)于参数的过程

η 归约

λx.M x ≡ M 用于清除 lambda 表达式中存在的冗余函数抽象

Church Numerals

按照皮亚诺公理可得自然数集合表示为 {0, S(0), S(S(0)), ...}, 于是得到如下定义:

S ≡ λn.λf.λx.f (n f x)\n\n0 ≡ λf.λx.x\n1 ≡ λf.λx.f x\n2 ≡ λf.λx.f (f x)\n3 ≡ λf.λx.f (f (f x))\n...\n
1
2
3
4
5
6
7

对后继函数 S 和丘奇数的简单验证如下:

S 0\n≡ (λn.λf.λx.f (n f x)) λf.λx.x\n= (λn.λg.λy.g (n g y)) λf.λx.x    // alpha\n= (λf.λx.f (n f x))[n := λf.λx.x] // beta\n= λg.λy.g ((λf.λx.x) g y)         // substitute\n= λg.λy.g (x[f := g, x := y])     // beta\n= λg.λy.g y                       // substitute\n= λf.λx.f x                       // alpha\n≡ 1\n
1
2
3
4
5
6
7
8
9

Definition for Functional Programming

  • avoid mutation
  • first class functions
  • recursive data structures and recursive functions
  • laziness

Datatype

Datatype Binding

tagged union, every constructor name as tag, fields for different constructors can't exist at the same time

Built-in Tagged Constructor

  • NONE
  • SOME i
  • []
  • x :: xs (infix constructor)
  • ()

Type Constructor

type constructor: datatype bindings with variables

datatype 'a myList = EMPTY | CONS of 'a * 'a myList\nmyList isn't a type, int list is a type\n
1
2
  • 'a , 'a equivalent/different
  • 'a, 'b different
  • ''a, ''a equivalent

Pattern Matching

  • null/isSome check tag part(variant)
  • hd/tl/valOf check data part (extract data)
case e of\n      p1 => e1\n    | pn => en\n\nval p = e (* declare multiple variables once time in p(pattern) *)\n\n(* declare multiple callee arguments(hidden to caller) once time in p(pattern) *)\nfun foo p = e\n
1
2
3
4
5
6
7
8

In SML, all functions only take 1 argument, a tuple/record:

fun f (x, y, z) = x + y + z seems that takes 3 arguments, but truly owing to pattern matching only takes 1 tuple argument Likewise, fun f () = 0 takes 1 empty tuple argument.

Further more, tuples is syntactic sugar for records.

As a whole: all functions only take 1 record argument owing to pattern matching.

Tail Position, Tail expression, Tail Call and Tail Recursion

recursive definition for Tail Position:

  • if E isn't in tail position, then sub expressions of E aren't in tail position
  • if E is in tail position, then some sub expressions of E are in tail position
if eb then e1 else e2\n
1

is in tail position, then e1 and e2 are in tail position, not eb

f (x, e)\n
1

is in tail position, then f is in tail position(tail call), not x and e

fun factorial n =\n    let\n        fun aux(n, acc) =\n            if\n                n = 0\n            then\n                acc\n            else\n                aux (n-1, n*acc)\n    in\n        aux (n,1)\n    end\n
1
2
3
4
5
6
7
8
9
10
11
12

Rules for expressions

  • Syntactic: syntax rules
  • Semantic: type checking rules
  • Runtime: evaluation rules

samples

syntax: if e1 then e2 else e3\ntype: e1 = bool,  e2 = e3 = any\nevaluation: e1 ? e2 : e3\n
1
2
3

Standard ML

functions

syntax: fun name (arg1: type1, .., argN: typeN) = body\ntype: name = type1 * ... * typeN -> body_type\nlazy evaluation\n
1
2
3

tuples

(* tuples *)\nsyntax: e = (e1, ..., en)\ntype: e1 * ... * en (can become fun's arguments list)\nevaluation: #1 e, #2 e, ..., #n e\n
1
2
3
4

lists

(* lists *)\nsyntax: l = [e1, ..., en]\ntype: [] = elem_type list; hd(head) l = elem_type, tl(tail) x = elem_type list\nevaluation: cons = e :: l; null [] = false;\n\n> 6 :: [1, 3, 5]\n
1
2
3
4
5
6

let expressions

syntax: let\n            b1 b2 ... bn\n        in\n            body\n        end\ntype: whole let type = body_type\nevaluation: whole let result = body_result\n
1
2
3
4
5
6
7

options

  • NONE : type = 'a option
  • SOME e: type = e_type option
  • isSome: type = 'a option -> bool
  • valOf : type = 'a option -> 'a

boolean operations

  • e1 andalso e2: keyword
  • e1 orelse e2 : keyword
  • not e1 : bool -> bool
  • =(equal) <>(not equal) > < >= <=: require two same type elem

closure

lexical scope vs dynamic scope

  • lexical scope: function where defined
  • dynamic scope: function where called

compose and pipeline

fun sqrt_of_abs = Math.sqrt o Real.fromInt o abs\n\ninfix !>\nfun x !> f = f x\n\nfun sqrt_of_abs i = i !> abs !> Real.fromInt !> Math.sqrt\n
1
2
3
4
5
6

curry and unCurry

fun carry f x y = f (x, y)\nfun unCarry f (x, y) = f x y\n\nfun range (i, j) = if i > j then [] else i :: range(i+1, j)\nfun countUp = curry range 1\n\nval arr = countUp 7 (* maps to [1, 2, ..., 7] *)\n
1
2
3
4
5
6
7

Type inference

',70);p.render=function(n,e,p,t,o,r){return a(),s("div",null,[l])};export default p;export{e as __pageData}; diff --git a/assets/programming_functionalProgramming_functionalProgrammingBasicNotes.md.140216fc.lean.js b/assets/programming_functionalProgramming_functionalProgrammingBasicNotes.md.140216fc.lean.js new file mode 100644 index 00000000000..75c0b525c89 --- /dev/null +++ b/assets/programming_functionalProgramming_functionalProgrammingBasicNotes.md.140216fc.lean.js @@ -0,0 +1 @@ +import{o as a,c as s,a as n}from"./app.6dd2a1b7.js";const e='{"title":"Functional Programming Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Lambda Calculus","slug":"lambda-calculus"},{"level":3,"title":"Lambda Expression (Lambda-Term)","slug":"lambda-expression-lambda-term"},{"level":3,"title":"Lambda Reduction","slug":"lambda-reduction"},{"level":3,"title":"Church Numerals","slug":"church-numerals"},{"level":2,"title":"Definition for Functional Programming","slug":"definition-for-functional-programming"},{"level":2,"title":"Datatype","slug":"datatype"},{"level":3,"title":"Datatype Binding","slug":"datatype-binding"},{"level":3,"title":"Built-in Tagged Constructor","slug":"built-in-tagged-constructor"},{"level":3,"title":"Type Constructor","slug":"type-constructor"},{"level":2,"title":"Pattern Matching","slug":"pattern-matching"},{"level":2,"title":"Tail Position, Tail expression, Tail Call and Tail Recursion","slug":"tail-position-tail-expression-tail-call-and-tail-recursion"},{"level":2,"title":"Rules for expressions","slug":"rules-for-expressions"},{"level":3,"title":"samples","slug":"samples"},{"level":2,"title":"Standard ML","slug":"standard-ml"},{"level":3,"title":"functions","slug":"functions"},{"level":3,"title":"tuples","slug":"tuples"},{"level":3,"title":"lists","slug":"lists"},{"level":3,"title":"let expressions","slug":"let-expressions"},{"level":3,"title":"options","slug":"options"},{"level":3,"title":"boolean operations","slug":"boolean-operations"},{"level":3,"title":"closure","slug":"closure"},{"level":2,"title":"Type inference","slug":"type-inference"}],"relativePath":"programming/functionalProgramming/functionalProgrammingBasicNotes.md","lastUpdated":1627196177000}',p={},l=n('',70);p.render=function(n,e,p,t,o,r){return a(),s("div",null,[l])};export default p;export{e as __pageData}; diff --git a/assets/programming_game_gameDesignBasicNotes.md.fb3c7fed.js b/assets/programming_game_gameDesignBasicNotes.md.fb3c7fed.js new file mode 100644 index 00000000000..21ce2e923b6 --- /dev/null +++ b/assets/programming_game_gameDesignBasicNotes.md.fb3c7fed.js @@ -0,0 +1 @@ +import{o as l,c as i,a}from"./app.6dd2a1b7.js";const e='{"title":"Game Design Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"游戏心理学","slug":"游戏心理学"},{"level":2,"title":"声音设计(音频制作/声音合成)","slug":"声音设计-音频制作-声音合成"},{"level":2,"title":"角色设计","slug":"角色设计"},{"level":2,"title":"AI","slug":"ai"},{"level":3,"title":"技能形式","slug":"技能形式"},{"level":2,"title":"战斗设计","slug":"战斗设计"},{"level":2,"title":"镜头设计","slug":"镜头设计"}],"relativePath":"programming/game/gameDesignBasicNotes.md","lastUpdated":1627196177000}',r={},s=a('

Game Design Basic Notes

游戏心理学

  • 代入感/角色扮演
  • 操纵感/打击感
  • 故事性
  • 挑战性
  • 探索性
  • 收集性
  • 个性化与自定义
  • 社交性

声音设计(音频制作/声音合成)

  • 声音包络 (ASDR): attack decay sustain release

角色设计

  • 表面模型设计: 2D/3D 模型 风格 动作 属性
  • 逻辑设计: 装备切换显示 动作绑点 碰撞盒

AI

  • 触发逻辑: 警戒范围 攻击范围
  • 援助逻辑
  • 逃跑逻辑
  • 集群逻辑
  • 刷新逻辑
  • 仇恨列表
  • 技能前摇
  • 技能后摇

技能形式

  • 爆发
  • 冲锋
  • 防御: 霸体 格挡 弹刀 防反 绝对防御
  • 发怒: 受击 援助 空击
  • 保护

战斗设计

  • 打击感: 镜头抖动 镜头特写 镜头位移 镜头慢犯
  • 流畅感: 操作缓存响应 动画插值
  • 表现力: 丰富攻击招式 丰富受击反馈 丰富死亡效果

镜头设计

  • 跟随主角
  • 跟随 Boss
  • 场景空间差值镜头
  • 场景限行差值镜头
',16);r.render=function(a,e,r,h,t,d){return l(),i("div",null,[s])};export default r;export{e as __pageData}; diff --git a/assets/programming_game_gameDesignBasicNotes.md.fb3c7fed.lean.js b/assets/programming_game_gameDesignBasicNotes.md.fb3c7fed.lean.js new file mode 100644 index 00000000000..60450ec1ecb --- /dev/null +++ b/assets/programming_game_gameDesignBasicNotes.md.fb3c7fed.lean.js @@ -0,0 +1 @@ +import{o as l,c as i,a}from"./app.6dd2a1b7.js";const e='{"title":"Game Design Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"游戏心理学","slug":"游戏心理学"},{"level":2,"title":"声音设计(音频制作/声音合成)","slug":"声音设计-音频制作-声音合成"},{"level":2,"title":"角色设计","slug":"角色设计"},{"level":2,"title":"AI","slug":"ai"},{"level":3,"title":"技能形式","slug":"技能形式"},{"level":2,"title":"战斗设计","slug":"战斗设计"},{"level":2,"title":"镜头设计","slug":"镜头设计"}],"relativePath":"programming/game/gameDesignBasicNotes.md","lastUpdated":1627196177000}',r={},s=a('',16);r.render=function(a,e,r,h,t,d){return l(),i("div",null,[s])};export default r;export{e as __pageData}; diff --git a/assets/programming_linux_linuxBasicNotes.md.37adc63b.js b/assets/programming_linux_linuxBasicNotes.md.37adc63b.js new file mode 100644 index 00000000000..8fef1c1386f --- /dev/null +++ b/assets/programming_linux_linuxBasicNotes.md.37adc63b.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const e='{"title":"Linux Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Linux Boot System","slug":"linux-boot-system"},{"level":3,"title":"Grub","slug":"grub"},{"level":3,"title":"重装 Linux","slug":"重装-linux"},{"level":2,"title":"SSH 命令","slug":"ssh-命令"},{"level":3,"title":"Key","slug":"key"},{"level":3,"title":"SSHD","slug":"sshd"},{"level":3,"title":"SSH Config File","slug":"ssh-config-file"},{"level":3,"title":"密钥文件","slug":"密钥文件"},{"level":3,"title":"远程传输文件","slug":"远程传输文件"},{"level":2,"title":"命令优先级","slug":"命令优先级"},{"level":2,"title":"Linux 文件架构","slug":"linux-文件架构"},{"level":2,"title":"Ubuntu","slug":"ubuntu"},{"level":3,"title":"Ubuntu Locale Settings","slug":"ubuntu-locale-settings"},{"level":3,"title":"Ubuntu Themes","slug":"ubuntu-themes"},{"level":2,"title":"Arch Linux","slug":"arch-linux"},{"level":3,"title":"Basic Arch Linux Setup","slug":"basic-arch-linux-setup"},{"level":3,"title":"Pacman","slug":"pacman"},{"level":3,"title":"AUR","slug":"aur"},{"level":2,"title":"基本处理命令","slug":"基本处理命令"},{"level":3,"title":"ls","slug":"ls"},{"level":3,"title":"cd","slug":"cd"},{"level":3,"title":"pwd","slug":"pwd"},{"level":3,"title":"rm","slug":"rm"},{"level":3,"title":"cp","slug":"cp"},{"level":3,"title":"mv","slug":"mv"},{"level":3,"title":"ln","slug":"ln"},{"level":2,"title":"基本搜索命令","slug":"基本搜索命令"},{"level":3,"title":"locate","slug":"locate"},{"level":3,"title":"type","slug":"type"},{"level":3,"title":"apropos","slug":"apropos"},{"level":3,"title":"whereis and whatis","slug":"whereis-and-whatis"},{"level":3,"title":"which","slug":"which"},{"level":3,"title":"find","slug":"find"},{"level":3,"title":"Grep","slug":"grep"},{"level":2,"title":"Process Command","slug":"process-command"},{"level":3,"title":"uptime","slug":"uptime"},{"level":3,"title":"ps","slug":"ps"},{"level":3,"title":"top","slug":"top"},{"level":3,"title":"vmstat","slug":"vmstat"},{"level":3,"title":"pidstat","slug":"pidstat"},{"level":3,"title":"mpstat","slug":"mpstat"},{"level":3,"title":"lscpu","slug":"lscpu"},{"level":3,"title":"jobs","slug":"jobs"},{"level":3,"title":"bg","slug":"bg"},{"level":3,"title":"fg","slug":"fg"},{"level":3,"title":"kill","slug":"kill"},{"level":3,"title":"killall","slug":"killall"},{"level":3,"title":"shutdown","slug":"shutdown"},{"level":3,"title":"pstree","slug":"pstree"},{"level":3,"title":"xload and tload","slug":"xload-and-tload"},{"level":3,"title":"screen","slug":"screen"},{"level":2,"title":"CLI Input Output Command","slug":"cli-input-output-command"},{"level":3,"title":"cat","slug":"cat"},{"level":3,"title":"sort","slug":"sort"},{"level":3,"title":"uniq","slug":"uniq"},{"level":3,"title":"wc","slug":"wc"},{"level":3,"title":"Head and Tail","slug":"head-and-tail"},{"level":3,"title":"Tee","slug":"tee"},{"level":3,"title":"nl","slug":"nl"},{"level":3,"title":"fold","slug":"fold"},{"level":3,"title":"fmt","slug":"fmt"},{"level":3,"title":"pr","slug":"pr"},{"level":3,"title":"printf","slug":"printf"},{"level":2,"title":"帮助命令","slug":"帮助命令"},{"level":3,"title":"man","slug":"man"},{"level":3,"title":"help shell 内部命令","slug":"help-shell-内部命令"},{"level":3,"title":"info","slug":"info"},{"level":3,"title":"System Info","slug":"system-info"},{"level":2,"title":"压缩命令","slug":"压缩命令"},{"level":3,"title":"Zip","slug":"zip"},{"level":3,"title":"Gz","slug":"gz"},{"level":3,"title":"Bz2","slug":"bz2"},{"level":3,"title":"Tar","slug":"tar"},{"level":3,"title":"7z","slug":"_7z"},{"level":2,"title":"User and Group Command","slug":"user-and-group-command"},{"level":3,"title":"组操作","slug":"组操作"},{"level":3,"title":"用户操作","slug":"用户操作"},{"level":2,"title":"权限管理命令","slug":"权限管理命令"},{"level":3,"title":"普通权限","slug":"普通权限"},{"level":3,"title":"ACL 权限","slug":"acl-权限"},{"level":3,"title":"sudo 权限","slug":"sudo-权限"},{"level":3,"title":"SetUID and SetGID","slug":"setuid-and-setgid"},{"level":2,"title":"显示器管理命令","slug":"显示器管理命令"},{"level":3,"title":"xrandr","slug":"xrandr"},{"level":2,"title":"主机信息管理命令","slug":"主机信息管理命令"},{"level":2,"title":"Disk IO","slug":"disk-io"},{"level":3,"title":"挂载命令","slug":"挂载命令"},{"level":3,"title":"修复命令","slug":"修复命令"},{"level":3,"title":"分区命令","slug":"分区命令"},{"level":3,"title":"Zero Copy","slug":"zero-copy"},{"level":2,"title":"Device Command","slug":"device-command"},{"level":3,"title":"Monitor Info","slug":"monitor-info"},{"level":3,"title":"Touch Pad Synoptics","slug":"touch-pad-synoptics"},{"level":2,"title":"包管理命令","slug":"包管理命令"},{"level":3,"title":"rpm 命令","slug":"rpm-命令"},{"level":3,"title":"yum","slug":"yum"},{"level":3,"title":"源码包安装","slug":"源码包安装"},{"level":3,"title":"Applications Management","slug":"applications-management"},{"level":2,"title":"网络连接命令","slug":"网络连接命令"},{"level":3,"title":"wget","slug":"wget"},{"level":3,"title":"Certificate Bot","slug":"certificate-bot"},{"level":3,"title":"GFW","slug":"gfw"},{"level":2,"title":"网络管理命令","slug":"网络管理命令"},{"level":3,"title":"ufw","slug":"ufw"},{"level":3,"title":"arp","slug":"arp"},{"level":3,"title":"netstat","slug":"netstat"},{"level":3,"title":"nslookup","slug":"nslookup"},{"level":3,"title":"ping","slug":"ping"},{"level":3,"title":"telnet","slug":"telnet"},{"level":3,"title":"Trace Route","slug":"trace-route"},{"level":3,"title":"Net Filter Framework","slug":"net-filter-framework"},{"level":2,"title":"网络扫描命令","slug":"网络扫描命令"},{"level":3,"title":"fping","slug":"fping"},{"level":3,"title":"hping","slug":"hping"},{"level":3,"title":"mtr","slug":"mtr"},{"level":3,"title":"nmap","slug":"nmap"},{"level":3,"title":"ncat","slug":"ncat"},{"level":2,"title":"脚本运行命令","slug":"脚本运行命令"},{"level":3,"title":"Systemctl","slug":"systemctl"},{"level":3,"title":"定时任务","slug":"定时任务"},{"level":3,"title":"后台任务","slug":"后台任务"},{"level":3,"title":"开机任务","slug":"开机任务"},{"level":2,"title":"历史记录命令","slug":"历史记录命令"},{"level":3,"title":"history","slug":"history"},{"level":3,"title":"ctrl-r","slug":"ctrl-r"},{"level":3,"title":"History Shortcuts","slug":"history-shortcuts"},{"level":2,"title":"并行命令","slug":"并行命令"},{"level":2,"title":"C/C++ Binary Command","slug":"c-c-binary-command"},{"level":3,"title":"ldd","slug":"ldd"},{"level":3,"title":"nm","slug":"nm"},{"level":2,"title":"Plot Command","slug":"plot-command"},{"level":2,"title":"Other Command","slug":"other-command"},{"level":3,"title":"Time","slug":"time"},{"level":2,"title":"Shell 编程","slug":"shell-编程"},{"level":3,"title":"Shell Warnings","slug":"shell-warnings"},{"level":3,"title":"文件重定向","slug":"文件重定向"},{"level":3,"title":"变量","slug":"变量"},{"level":3,"title":"数值运算","slug":"数值运算"},{"level":3,"title":"Bash Expansions","slug":"bash-expansions"},{"level":3,"title":"流程控制语句","slug":"流程控制语句"},{"level":3,"title":"Bash Array","slug":"bash-array"},{"level":3,"title":"Bash Function","slug":"bash-function"},{"level":3,"title":"Bash IO","slug":"bash-io"},{"level":3,"title":"信号","slug":"信号"},{"level":3,"title":"Bash Debugging","slug":"bash-debugging"},{"level":3,"title":"Shell Script Best Practices","slug":"shell-script-best-practices"},{"level":3,"title":"Interactive Shell Script Tips","slug":"interactive-shell-script-tips"},{"level":2,"title":"Terminal","slug":"terminal"},{"level":3,"title":"Terminal Basis","slug":"terminal-basis"},{"level":3,"title":"Default Terminal","slug":"default-terminal"},{"level":3,"title":"Tmux","slug":"tmux"},{"level":2,"title":"Perf Tools","slug":"perf-tools"},{"level":3,"title":"Top Command","slug":"top-command"},{"level":3,"title":"dmesg","slug":"dmesg"},{"level":3,"title":"iostat","slug":"iostat"},{"level":3,"title":"free","slug":"free"},{"level":3,"title":"sar","slug":"sar"},{"level":3,"title":"perf","slug":"perf"},{"level":2,"title":"Linux Tools","slug":"linux-tools"},{"level":3,"title":"FFmpeg","slug":"ffmpeg"},{"level":3,"title":"Nginx","slug":"nginx"},{"level":3,"title":"Namespaces and Cgroup","slug":"namespaces-and-cgroup"},{"level":3,"title":"Docker","slug":"docker"}],"relativePath":"programming/linux/linuxBasicNotes.md","lastUpdated":1627731858000}',p={},l=a('

Linux Basic Notes

Linux Boot System

Grub

Grub Configuration

  • /etc/default/grub配置文件, 用于一些基本的修改项, 如默认启动项, Grub 界面等待时长, Grub 主题 etc. More details in info -f grub -n 'Simple configuration'
# Default Startup OS\nGRUB_DEFAULT=0\n\n# Default Timeout\nGRUB_TIMEOUT=5\n\n# https://github.com/vinceliuice/grub2-themes\nGRUB_THEME="/boot/grub/themes/Tela/theme.txt"\n
1
2
3
4
5
6
7
8
  • /etc/grub.d/*生成/boot/grub/grub.cfg的执行脚本(update-grub命令), 可以更细致地修改启动项, 如各个启动项的名称、顺序等.

Grub Repair

Windows Repair
  • easyBCD for non-efi loader
  • with efi loader, run command:
# root commander\nbcdedit /set "{bootmgr}" path \\EFI\\ubuntu\\grubx64.efi\n
1
2
Ubuntu Live Repair
sudo add-apt add-apt-repository ppa:yannubuntu/boot-repair\nsudo apt update\nsudo apt install boot-repair\nboot-repair\n
1
2
3
4

重装 Linux

  • 自动挂载项 /etc/fstab etc/rc.local
  • 自定义脚本-新建目录(加入环境变量)
  • 自定义别名 ~/.bashrc

SSH 命令

Key

ssh-keygen -t rsa\nssh-add ~/.ssh/id_rsa\n
1
2

SSHD

  • config file in /etc/ssh/sshd_config
sudo systemctl reload sshd\nsudo service restart sshd\n
1
2

SSH Config File

~/.ssh/config:

  • Host 别名
    • HostName 主机名(ip) ssh user@ip
    • Port 可忽略
    • User 登录用户名 ssh user@ip
    • PreferredAuthentications publicKey
    • IdentityFile 密钥文件完整路径 ssh -i file
Host github.com\n  HostName github.com\n  PreferredAuthentications publicKey\n  IdentityFile ~/.ssh/id_rsa\nHost cs.github.com\n  HostName github.com\n  PreferredAuthentications publicKey\n  IdentityFile ~/.ssh/cs\nHost cloud\n    HostName xx.org\n    User  root\n    IdentityFile ~/.ssh/dsl_private_key\nHost bwg\n    HostName 23.106.150.152\n    User root\n    Port 29692\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git clone git@github.com:user/repo\ngit clone git@cs.github.com:user/repo\n
1
2
ssh -qTfnN -D 7070 bwg\ngoogle-chrome socks5 127.0.0.1 7070\n
1
2

密钥文件

远程传输文件

rsync -ax -e 'ssh -c blowfish' /root/start_dir root@x.x.x.x:/root/dest_dir\n
1
sshpass -p "$DEPLOY_PASSWORD" \\\n  scp -o StrictHostKeyChecking=no \\\n      -P $DEPLOY_PORT \\\n      -r ./build $DEPLOY_USER@$DEPLOY_ADDR:/var/www/html\n
1
2
3
4

命令优先级

包含路径命令 ./vmtools.pl >别名命令 >bash 内部命令 >$PATH 包含目录内的命令 /bin /sbin

Linux 文件架构

man hier\n
1

通过源码包安装的软件,可以通过 ./configure --prefix=/opt/

/usr/src 是内核源码存放的目录

Ubuntu

Ubuntu Locale Settings

export LANG=en_US\nxdg-user-dirs-gtk-update\nexport LANG=zh_CN\n
1
2
3
  • /var/lib/locales/supported.d/local
sudo locale-gen zh_CN.GBK\nsudo locale-gen zh_CN.GB18030\nsudo dpkg-reconfigure locales\n
1
2
3

Ubuntu Themes

Icon Themes

Nightly build for Numix Circle icon:

sudo add-apt-repository ppa:numix/ppa\nsudo apt update\nsudo apt install numix-icon-theme-circle\n
1
2
3

GTK Themes

GTK/GNOME themes located in /usr/share/themes/ or ~/.themes/:

# Vimix Cursors Installation\ngit clone https://github.com/vinceliuice/Vimix-cursors\nsudo ./Vimix-cursors/install.sh\n\n# WhiteSur GNOME theme Installation\ngit clone https://github.com/vinceliuice/WhiteSur-gtk-theme\nsudo ./WhiteSur-gtk-theme/install.sh -t all -i ubuntu\n# Tweak for Firefox\nsudo ./WhiteSur-gtk-theme/tweaks.sh -f\n# Tweak for Snap Apps\nsudo ./WhiteSur-gtk-theme/tweaks.sh -s\n# Tweak for GDM\nsudo ./WhiteSur-gtk-theme/tweaks.sh -g -i ubuntu\n# Tweak Help Docs\nsudo ./WhiteSur-gtk-theme/tweaks.sh -h\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Repair for not detected HDMI problem:

sudo dpkg-reconfigure gdm3\nsudo apt install --reinstall gdm3 lightdm ubuntu-desktop\n
1
2

GNOME Shell Extension

  • Install GNOME shell extension for browser.
  • Install local binding app: sudo apt install chrome-gnome-shell.
  • Visit extensions.gnome.org to install extensions.

Arch Linux

Basic Arch Linux Setup

less /usr/share/aif/docs/official_installation_guide_en\npacman -S lynx arch-wiki-docs arch-wiki-lite\nlynx /usr/share/doc/arch-wiki/html/index.html\n
1
2
3
systemctl enable dhcpcd\nreboot\npacman -S --needed base-devel git wget jshon expac yajl zsh vim\n
1
2
3
  • makepkg
curl -L -O https://aur.archlinux.org/cgit/aur.git/snapshot/package_name.tar.gz\ncd package_name\nless PKGBUILD\nless package_name.install\n\nmakepkg -si\n# -s sync deps\n# -i install\n# -r rm deps\n# -c clean up\n
1
2
3
4
5
6
7
8
9
10

Pacman

  • /etc/pacman.conf
  • /etc/pacman.d/mirrorlist

AUR

# packages' list\nwget https://aur.archlinux.org/packages.gz\n
1
2

基本处理命令

ls

  • -lh(long human)
  • -ld(long directory)
  • -i inode(ID 号) --color==auto

权限(user/group/other) 引用计数 user group 文件大小 文件修改时间 文件名

cd

  • -上次目录
  • ..上级目录

pwd

print working directory

rm

  • –r delete directory
  • –f delete forcedly
  • -i 显示确认信息

cp

  • -r copy directory
  • -p copy property
  • -d copy link
  • -a --all(-pdr)
  • -i 显示确认信息

mv

无需参数(改名+移动)

  • -i 显示确认信息

ln

link 命令 .bak/.hard(硬链接) .soft(软链接:创建链接时填写绝对路径)

ln -s(创建软链接) [原文件][目标文件]

基本搜索命令

locate

结合 updatedb 命令(该命令一般自动 1 天/次)

type

indicate how a command name is interpreted

apropos

display a list of appropriate commands

whereis and whatis

which

find

find [搜索路径] [可选参数] [文件名](可加""):

  • -name
  • -iname 不区分大小写
  • -user user_name 按照所有者搜索
  • -nouser 搜索没有所有者的文件
  • -atime(文件访问时间)/-ctime(改变文件属性)/-mtime(改变文件内容) -10(十天内)/10(十天当天)/+10(十天前)
  • -size(文件大小) -25k(小于 25k)/25M(25M)/+25G(大于 25G)
  • -inum inode_number
  • -a / -o 逻辑与/逻辑或(左右两端搜索条件)
  • -exec/-ok `system_command_list {} ;对搜索结果执行操作

Grep

grep [可选参数] '字符串' 文件名:

  • -I: 不区分大小写
  • -v: 排除指定字符串
  • -r: recursive on directory
  • -l: only print matched filename
  • --exclude

Find FunctionalComponent in files and open them all:

grep -lr FunctionalComponent src --exclude=\\*.md | xargs code\n
1

Process Command

uptime

Average load information

ps

Report a snapshot of current processes

ps aux\n
1

top

top/htop:

  • Display tasks
  • Average load
  • Process status
  • CPU usage

atop:

  • Memory usage
  • Disk I/O usage
  • Network usage

vmstat

Outputs a snapshot of system resource usage:

  • CPU usage
  • Context switch times
  • Interrupt times (/proc/interrupts)
  • Running and exclusive process status
  • Memory usage
  • Swap space
  • Disk I/O usage

pidstat

Process and Thread:

  • CPU usage
  • Context switch times
  • Interrupt times (/proc/interrupts)
pidstat 1\n
1

mpstat

  • CPU usage
  • Software interrupt times (/proc/interrupts)
mpstat -P ALL 1\n
1

lscpu

Show /proc/cpuinfo.

jobs

list active jobs

bg

place a job in the background

fg

place a job in the foreground

kill

send a signal to a process

killall

kill processes by name

shutdown

shutdown or reboot the system

pstree

outputs a process list arranged in a tree-like pattern

xload and tload

draws a graph showing system load over time

screen

screen -S screenName\nscreen -ls\nscreen -r\n
1
2
3
  • Ctrl+d // detach window
  • Ctrl+k // kill window

CLI Input Output Command

cat

concatenate files

sort

sort lines of text

uniq

report or omit repeated lines

wc

print newline, word, and byte counts for each file

Head and Tail

output the first/last part of a file

head -n 5 filename\ntail -f filename\n
1
2

Tee

read from standard input and write to standard output and files

[me@linuxBox ~]$ ls /usr/bin | tee ls.txt | grep zip\nbunzip2\nbzip2\n....\n
1
2
3
4

nl

number lines

fold

wrap each line to a specified length

fmt

a simple text formatter

pr

prepare text for printing

printf

format and print data

帮助命令

man

  • -f 显示操作等级
  • -k 包含匹配
  • -1/2/.../9 显示命令不同操作等级的帮助
  1. Commands (Programs) Those commands that can be executed by the user from within a shell.
  2. System calls Those functions which must be performed by the kernel.
  3. Library calls Most of the libc functions.
  4. Special files (devices) Files found in /dev.
  5. File formats and conventions The format for /etc/passwd and other human-readable files.
  6. Games
  7. Conventions and miscellaneous Overviews of various topics, conventions and protocols, character set standards, and miscellaneous other things.
  8. System management commands Commands like mount(8), many of which only root can execute.

help shell 内部命令

显示 shell 内部命令帮助,如 cd 命令(shell 内部命令)

info

显示大型帮助文档 - enter 进入 u 返回 p 上一节 n 下一节 q 退出

System Info

sudo add-apt-repository ppa:dawidd0811/neofetch\nsudo apt-get update\nsudo apt-get install neofetch\n
1
2
3
sudo apt-get install screenfetch\n
1

压缩命令

Zip

  • zip -r(目录) 压缩文件 源文件/源目录
  • unzip 源文件 -d 指定路径

Gz

  • gzip 源文件
  • gzip –c 源文件 > 压缩文件
  • gzip -r 源目录 将源目录下所有子文件分别单独压缩
  • gzip –d(解压缩) 文件
  • gunzip 压缩文件

Bz2

不可压缩目录

  • bzip2 –k(保留源文件) 源文件
  • bzip2 –d(解压缩) –k(保留压缩文件) 压缩文件
  • bunzip2 –k(保留压缩文件) 压缩文件

Tar

tar.gz/.tar.bz2:

tar [可选参数] 压缩文件(可指定压缩路径) [-c 解压缩路径]源文件/源目录

  • -z 将.tar 压缩为.tar.gz -j 将.tar 压缩为.tar.bz2
  • -c 打包 -x 解打包
  • -t 查看压缩文件
  • -v 显示过程
  • -f 指定压缩文件名
  • -C 指定解压缩路径
  • -zcvf/-zxvf/-ztcf -jcvf/-jxvf/-jtvf

7z

7z x manager.7z -r -o /home/xx\n7z a -t7z -r manager.7z /home/manager/*\n
1
2
  • a: add
  • x: extract
  • -r: recursive
  • -o: specific path
  • -t: type

User and Group Command

  • w/who 查看用户详细信息
  • last 显示所有用户登陆信息(/var/log/wtmp)
  • lastlog 显示所有用户最后一次登陆时间(/var/log/lastlog)

组操作

创建组

groupadd test

修改组

groupmod -n test2 test -g

删除组

groupdel test2

查看组

  • groups someUser
  • cat /etc/group
cat /etc/passwd | awk -F [:]{print $4}’\n\\ |sort|uniq | getent group |awk -F [:]{print $1}’\n
1
2

用户操作

增加用户

useradd [options] LOGIN\n
1

Options:

  • -b, --base-dir BASE_DIR 设置基本路径作为用户的登录目录
  • -c, --comment COMMENT 对用户的注释
  • -d, --home-dir HOME_DIR 设置用户的登录目录
  • -D, --defaults 改变设置
  • -e, --expiredate EXPIRE_DATE 设置用户的有效期
  • -f, --inactive INACTIVE 用户过期后,让密码无效
  • -g, --gid GROUP 使用户只属于某个组
  • -G, --groups GROUPS 使用户加入某个组(附设组)
  • -h, --help 帮助
  • -k, --skel SKEL_DIR 指定其他的 skel 目录
  • -K, --key KEY=VALUE 覆盖 /etc/login.defs 配置文件
  • -m, --create-home 自动创建登录目录
  • -l, 不把用户加入到 lastlog 文件中
  • -M, 不自动创建登录目录
  • -r, 建立系统账号
  • -o, --non-unique 允许用户拥有相同的 UID
  • -p, --password PASSWORD 为新用户使用加密密码
  • -s, --shell SHELL 登录时候的 shell
  • -u, --uid UID 为新用户指定一个 UID
  • -Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping
useradd -s bash -m testUser\npasswd testUser # modify `/etc/passwd`, then add to `/etc/sudoers`\n
1
2

adduser is a perl script which uses useradd binary in back-end, adduser is more user friendly and interactive.

修改用户

usermod -d /home/test -G test2 test

  • -l 新用户名 旧用户名 修改用户名
  • -g 新用户组 目标用户 改变用户所属组

gpasswd -a test test2 将用户 test 加入到 test2 组(附设组)

gpasswd -d test test2 将用户 test 从 test2 组中移出

usermod -aG sudo <username>\n
1

删除用户

userdel test -r 同时删除用户登录目录(/home/xxx)

查看用户

w/who 查看当前登录的所有用户

whoami 查看当前登录用户名

finger apacheUser 查看单个用户信息

限制用户

  • passwd -l 用户名 锁定用户
  • passwd -u 用户名 解锁用户
  • passwd -d 用户名 清除用户密码

权限管理命令

普通权限

  • chown 用户名:组名 文件名
  • chgrp 组名 文件名
  • umask 存储位置 ——/etc/profile
  • 文件默认权限 = 文件默认最大权限 rw-(666) 减去 umask 值(如----w--w-)(022)
  • 目录默认权限 = 目录默认最大权限 rwx(777) 减去 umask 值
  • id <username>

ACL 权限

  • 查看分区 ACL 权限是否开启 dumpe2fs -h 设备分区名

  • 临时开启分区 ACL 权限 mount -o remount,acl 设备分区名

  • 永久开启分区 ACL 权限 /etc/fstab

  • setfacl -m (d:默认权限) u/g:用户名/组名:权限(rwx) 文件名

  • getfacl 文件名——查看文件 ACL 权限

sudo 权限

/etc/sudoers.tmp

SetUID and SetGID

可执行程序/目录+普通用户临时获得 root 权限 (rws):

  • chmod 0xxx 取消双权限
  • chmod 2xxx 设置 SetGID 权限
  • chmod 4xxx 设置 SetUID 权限
  • chmod 6xxx 设置双权限

显示器管理命令

xrandr

xrandr -s 1920x1800 # set resolution\n
1

主机信息管理命令

#!/bin/bash\n\n# Simple print cpu topology\n\n# numactl --hardware\n# ls /sys/devices/system/node/node0\n# lscpu\n\nfunction get_nr_processor()\n{\n    grep '^processor' /proc/cpuinfo | wc -l\n}\n\nfunction get_nr_socket()\n{\n    grep 'physical id' /proc/cpuinfo | awk -F: '{\n            print $2 | "sort -un"}' | wc -l\n}\n\nfunction get_nr_siblings()\n{\n    grep 'siblings' /proc/cpuinfo | awk -F: '{\n            print $2 | "sort -un"}'\n}\n\nfunction get_nr_cores_of_socket()\n{\n    grep 'cpu cores' /proc/cpuinfo | awk -F: '{\n            print $2 | "sort -un"}'\n}\n\necho '===== CPU Topology Table ====='\necho\n\necho '+--------------+---------+-----------+'\necho '| Processor ID | Core ID | Socket ID |'\necho '+--------------+---------+-----------+'\n\nwhile read line; do\n    if [ -z "$line" ]; then\n        printf '| %-12s | %-7s | %-9s |\\n' $p_id $c_id $s_id\n        echo '+--------------+---------+-----------+'\n        continue\n    fi\n\n    if echo "$line" | grep -q "^processor"; then\n        p_id=`echo "$line" | awk -F: '{print $2}' | tr -d ' '`\n    fi\n\n    if echo "$line" | grep -q "^core id"; then\n        c_id=`echo "$line" | awk -F: '{print $2}' | tr -d ' '`\n    fi\n\n    if echo "$line" | grep -q "^physical id"; then\n        s_id=`echo "$line" | awk -F: '{print $2}' | tr -d ' '`\n    fi\ndone < /proc/cpuinfo\n\necho\n\nawk -F: '{\n    if ($1 ~ /processor/) {\n        gsub(/ /,"",$2);\n        p_id=$2;\n    } else if ($1 ~ /physical id/){\n        gsub(/ /,"",$2);\n        s_id=$2;\n        arr[s_id]=arr[s_id] " " p_id\n    }\n}\n\nEND{\n    for (i in arr)\n        printf "Socket %s:%s\\n", i, arr[i];\n}' /proc/cpuinfo\n\necho\necho '===== CPU Info Summary ====='\necho\n\nnr_processor=`get_nr_processor`\necho "Logical processors: $nr_processor"\n\nnr_socket=`get_nr_socket`\necho "Physical socket: $nr_socket"\n\nnr_siblings=`get_nr_siblings`\necho "Siblings in one socket: $nr_siblings"\n\nnr_cores=`get_nr_cores_of_socket`\necho "Cores in one socket: $nr_cores"\n\nlet nr_cores*=nr_socket\necho "Cores in total: $nr_cores"\n\nif [ "$nr_cores" = "$nr_processor" ]; then\n    echo "Hyper-Threading: off"\nelse\n    echo "Hyper-Threading: on"\nfi\n\necho\necho '===== END ====='\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

Disk IO

主分区(primary)与延伸分区(extended) 延伸分区可以继续划分成逻辑分区(logical)

挂载命令

mount [-t 文件系统][-o 特殊选项] 设备文件名 挂载点(挂载目录/media /misc /mnt)

  • 无参数 显示当前挂载设备
  • -a 依据/etc/fstab 文件配置,自动挂载

umount 设备文件名/挂载点

fdisk –l

修复命令

sudo debugfs /dev/sda9\n> debugfs: lsdel\n
1
2

分区命令

fdisk

分区表类型 MBR

n p e l 新 主 逻辑 扩展 分区 w 激活

parted

分区表类型 MBR/GPT

  • mklabel 选择分区表类型
  • print 打印分区信息
  • mkpart 新建分区
  • rm 删除分区
  • unit 选择单位
  • quit 结束分区

Zero Copy

  • read + write: 4 context switch, 4 data copy (2 DMA, 2 CPU).
  • mmap + write: 4 context switch, 3 data copy (2 DMA, 1 CPU).
  • sendfile: 2 context switch, 3 data copy (2 DMA, 1 CPU).
  • scatter and gather sendfile: 2 context switch, 2 data copy (1 DMA, 1 SG-DMA).
  • 传输大文件 (无法命中内核 PageCache) 使用 异步 I/O + 直接 I/O, 传输小文件使用 Zero Copy.
location /video/ {\n    sendfile on;\n    aio on;\n    directio 1024m;\n}\n
1
2
3
4
5

Device Command

Monitor Info

sudo apt-get install read-edid\nsudo get-edid | parse-edid\n
1
2

Touch Pad Synoptics

synclient TouchpadOff=0\n
1

包管理命令

rpm 命令

安装和卸载时同时存在依赖性(包依赖、库依赖)

rpm 查询:

  • -q 包名 查询已安装的包 //必备参数
  • -a 查询所有已安装的包
  • -i 查询软件信息
  • -l list
  • -f 查询系统文件属于哪个软件包
  • -R 查询软件包的依赖性
  • -p 查询未安装包 //普适参数

rpm 校验(查看 Cracker 信息):

  • -V 校验已安装包 相应信息不是.号便是被修改项 可用于找回丢失的系统命令

yum

源配置文件:/etc/yum.repos.d

cd /etc/yum.repos.d\nmv CentOS-Base.repo CentOS-Base.repo.bk\nwget http://mirrors.163.com/.help/CentOS7-Base-163.repo\nyum makecache\n
1
2
3
4
  • yum list
  • yum search 'keyword'
  • yum -y(自动回答 yes) install 包名
  • yum -y update 包名
  • yum -y remove 包名
  • yum grouplist
  • yum groupinstall 软件组名
  • yum groupremove 软件组名

源码包安装

指定位置:

  • /usr/local/软件名/
  • /usr/local/src/软件名/

(如上述脚本出错,执行 make clean)

make install\n
1

e.g apache /var/www/html/index.html /usr/local/apache/htdocs/index.html

Applications Management

  • desktop shortcut: /usr/share/applications
  • startup apps: gnome-session-properties or gnome-tweaks

网络连接命令

wget

  • 下载全站资料
  • -P 表示下载到哪个目录
  • -r 表示递归下载
  • -np 表示不下载旁站连接.
  • -k 表示将下载的网页里的链接修改为本地链接.
  • -p 获得所有显示网页所需的元素
wget -r -p -np -k -P ~/tmp/ http://java-er.com\n
1

Certificate Bot

CertBot for SSL certificates.

GFW

yum install python-setuptools && easy_install pip\npip install shadowsocks\necho "nohup sslocal -c /etc/shadowsocks.json /dev/null 2>&1 &" /etc/rc.local\nnohup ssserver -c /etc/shadowsocks.json -d start /dev/null 2>&1 &\n
1
2
3
4

网络管理命令

用途net-tool(被淘汰)iproute2
地址和链路配置ifconfigip addr, ip link
路由表routeip route
邻居arpip neigh
VLANvconfigip link
隧道iptunnelip tunnel
组播ipmaddrip maddr
统计netstatss
ip link show\nip address show\nip route show\n\n# add commands to /etc/init.d/local.sh\n\nrm -fr /etc/udev/rules.d/70-persistent-net.rules\n\n# start up network adapter\nip link set eth0 up\n\n# add/delete static ip\nip address add 192.168.1.1/24 dev eth0\nip address del 192.168.1.1/24 dev eth0\n\n# add/delete static route\nip route add 192.168.1.0/24 dev eth0\nip route del 192.168.1.0/24 dev eth0\nip route add default via 192.168.0.196\n\n# watch packets\nwatch -n 1 "ifconfig eth0"\nwatch -n 1 "ifconfig eth1"\nwatch -n 1 "ifconfig eth2"\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

ufw

ufw status\nufw enable\nufw allow ssh\nufw allow http\nufw allow https\n
1
2
3
4
5

arp

arp -a显示地址解析协议 (IP 地址—网卡地址):

  • 网际互联层:IP 协议(网际)、IGMP 协议(互联网组管理)、ICMP 协议(互联网控制报文)
  • 传输层:TCP 协议(传输控制)、UDP 协议(用户数据报)

netstat

netstat -an查看本机启用的端口:

  • (-a 查看所有连接和监听端口 -n 显示 IP 地址和端口号)
  • -t tcp 协议端口
  • -u udp 协议端口
  • -l 监听状态服务

nslookup

nslookup domain_name查看 DNS 解析器:

/etc/network/interfaces

主机名:

  • /etc/hostname
  • /etc/sysconfig/network
  • /etc/resolv.conf

ping

ping -c ip/domain探测网络状况

telnet

telnet [ip/domain] [端口]远程管理与端口探测命令

Trace Route

  • traceroute [-n IP] domain路由跟踪命令
  • traceroute -n -I -T -p路由扫描

Net Filter Framework

nftables 命令行工具:nft

网络扫描命令

预防策略——SYN 攻击、DDoS 攻击

fping

fping -a -u -g -f [target]批量扫描主机地址

hping

hping -p -S -a可伪造 IP 地址

mtr

路由扫描

nmap

批量主机服务扫描:

  • -P ICMP
  • -sS TCP SYN
  • -sT TCP connect()
  • -sU UDP

ncat

批量主机服务扫描:

  • -w 设置超时时间
  • -v 显示命令执行过程
  • -z 一个输入输出模式
  • -u UDP 协议

脚本运行命令

exec 1>>output.log exec 2>>error.log

Systemctl

systemctl enable local\n
1

in /etc/init.d/local

#!/bin/bash\n### BEGIN INIT INFO\n# Provides:          local\n# Required-Start:    $all\n# Required-Stop:\n# Default-Start:     3 4 5\n# Default-Stop:\n# Short-Description: Personal start script\n\nsslocal -c shadowsocks.json -d start\n
1
2
3
4
5
6
7
8
9
10

内存控制

sysctl vm [-options] CONFIG \nswapoff\n
1
2

定时任务

crontab

crontab -l(list)\ncrontab -e(establish)\n
1
2
  • m,n —— 分隔多个时间
  • m-n —— 表示时间范围
  • /n —— 表示每隔 n 时间
  • 天数 与 星期 设置 之间 是 “或”关系
  • /var/spool/cron/user_name/
  • /var/log/con
  • /etc/cron.*ly 时间表
  • /etc/anacrontab: 异步时间表

后台任务

  • jobs —— 所有作业

  • atq —— 延时作业队列

  • at -M(不使用邮件发送运行结果) -f filename deltaTime

  • atrm 作业号/名

  • bg/fg 作业号/名

  • nohup 脚本 & —— 脱离控制台并后台运行脚本

19 ~ -20 (-20 优先级最高)

  • nice -n number 作业号/名
  • renice number -p PID

开机任务

  • /etc/rc.local —— 系统开机任务
  • /etc/profile/ /etc/bash.bashrc —— bash 启动任务/远程登陆任务
  • /etc/bash.bashrc —— SSH 连接任务

历史记录命令

history

  • -c 清除历史命令
  • -w (~/.bash_history) 保存历史命令

/etc/profile 中修改 HISTSIZE !n/!!/!字符串 重复执行第 n 条/上一条/指定开头的历史命令

# repeat history command\n!number\n
1
2

ctrl-r

press ctrl-r 提示符改变,显示我们正在执行反向增量搜索。 搜索过程是”反向的”,因为我们按照从”现在”到过去 某个时间段的顺序来搜寻。 下一步,我们开始输入要查找的文本搜索返回我们需要的结果。 (enter to execute, ctrl-j to copy)

History Shortcuts

commandfunction
Ctrl-p移动到上一个历史条目
Ctrl-n移动到下一个历史条目
Alt-<移动到历史列表开头
Alt->移动到历史列表结尾
Ctrl-r反向增量搜索
Alt-p反向搜索,非增量搜索
Alt-n向前搜索,非增量
Ctrl-o执行历史列表中的当前项,并移到下一个

并行命令

命令间插入符

  • command1;command2 顺序执行,相当于 C 语言中语句结束符
  • command1&&command2 命令同时执行(当 1 正确时)或同时不执行(当 1 出错时)
  • command1 || command2 只执行一个命令(正确命令)
  • command1 | command2 前一正确命令的输出结果作为后一命令的输入结果

e.g ls && echo yes >> .log || echo no >> .log

C/C++ Binary Command

ldd

ldd ./lib.sio\n
1

nm

nm -Ca ./lib.so\n
1

Plot Command

chart.gp

#!/usr/bin/env gnuplot\n\nset term wxt enhanced\nset xtics\nset view\nset multiplot\nset size\nset origin\nfit\n\nplot 'data.dat' using 1:2, 'data.dat' using 1:3\n
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/gnuplot -c\n\nset terminal png enhanced\nset output ARG1.".png"\nset style data linespoints\nshow timestamp\nset title ARG1\nset xlabel "time (seconds)"\nset ylabel "Segments (cwnd, ssthresh)"\nplot ARG1 using 1:7 title "snd_cwnd", \\\n     ARG1 using 1:($8>=2147483647 ? 0 : $8) title "snd_ssthresh"\n
1
2
3
4
5
6
7
8
9
10
11

Other Command

Time

date\n
1

change ntp (Network Time Protocol) time

sudo apt-get install ntpdate\nsudo iptables -A OUTPUT -p udp --dport 123 -j ACCEPT\nsudo iptables -A INPUT -p udp --sport 123 -j ACCEPT\nsudo ntpdate time.windows.com\nsudo hwclock --localtime --systohc\n
1
2
3
4
5

use local time (not UTC time)

sudo timedatectl set-local-rtc 1\n
1

Shell 编程

Shell Warnings

  • = 左右无空格
  • () [] 内部最好有空格
  • 数值运算用 (()) 或 $(())

文件重定向

  • > 文件名/输出设备名 覆盖标准输出重定向
  • >> 文件名/输出设备名 追加标准输出重定向
  • 2>(右端无空格)文件名/输出设备名 覆盖错误输出重定向
  • 2>>(右端无空格)文件名/输出设备名 追加错误输出重定向
  • >/>> 文件 2>&1 &>/&>>文件 覆盖/追加正确输出与错误输出同时重定向
  • </<< 文件名/输入设备名 覆盖/追加标准输入重定向

Here Document

command << END\n...\nEND\n\ncommand << EOF\n...\nEOF\n
1
2
3
4
5
6
7
#!/bin/bash\ngnuplot -persist <<EOF\nset data style linespoints\nshow timestamp\nset title "$1"\nset xlabel "time (seconds)"\nset ylabel "Segments (cwnd, ssthresh)"\nplot "$1" using 1:7 title "snd_cwnd", \\\\\n     "$1" using 1:(\\$8>=2147483647 ? 0 : \\$8) title "snd_ssthresh"\nEOF\n
1
2
3
4
5
6
7
8
9
10

变量

基本变量

  • = : 左右两端不可有空格
  • ' ': 完全标准字符串
  • " ": 格式化字符串
  • 调用变量值:$变量名
  • set/unset——设置/取消变量

built-in 变量

  • $*/$@: argv[1], ..., argv[n]
  • $0/$1/../$n: argv[0], ..., argv[n]
  • $#: argc
  • $?: exit code of last command
if [ "$?" -ne "0" ];then\n    echo "sorry, command execution failed!"\nfi\n
1
2
3

每次 shift 命令执行的时候,变量 $2 的值会移动到变量 $1 中,变量 $3 的值会移动到变量 $2 中. 变量 $# 的值也会相应的减 1

#!/bin/bash\n# posit-param2: script to display all arguments\ncount=1\nwhile [[ $# -gt 0 ]]; do\n    echo "Argument $count = $1"\n    count=$((count + 1))\n    shift\ndone\n
1
2
3
4
5
6
7
8
usage () {\n    echo "$PROGNAME: usage: $PROGNAME [-f file | -i]"\n    return\n}\n# process command line options\ninteractive=\nfilename=\nwhile [[ -n $1 ]]; do\n    case $1 in\n    -f | --file)            shift\n                            filename=$1\n                            ;;\n    -i | --interactive)     interactive=1\n                            ;;\n    -h | --help)            usage\n                            exit\n                            ;;\n    *)                      usage >&2\n                            exit 1\n                            ;;\n    esac\n    shift\ndone\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

环境变量

  • /etc/profile.d/*.sh
  • ~/.bash_profile
  • ~/.bashrc
  • /etc/profile
  • /etc/bash.bashrc
  • /etc/issue——shell 登录信息
  • PS1 环境变量——shell 头行打印信息
  • PATH 环境变量
Environment Command
  • env——查看环境变量
  • export 变量名=变量值——设置环境变量
  • printenv

数值运算

declare 命令

Bash Expansions

  • $(()) or $[]: arithmetic expansion

一般地, 将数值运算用 (()) [[]]$(()) 括起, 可以确保变量不会被识别为 string

read x\nread y\n\necho $((x + y))\necho $((a < b ? a : b))\n\nif ((a > b))\nthen\n    echo "a > b"\nfi\n\nif [[ a -gt b ]]\nthen\n    echo "a > b"\nfi\n\nif [ "$a" -gt "$b" ]\nthen\n    echo "a > b"\nfi\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • [[ xxx ]]: condition
  • (( xxx )): arithmetic condition
operatorfunction
! EXPRESSIONThe EXPRESSION is false
-n STRINGThe length of STRING is greater than zero
-z STRINGThe length of STRING is zero (ie it is empty)
STRING1 == STRING2STRING1 is equal to STRING2
STRING1 != STRING2STRING1 is not equal to STRING2
STRING1 > STRING2STRING1 sorts after STRING2
STRING1 < STRING2STRING1 sorts before STRING2
INTEGER1 -eq INTEGER2INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2INTEGER1 is numerically less than INTEGER2
-d FILEFILE exists and is a directory
-e FILEFILE exists
-r FILEFILE exists and the read permission is granted
-s FILEFILE exists and it's size is greater than zero
-w FILEFILE exists and the write permission is granted
-x FILEFILE exists and the execute permission is granted
AND -a &&
OR -o ||
NOT ! !
  • {}: group regexp
echo a{A{1,2},B{3,4}}b\naA1b aA2b aB3b aB4b\n
1
2
  • ${}: string expansion
    • ${parameter:-word}: 若 parameter 没有设置(例如,不存在)或者为空,展开结果是 word 的值。 若 parameter 不为空,则展开结果是 parameter 的值
    • ${parameter:+word}: 若 parameter 没有设置或为空,展开结果为空。 若 parameter 不为空, 展开结果是 word 的值会替换掉 parameter 的值
    • ${parameter:=word}: 若 parameter 没有设置或为空,展开结果是 word 的值。 另外,word 的值会赋值给 parameter。 若 parameter 不为空,展开结果是 parameter 的值
    • ${parameter:?word}: 若 parameter 没有设置或为空,这种展开导致脚本带有错误退出, 并且 word 的内容会发送到标准错误。 若 parameter 不为空, 展开结果是 parameter 的值
    • ${!prefix*} ${!prefix@}: 这种展开会返回以 prefix 开头的已有变量名
    • ${#parameter}: 展开成由 parameter 所包含的字符串的长度
    • ${parameter:offset} ${parameter:offset:length}: 提取一部分字符
    • ${parameter,,} 把 parameter 的值全部展开成小写字母
    • ${parameter,} 仅仅把 parameter 的第一个字符展开成小写字母
    • ${parameter^^} 把 parameter 的值全部转换成大写字母
    • ${parameter^} 仅仅把 parameter 的第一个字符转换成大写字母
    • ${parameter#pattern} ${parameter##pattern}, ${parameter%pattern} ${parameter%%pattern}: 从 parameter 所包含的字符串中清除开头/末尾一部分文本
    • ${parameter/pattern/string}, ${parameter//pattern/string}, ${parameter/#pattern/string}, ${parameter/%pattern/string}: replace
foo=file.txt.zip\necho ${foo#*.}\ntxt.zip\necho ${foo##*.}\nzip\n\nfoo=file.txt.zip\necho ${foo%.*}\nfile.txt\necho ${foo%%.*}\nfile\n
1
2
3
4
5
6
7
8
9
10
11
foo=JPG.JPG\necho ${foo/JPG/jpg}\njpg.JPG\necho ${foo//JPG/jpg}\njpg.jpg\necho ${foo/#JPG/jpg}\njpg.JPG\necho ${foo/%JPG/jpg}\nJPG.jpg\n
1
2
3
4
5
6
7
8
9
  • $(): command result
  • "": allow expansions string
  • '': disallow expansions string

流程控制语句

if 语句

if [[ 条件判断式 ]] ; then\n    程序\nfi\nif [[ 条件判断式 ]]\n    then\n        程序\n    else\n        程序\nfi\n\nif [[ 条件判断式1 ]]\n    then\n        程序1\nelif [[ 条件判断式2 ]]\n    then\n        程序2\n……\nelse\n        程序n\nfi\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

case 语句

case $变量名 in\n    “值1”)\n            程序\n            ;;\n    “值2”)\n            程序\n            ;;\n    *)\n            程序\n;;\nesac\n
1
2
3
4
5
6
7
8
9
10
11
case patternfunction
a)word equals "a"
[[:alpha:]])word is a single alphabetic character
???)word is exactly three characters long
\\*.txt)word ends with the characters “.txt”
*)any value of word
#!/bin/bash\n# case-menu: a menu driven system information program\nclear\necho "\nPlease Select:\n1. Display System Information\n2. Display Disk Space\n3. Display Home Space Utilization\n0. Quit\n"\nread -p "Enter selection [0-3] > "\ncase $REPLY in\n    0)  echo "Program terminated."\n        exit\n        ;;\n    1)  echo "Hostname: $HOSTNAME"\n        uptime\n        ;;\n    2)  df -h\n        ;;\n    3)  if [[ $(id -u) -eq 0 ]]; then\n            echo "Home Space Utilization (All Users)"\n            du -sh /home/*\n        else\n            echo "Home Space Utilization ($USER)"\n            du -sh $HOME\n        fi\n        ;;\n    *)  echo "Invalid entry" >&2\n        exit 1\n        ;;\nesac\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

or case pattern

#!/bin/bash\n# case-menu: a menu driven system information program\nclear\necho "\nPlease Select:\nA. Display System Information\nB. Display Disk Space\nC. Display Home Space Utilization\nQ. Quit\n"\nread -p "Enter selection [A, B, C or Q] > "\ncase $REPLY in\nq|Q) echo "Program terminated."\n     exit\n     ;;\na|A) echo "Hostname: $HOSTNAME"\n     uptime\n     ;;\nb|B) df -h\n     ;;\nc|C) if [[ $(id -u) -eq 0 ]]; then\n         echo "Home Space Utilization (All Users)"\n         du -sh /home/*\n     else\n         echo "Home Space Utilization ($USER)"\n         du -sh $HOME\n     fi\n     ;;\n*)   echo "Invalid entry" >&2\n     exit 1\n     ;;\nesac\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

fall through case pattern (;;&)

#!/bin/bash\n# case4-2: test a character\nread -n 1 -p "Type a character > "\necho\ncase $REPLY in\n    [[:upper:]])    echo "'$REPLY' is upper case." ;;&\n    [[:lower:]])    echo "'$REPLY' is lower case." ;;&\n    [[:alpha:]])    echo "'$REPLY' is alphabetic." ;;&\n    [[:digit:]])    echo "'$REPLY' is a digit." ;;&\n    [[:graph:]])    echo "'$REPLY' is a visible character." ;;&\n    [[:punct:]])    echo "'$REPLY' is a punctuation symbol." ;;&\n    [[:space:]])    echo "'$REPLY' is a whitespace character." ;;&\n    [[:xdigit:]])   echo "'$REPLY' is a hexadecimal digit." ;;&\nesac\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

for 语句

for 变量 in 值1 值2 值3 …… 值n\n    do\n        程序\n    done\n\n$(seq 1 50)  # 1 2 ... 50\n{1..50}     # 1 2 ... 50\n{0..10..2}  # 0 2 4 6 8 10\n\nfor (( 初始值;循环控制条件;变量变化 )); do\n    程序\ndone\n
1
2
3
4
5
6
7
8
9
10
11
12

while 语句与 until 语句

while [[ 条件判断式 ]]\n    do\n        程序\n    done\n\nuntil [[ 条件判断式 ]]\n    do\n        程序\n    done\n
1
2
3
4
5
6
7
8
9
(( expression1 ))\nwhile (( expression2 )); do\n    commands\n    (( expression3 ))\ndone\n
1
2
3
4
5
#!/bin/bash\n# while-menu: a menu driven system information program\nDELAY=3 # Number of seconds to display results\nwhile [[ $REPLY != 0 ]]; do\n    clear\n    cat <<- _EOF_\n        Please Select:\n        1. Display System Information\n        2. Display Disk Space\n        3. Display Home Space Utilization\n        0. Quit\n    _EOF_\n    read -p "Enter selection [0-3] > "\n    if [[ $REPLY =~ ^[0-3]$ ]]; then\n        if [[ $REPLY == 1 ]]; then\n            echo "Hostname: $HOSTNAME"\n            uptime\n            sleep $DELAY\n        fi\n        if [[ $REPLY == 2 ]]; then\n            df -h\n            sleep $DELAY\n        fi\n        if [[ $REPLY == 3 ]]; then\n            if [[ $(id -u) -eq 0 ]]; then\n                echo "Home Space Utilization (All Users)"\n                du -sh /home/*\n            else\n                echo "Home Space Utilization ($USER)"\n                du -sh $HOME\n            fi\n            sleep $DELAY\n        fi\n    else\n        echo "Invalid entry."\n        sleep $DELAY\n    fi\ndone\necho "Program terminated."\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash\n# while-read: read lines from a file\nwhile read dist version release; do\n    printf "Dist: %s\\tVersion: %s\\tReleased: %s\\n" \\\n        $dist \\\n        $version \\\n        $release\ndone < dist.txt\n
1
2
3
4
5
6
7
8

do while statement

while : ; do\n    actions\n    [[ current_time <= $cutoff ]] || break\ndone\n
1
2
3
4

Bash Array

Bash Function

  • 函数局部变量 local + 变量名
  • 函数参数 : $ + #/?/@/n
  • 引用函数库文件 —— source sh 文件名 / . sh 文件名 可修改~/.bashrc 文件

Bash IO

#!/bin/bash\n# read-validate: validate input\ninvalid_input () {\n    echo "Invalid input '$REPLY'" >&2\n    exit 1\n}\nread -p "Enter a single item > "\n# input is empty (invalid)\n[[ -z $REPLY ]] && invalid_input\n# input is multiple items (invalid)\n(( $(echo $REPLY | wc -w) > 1 )) && invalid_input\n# is input a valid filename?\nif [[ $REPLY =~ ^[-[:alnum:]\\._]+$ ]]; then\n    echo "'$REPLY' is a valid filename."\n    if [[ -e $REPLY ]]; then\n        echo "And file '$REPLY' exists."\n    else\n        echo "However, file '$REPLY' does not exist."\n    fi\n    # is input a floating point number?\n    if [[ $REPLY =~ ^-?[[:digit:]]*\\.[[:digit:]]+$ ]]; then\n        echo "'$REPLY' is a floating point number."\n    else\n        echo "'$REPLY' is not a floating point number."\n    fi\n    # is input an integer?\n    if [[ $REPLY =~ ^-?[[:digit:]]+$ ]]; then\n        echo "'$REPLY' is an integer."\n    else\n        echo "'$REPLY' is not an integer."\n    fi\nelse\n    echo "The string '$REPLY' is not a valid filename."\nfi\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash\n# read-menu: a menu driven system information program\nclear\necho "\nPlease Select:\n\n    1. Display System Information\n    2. Display Disk Space\n    3. Display Home Space Utilization\n    0. Quit\n"\nread -p "Enter selection [0-3] > "\n\nif [[ $REPLY =~ ^[0-3]$ ]]; then\n    if [[ $REPLY == 0 ]]; then\n        echo "Program terminated."\n        exit\n    fi\n    if [[ $REPLY == 1 ]]; then\n        echo "Hostname: $HOSTNAME"\n        uptime\n        exit\n    fi\n    if [[ $REPLY == 2 ]]; then\n        df -h\n        exit\n    fi\n    if [[ $REPLY == 3 ]]; then\n        if [[ $(id -u) -eq 0 ]]; then\n            echo "Home Space Utilization (All Users)"\n            du -sh /home/*\n        else\n            echo "Home Space Utilization ($USER)"\n            du -sh $HOME\n        fi\n        exit\n    fi\nelse\n    echo "Invalid entry." >&2\n    exit 1\nfi\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# interactive mode\nif [[ -n $interactive ]]; then\n    while true; do\n        read -p "Enter name of output file: " filename\n        if [[ -e $filename ]]; then\n            read -p "'$filename' exists. Overwrite? [y/n/q] > "\n            case $REPLY in\n            Y|y)    break\n                    ;;\n            Q|q)    echo "Program terminated."\n                    exit\n                    ;;\n            *)      continue\n                    ;;\n            esac\n        elif [[ -z $filename ]]; then\n            continue\n        else\n            break\n        fi\n    done\nfi\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

信号

  • man 7 signal —— 传递信号给进程
  • CTRL + C —— SIGINT
  • CTRL + Z —— SIGSTP
  • kill/killall —— SIGINT
  • trap SIG*/EXIT —— 捕捉信号(后 + 忽略信号/默认处理信号/自定义处理信号)
  • trap – SIG*/EXIT —— 移除信号

Bash Debugging

-x option

#!/bin/bash -x\n# trouble: script to demonstrate common errors\nnumber=1\nif [ $number = 1 ]; then\n    echo "Number is equal to 1."\nelse\n    echo "Number is not equal to 1."\nfi\n
1
2
3
4
5
6
7
8
#!/bin/bash\n# trouble: script to demonstrate common errors\nnumber=1\necho "number=$number" # DEBUG\nset -x # Turn on tracing\nif [ $number = 1 ]; then\n    echo "Number is equal to 1."\nelse\n    echo "Number is not equal to 1."\nfi\nset +x # Turn off tracing\n
1
2
3
4
5
6
7
8
9
10
11

Shell Script Best Practices

  • Google Shell Style Guide
  • 指定默认解释器cat /etc/shells
  • 设置bash strict mode
  • 条件判断使用[[ ]]
  • 使用变量时用双引号把变量包起来 cp -r "$src_dir" "$dest_dir"
  • 使用$()获取表达式的值
  • 使用${arr[@]}进行列表循环
#!/usr/bin/env bash\n\n# 设置命令回显\nset -x\n\n# 遇到未声明的变量则报错停止\nset -u\n# 遇到执行错误则停止\nset -e\n# 管道命令其中一步失败则中止\nset -o pipefail\n
1
2
3
4
5
6
7
8
9
10
11

Interactive Shell Script Tips

Check Root Validation

if (( $EUID != 0 )); then\n    echo "Please run as root!"\n    exit\nfi\n
1
2
3
4
# run as root directly\nsudo chown root <filename>\nsudo chmod +s <filename>\n
1
2
3

Bash help option

#!/bin/sh\nif [[ ${#@} -ne 0 ]] && [[ "${@#"--help"}" = "" ]]; then\n  printf -- '...help...\\n';\n  exit 0;\nfi;\n
1
2
3
4
5

Bash Silent Option

#!/bin/sh\nif [[ ${#@} -ne 0 ]] && [[ "${@#"--silent"}" = "" ]]; then\n  stty -echo;\nfi;\n# ...\n# before point of intended output:\nstty +echo && printf -- 'intended output\\n';\n# silence it again till end of script\nstty -echo;\n# ...\nstty +echo;\nexit 0;\n
1
2
3
4
5
6
7
8
9
10
11
12

Check Command Validation

#!/bin/sh\n_=$(command -v docker);\nif [[ "$?" != "0" ]]; then\n  printf -- 'You don\\'t seem to have Docker installed.\\n';\n  printf -- 'Get it: https://www.docker.com/community-edition\\n';\n  printf -- 'Exiting with code 127...\\n';\n  exit 127;\nfi;\n
1
2
3
4
5
6
7
8
Get Absolute Path
#!/bin/sh\nCUR_DIR="$(dirname $0);"\nprintf -- 'moving application to /opt/app.jar';\nmv "${CUR_DIR}/application.jar" /opt/app.jar;\n
1
2
3
4

Bash error handle

#!/bin/sh\nerror_handle() {\n  stty echo;\n}\n\nif [[ ${#@} -ne 0 ]] && [[ "${@#"--silent"}" = "" ]]; then\n  stty -echo;\n  trap error_handle INT;\n  trap error_handle TERM;\n  trap error_handle KILL;\n  trap error_handle EXIT;\nfi;\n# ...\n
1
2
3
4
5
6
7
8
9
10
11
12
13

Bash loading progress

#!/bin/sh\nprintf -- 'Performing asynchronous action..';\n./trigger-action;\nDONE=0;\nwhile [ $DONE -eq 0 ]; do\n  ./async-checker;\n  if [ "$?" = "0" ]; then DONE=1; fi;\n  printf -- '.';\n  sleep 1;\ndone;\nprintf -- ' DONE!\\n';\n
1
2
3
4
5
6
7
8
9
10
11

Terminal

Terminal Basis

  • 电传打字机 (TeleTypeWriter, TTY) 是物理设备, 最初是为电报设计, 后来被连接到计算机上, 发送输入和获取输出.
  • 电传打字机 (TTY) 现在被运行在内核中的模块所模拟, 被称为终端模拟器 (Terminal Emulator).
  • 伪终端 (Pseudo Terminal, PTY) 是运行在用户区的终端模拟程序.
  • Shell 由 Terminal fork 出来, 是 Terminal 的子进程. Terminal 处理键盘事件, 负责字符的显示. Shell 负责解释执行用户输入的字符, 返回操作系统底层响应.
  • 可以使用 stty 命令对 TTY 设备进行配置.
  • 远程终端 ssh 也是一种伪终端 PTY:
    • Local: PTY Master is Terminal, PTY Slave is bash and ssh client.
    • Remote: PTY Master is ssh server, PTY Slave is bash.

Default Terminal

sudo update-alternatives --install /usr/bin/x-terminal-emulator\n \\ x-terminal-emulator /opt/Hyper/hyper 50\n
1
2
sudo update-alternatives --config x-terminal-emulator\n
1

Tmux

Basic Tmux Command

tmux ls\ntmux new -s sessionID\ntmux a -t sessionID # attach\ntmux show -g >> current.tmux.conf # export configuration\n
1
2
3
4

Basic Hotkeys

?        # 快捷键帮助列表\n
1
Session management
:new<CR> # 创建新的 Session,其中 : 是进入 Tmux 命令行的快捷键\ns list sessions\n$ rename the current session\nd detach from the current session\n
1
2
3
4
Tmux Windows Hotkeys
c create a new window\n, rename the current window\nw list windows\n% split horizontally\n" split vertically\nn change to the next window\np change to the previous window\n0 to 9 select windows 0 through 9\n
1
2
3
4
5
6
7
8
Tmux Panes Hotkeys
% create a horizontal pane\n" create a vertical pane\n<space>  # 切换 Pane 布局\nh move to the left pane. *\nj move to the pane below *\nl move to the right pane *\nk move to the pane above *\nq show pane numbers\no toggle between panes\n} swap with next pane\n{ swap with previous pane\n! break the pane out of the window\nx kill the current pane\nt        # 显示一个时钟\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Tmux Scroll
  • C-a + [ to into scroll mode, q to quit scroll mode
  • copy mode can scroll too
  • set -g mouse on for enabling mouse scrolling

Configuration

# C-b is not acceptable -- Vim uses it\nset-option -g prefix C-a\nbind-key C-a last-window\n\n# Start numbering at 1\nset -g base-index 1\n\n# Allows for faster key repetition\nset -s escape-time 0\n\n# Set status bar\nset -g status-bg black\nset -g status-fg white\nset -g status-left ""\nset -g status-right "#[fg=green]#H"\n\n# Enable scroll mouse\nset -g mouse on\n\n# Rather than constraining window size to the maximum size of any client\n# connected to the *session*, constrain window size to the maximum size of any\n# client connected to *that window*. Much more reasonable.\nsetw -g aggressive-resize on\n\n# Allows us to use C-a a <command> to send commands to a TMUX session inside\n# another TMUX session\nbind-key a send-prefix\n\n# Reload configuration\nbind r source-file ~/.tmux.conf \\; display-message "Config reloaded"\n\n# Escape to enter copy mode, v to selection, y to yank, p to paste\nbind Escape copy-mode\nbind-key -T copy-mode-vi 'v' send -X begin-selection\nbind-key -T copy-mode-vi 'y' send -X copy-selection-and-cancel\n# bind-key -t vi-copy v begin-selection\n# bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"\nunbind p\nbind p pasteb\n\nsetw -g mode-keys vi      # Vi\n\n# Highlight active window\nset-window-option -g window-status-current-bg red\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

Perf Tools

Top Command

top\n
1

dmesg

dmesg | tail\n
1

iostat

iostat -xz 1\n
1

free

free -m\n
1

sar

`sar -n DEV 1`\n
1
`sar -n TCP,ETCP 1`\n
1

perf

Perf Examples

perf list # events\nperf stat <command>\nperf stat -e <events> <command>\nperf record -e <events> -a <command>\nperf report\n
1
2
3
4
5

Linux Tools

FFmpeg

# https://www.yanxurui.cc/posts/tool/2017-10-07-use-ffmpeg-to-edit-video\nffmpeg -global_options -input_1_options -i input_1 -input_2_options -i input_2 \\\n  -output_1_options output_1 ...\n
1
2
3

FFmpeg Probe

ffprobe input.mp4\nffmpeg -hide_banner -i input.mkv\n
1
2

FFmpeg Transform

  • MP4: H264Video + ACCAudio
  • WebM: VP8Video + VorbisAudio
  • OGG: TheoraVideo + VorbisAudio
# code decoder information\nffmpeg -codecs\n
1
2
# mkv to mp4\nffmpeg -i input.mkv -codec copy output.mp4\n
1
2
# compress\nffmpeg -i input.mkv -c copy -c:v libx264 -vf scale=-2:720 output.mkv\n
1
2
# make mkv with video and subtitle\nffmpeg -i input.avi -i input.srt \\\n  -map 0:0 -map 0:1 -map 1:0 -c:v libx264 -c:a aac -c:s srt output.mkv\n
1
2
3
# flac to mp3\nffmpeg -i "Michael Jackson - Billie Jean.flac" \\\n  -ab 320k "Michael Jackson - Billie Jean.mp3"\nffmpeg -i music_flac.flac \\\n  -acodec libmp3lame      \\\n  -ar 44100               \\\n  -ab 320k                \\\n  -ac 2 music_flac_mp3.mp3\n# - acodec: Audio Coder Decoder 音频编码解码器\n# - libmp3lame: MP3 解码器\n# - ar: audio rate 音频采样率, 默认用原音频的采样率\n# - ab: audio bit rate 音频比特率, 默认 128K\n# - ac: audio channels 音频声道, 默认采用源音频的声道数\n
1
2
3
4
5
6
7
8
9
10
11
12
13
# mp4 to avi\nffmpeg -i video.mp4 \\\n  -s 1920x1080      \\\n  -pix_fmt yuv420p  \\\n  -vcodec libx264   \\\n  -preset medium    \\\n  -profile:v high   \\\n  -level:v 4.1      \\\n  -crf 23           \\\n  -r 30             \\\n  -acodec aac       \\\n  -ar 44100         \\\n  -ac 2             \\\n  -b:a 128k video_avi.avi\n# - s: 缩放视频新尺寸 (size)\n# - pix_fmt:pixel format, 设置视频颜色空间\n# - vcodec: Video Coder Decoder, 视频编码解码器\n# - preset: 编码器预设\n# - profile:v: 编码器配置, 与压缩比有关. 实时通讯-baseline, 流媒体-main, 超清视频-high\n# - level:v: 对编码器设置的具体规范和限制, 权衡压缩比和画质\n# - crf: 设置码率控制模式, constant rate factor恒定速率因子模式, 范围 0~51, 数值越小, 画质越高\n# - r:设置视频帧率\n# - b:a: 音频比特率, 大多数网站限制音频比特率 128k, 129k\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

FFmpeg Cutting

# audio only\nffmpeg -i cut.mp4 -vn output.mp3\nffmpeg -i video.mp4 -vn -acodec copy video_noVideo.m4a\n\n# video only\nffmpeg -i video.mp4 -vcodec copy -an video_silent.mp4\n
1
2
3
4
5
6
# from to cutting\nffmpeg -i music.mp3 -ss 00:00:30 -to 00:02:00 -acodec copy music_cutout.mp3\nffmpeg -i in.mp4 -ss 00:01:00 -to 00:01:10 -c copy out.mp4\nffmpeg -ss 00:01:00 -i in.mp4 -to 00:01:10 -c copy -copyts out.mp4\n\n# 30s duration cutting\nffmpeg -ss 00:02:00.0 -i input.mkv -t 30 -c copy output.mkv\nffmpeg -i input.mkv -ss 00:02:00.0 -t 30 -c copy output.mkv\nffmpeg -ss 00:01:30.0 -i input.mkv -ss 00:00:30.0 -t 30 output.mkv\n
1
2
3
4
5
6
7
8
9

FFmpeg Mixing

# replace audio\nffmpeg -i input.mkv -i input.mp3 -map 0:v -map 1:a -c copy -shortest output.mp4\n\n# merge audio and video\nffmpeg -i video_noVideo.m4a -i video_silent.mp4 -c copy video_merge.mp4\n\nffmpeg -i "concat:01.mp4|02.mp4|03.mp4" -c copy out.mp4\n\nffmpeg -i input.mkv -i output.aac \\\n  -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map 0:v -map "[a]" \\\n  -c:v copy -c:a aac -ac 2 -shortest output.mp4\n
1
2
3
4
5
6
7
8
9
10
11

FFmpeg Screenshot

# -vf -> -filter:v\nffmpeg -ss 00:30:14.435 -i input.mkv -vframes 1 out.png\nffmpeg -i input.mkv -vf fps=1/60 -strftime 1 out_%Y%m%d%H%M%S.jpg\n
1
2
3

FFmpeg Gif

ffmpeg -i video.mp4 -ss 7.5 -to 8.5 -s 640x320 -r 15 video_gif.gif\n\npalette="/tmp/palette.png"\nfilters="fps=10,scale=-1:144:flags=lanczos"\nffmpeg -ss 30 -t 5 -i input.mp4 -vf "$filters,palettegen" -y $palette\nffmpeg -ss 30 -t 5 -i input.mp4 -i $palette \\\n  -filter_complex "$filters [x]; [x][1:v] paletteuse" -y output.gif\n
1
2
3
4
5
6
7

FFmpeg Subtitle

ffmpeg -i input.mkv -vf subtitles=input.srt output.mp4\nffmpeg -i input.mkv -vf ass=input.ass output.mp4\n
1
2

FFmpeg Watermark

ffmpeg -i input.mkv -i input.png \\\n  -filter_complex "overlay=W-w-5:5" -c copy -c:v libx264 output.mkv\n
1
2

FFmpeg Desktop Recording

# windows\nffmpeg -f gdigrab -i desktop rec.mp4\n\n# linux\nsudo ffmpeg -f fbdev -framerate 10 -i /dev/fb0 rec.mp4\n
1
2
3
4
5

FFmpeg Live Streaming

ffmpeg -re i rec.mp4 按照网站要求编码 -f flv "你的 RTMP 地址/你的直播码"\n
1

Nginx

  • /etc/nginx/sites-available: sites config
nginx -t # check config syntax\n
1

Nginx Basic Configuration

泛域名路径分离: xxx.test.dev -> /usr/local/html/xxx

server {\n  listen 80;\n  server_name ~^([\\w-]+)\\.test\\.dev$;\n  root /usr/local/html/$1;\n}\n
1
2
3
4
5
server {\n  # SSL configuration\n  #\n  # listen 443 ssl default_server;\n  # listen [::]:443 ssl default_server;\n  #\n  root /var/www/html/;\n\n  # Add index.php to the list if you are using PHP\n  index index.html index.htm index.nginx-debian.html;\n\n  server_name example.tld www.example.tld;\n\n  # Cache static assets\n  location ~* \\.(?:jpg|jpeg|gif|png|ico|svg)$ {\n    expires 7d;\n    add_header Cache-Control "public";\n  }\n\n  location ^~ /assets/ {\n    gzip_static on;\n    expires 12h;\n    add_header Cache-Control "public";\n  }\n\n  # Cache css and js bundle\n  location ~* \\.(?:css|js)$ {\n    add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";\n  }\n\n  location / {\n    include /etc/nginx/mime.types;\n    try_files $uri $uri/ /index.html;\n    # try_files $uri $uri/ =404;\n\n    # proxy_http_version 1.1;\n    # proxy_cache_bypass $http_upgrade;\n    # proxy_set_header Upgrade $http_upgrade;\n    # proxy_set_header Connection 'upgrade';\n    # proxy_set_header Host $host;\n    # proxy_set_header X-Real-IP $remote_addr;\n    # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n    # proxy_set_header X-Forwarded-Proto $scheme;\n    # proxy_pass http://localhost:3000;\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

Namespaces and Cgroup

Namespaces

# PID Namespace\nunshare --fork --pid --mount-proc /bin/bash\nps -aux\n\n# Mount Namespace\nunshare --fork --mount /bin/bash\nmkdir /tmp/mnt\nmount -t tmpfs -o size=1m tmpfs /tmp/mnt\ndf -h |grep mnt\n\n# User Namespace\nPS1='\\u@container#' unshare --user -r /bin/bash\n\n# UTS Namespace (isolated hostname)\nunshare --fork --uts /bin/bash\nhostname -b container\n\n# IPC Namespace\nunshare --fork --ipc /bin/bash\nipcmk -Q\nipcs -q\n\n# Net Namespace\nunshare --fork --net /bin/bash\nip addr\nnetstat -ntlp\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

Cgroup

Cgroup (Linux Control Group): limit process group resources usage, including CPU, Memory, Disk I/O, Network Bandwidth etc.

  • Resource usage limit.
  • Priority.
  • Resource record.
  • Process Control.

List cgroup

mount -t cgroup\nls -l /sys/fs/cgroup/\n
1
2

Create cgroup

mkdir /sys/fs/cgroup/cpu/loop\nls -l /sys/fs/cgroup/cpu/loop\ncat /sys/fs/cgroup/cpu/loop/cpu.cfs_period_us # 100000us\ncat /sys/fs/cgroup/cpu/loop/cpu.cfs_quota_us  # -1 (no limit)\n
1
2
3
4

Resource control via cgroup

# limit cpu usage to 50%\necho 50000 >/sys/fs/cgroup/cpu/loop/cpu.cfs_quota_us\n\n# add pid to `loop` cgroup\necho 21497 >/sys/fs/cgroup/cpu/loop/tasks\n
1
2
3
4
5

Docker

Docker Installation

 sudo apt-get update\n sudo apt-get install \\\n  apt-transport-https \\\n  ca-certificates \\\n  curl \\\n  gnupg \\\n  lsb-release\ncurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -\nsudo add-apt-repository \\\n  "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"\nsudo apt update\nsudo apt install docker-ce docker-ce-cli containerd.io\n
1
2
3
4
5
6
7
8
9
10
11
12
curl -fsSL https://get.docker.com -o get-docker.sh\nsudo DRY_RUN=1 sh get-docker.sh\n
1
2
sudo systemctl status docker\nsudo usermod -aG docker $USER\n
1
2

Docker Uninstallation

docker container stop $(docker container ls -aq)\ndocker system prune -a --volumes\nsudo apt purge docker-ce docker-ce-cli containerd.io\nsudo apt autoremove\nsudo rm -rf /var/lib/docker\nsudo rm -rf /var/lib/containerd\n
1
2
3
4
5
6

Docker Basic Usage

Build Docker Image
docker build . -t image-name\n\n# use Dockerfile at the root of the repository\ndocker build <github-repo-url> -t image-name\n
1
2
3
4
Run Docker Image
# docker run -dp <host-port>:<container-port> [docker-image]\ndocker run -d -p 80:80 --name app-name docker/getting-started\ndocker run -d -p 80:80/tcp -p 80:80/udp --name app-name docker/getting-started\n
1
2
3
',565);p.render=function(a,e,p,t,r,i){return n(),s("div",null,[l])};export default p;export{e as __pageData}; diff --git a/assets/programming_linux_linuxBasicNotes.md.37adc63b.lean.js b/assets/programming_linux_linuxBasicNotes.md.37adc63b.lean.js new file mode 100644 index 00000000000..123ea40c760 --- /dev/null +++ b/assets/programming_linux_linuxBasicNotes.md.37adc63b.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const e='{"title":"Linux Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Linux Boot System","slug":"linux-boot-system"},{"level":3,"title":"Grub","slug":"grub"},{"level":3,"title":"重装 Linux","slug":"重装-linux"},{"level":2,"title":"SSH 命令","slug":"ssh-命令"},{"level":3,"title":"Key","slug":"key"},{"level":3,"title":"SSHD","slug":"sshd"},{"level":3,"title":"SSH Config File","slug":"ssh-config-file"},{"level":3,"title":"密钥文件","slug":"密钥文件"},{"level":3,"title":"远程传输文件","slug":"远程传输文件"},{"level":2,"title":"命令优先级","slug":"命令优先级"},{"level":2,"title":"Linux 文件架构","slug":"linux-文件架构"},{"level":2,"title":"Ubuntu","slug":"ubuntu"},{"level":3,"title":"Ubuntu Locale Settings","slug":"ubuntu-locale-settings"},{"level":3,"title":"Ubuntu Themes","slug":"ubuntu-themes"},{"level":2,"title":"Arch Linux","slug":"arch-linux"},{"level":3,"title":"Basic Arch Linux Setup","slug":"basic-arch-linux-setup"},{"level":3,"title":"Pacman","slug":"pacman"},{"level":3,"title":"AUR","slug":"aur"},{"level":2,"title":"基本处理命令","slug":"基本处理命令"},{"level":3,"title":"ls","slug":"ls"},{"level":3,"title":"cd","slug":"cd"},{"level":3,"title":"pwd","slug":"pwd"},{"level":3,"title":"rm","slug":"rm"},{"level":3,"title":"cp","slug":"cp"},{"level":3,"title":"mv","slug":"mv"},{"level":3,"title":"ln","slug":"ln"},{"level":2,"title":"基本搜索命令","slug":"基本搜索命令"},{"level":3,"title":"locate","slug":"locate"},{"level":3,"title":"type","slug":"type"},{"level":3,"title":"apropos","slug":"apropos"},{"level":3,"title":"whereis and whatis","slug":"whereis-and-whatis"},{"level":3,"title":"which","slug":"which"},{"level":3,"title":"find","slug":"find"},{"level":3,"title":"Grep","slug":"grep"},{"level":2,"title":"Process Command","slug":"process-command"},{"level":3,"title":"uptime","slug":"uptime"},{"level":3,"title":"ps","slug":"ps"},{"level":3,"title":"top","slug":"top"},{"level":3,"title":"vmstat","slug":"vmstat"},{"level":3,"title":"pidstat","slug":"pidstat"},{"level":3,"title":"mpstat","slug":"mpstat"},{"level":3,"title":"lscpu","slug":"lscpu"},{"level":3,"title":"jobs","slug":"jobs"},{"level":3,"title":"bg","slug":"bg"},{"level":3,"title":"fg","slug":"fg"},{"level":3,"title":"kill","slug":"kill"},{"level":3,"title":"killall","slug":"killall"},{"level":3,"title":"shutdown","slug":"shutdown"},{"level":3,"title":"pstree","slug":"pstree"},{"level":3,"title":"xload and tload","slug":"xload-and-tload"},{"level":3,"title":"screen","slug":"screen"},{"level":2,"title":"CLI Input Output Command","slug":"cli-input-output-command"},{"level":3,"title":"cat","slug":"cat"},{"level":3,"title":"sort","slug":"sort"},{"level":3,"title":"uniq","slug":"uniq"},{"level":3,"title":"wc","slug":"wc"},{"level":3,"title":"Head and Tail","slug":"head-and-tail"},{"level":3,"title":"Tee","slug":"tee"},{"level":3,"title":"nl","slug":"nl"},{"level":3,"title":"fold","slug":"fold"},{"level":3,"title":"fmt","slug":"fmt"},{"level":3,"title":"pr","slug":"pr"},{"level":3,"title":"printf","slug":"printf"},{"level":2,"title":"帮助命令","slug":"帮助命令"},{"level":3,"title":"man","slug":"man"},{"level":3,"title":"help shell 内部命令","slug":"help-shell-内部命令"},{"level":3,"title":"info","slug":"info"},{"level":3,"title":"System Info","slug":"system-info"},{"level":2,"title":"压缩命令","slug":"压缩命令"},{"level":3,"title":"Zip","slug":"zip"},{"level":3,"title":"Gz","slug":"gz"},{"level":3,"title":"Bz2","slug":"bz2"},{"level":3,"title":"Tar","slug":"tar"},{"level":3,"title":"7z","slug":"_7z"},{"level":2,"title":"User and Group Command","slug":"user-and-group-command"},{"level":3,"title":"组操作","slug":"组操作"},{"level":3,"title":"用户操作","slug":"用户操作"},{"level":2,"title":"权限管理命令","slug":"权限管理命令"},{"level":3,"title":"普通权限","slug":"普通权限"},{"level":3,"title":"ACL 权限","slug":"acl-权限"},{"level":3,"title":"sudo 权限","slug":"sudo-权限"},{"level":3,"title":"SetUID and SetGID","slug":"setuid-and-setgid"},{"level":2,"title":"显示器管理命令","slug":"显示器管理命令"},{"level":3,"title":"xrandr","slug":"xrandr"},{"level":2,"title":"主机信息管理命令","slug":"主机信息管理命令"},{"level":2,"title":"Disk IO","slug":"disk-io"},{"level":3,"title":"挂载命令","slug":"挂载命令"},{"level":3,"title":"修复命令","slug":"修复命令"},{"level":3,"title":"分区命令","slug":"分区命令"},{"level":3,"title":"Zero Copy","slug":"zero-copy"},{"level":2,"title":"Device Command","slug":"device-command"},{"level":3,"title":"Monitor Info","slug":"monitor-info"},{"level":3,"title":"Touch Pad Synoptics","slug":"touch-pad-synoptics"},{"level":2,"title":"包管理命令","slug":"包管理命令"},{"level":3,"title":"rpm 命令","slug":"rpm-命令"},{"level":3,"title":"yum","slug":"yum"},{"level":3,"title":"源码包安装","slug":"源码包安装"},{"level":3,"title":"Applications Management","slug":"applications-management"},{"level":2,"title":"网络连接命令","slug":"网络连接命令"},{"level":3,"title":"wget","slug":"wget"},{"level":3,"title":"Certificate Bot","slug":"certificate-bot"},{"level":3,"title":"GFW","slug":"gfw"},{"level":2,"title":"网络管理命令","slug":"网络管理命令"},{"level":3,"title":"ufw","slug":"ufw"},{"level":3,"title":"arp","slug":"arp"},{"level":3,"title":"netstat","slug":"netstat"},{"level":3,"title":"nslookup","slug":"nslookup"},{"level":3,"title":"ping","slug":"ping"},{"level":3,"title":"telnet","slug":"telnet"},{"level":3,"title":"Trace Route","slug":"trace-route"},{"level":3,"title":"Net Filter Framework","slug":"net-filter-framework"},{"level":2,"title":"网络扫描命令","slug":"网络扫描命令"},{"level":3,"title":"fping","slug":"fping"},{"level":3,"title":"hping","slug":"hping"},{"level":3,"title":"mtr","slug":"mtr"},{"level":3,"title":"nmap","slug":"nmap"},{"level":3,"title":"ncat","slug":"ncat"},{"level":2,"title":"脚本运行命令","slug":"脚本运行命令"},{"level":3,"title":"Systemctl","slug":"systemctl"},{"level":3,"title":"定时任务","slug":"定时任务"},{"level":3,"title":"后台任务","slug":"后台任务"},{"level":3,"title":"开机任务","slug":"开机任务"},{"level":2,"title":"历史记录命令","slug":"历史记录命令"},{"level":3,"title":"history","slug":"history"},{"level":3,"title":"ctrl-r","slug":"ctrl-r"},{"level":3,"title":"History Shortcuts","slug":"history-shortcuts"},{"level":2,"title":"并行命令","slug":"并行命令"},{"level":2,"title":"C/C++ Binary Command","slug":"c-c-binary-command"},{"level":3,"title":"ldd","slug":"ldd"},{"level":3,"title":"nm","slug":"nm"},{"level":2,"title":"Plot Command","slug":"plot-command"},{"level":2,"title":"Other Command","slug":"other-command"},{"level":3,"title":"Time","slug":"time"},{"level":2,"title":"Shell 编程","slug":"shell-编程"},{"level":3,"title":"Shell Warnings","slug":"shell-warnings"},{"level":3,"title":"文件重定向","slug":"文件重定向"},{"level":3,"title":"变量","slug":"变量"},{"level":3,"title":"数值运算","slug":"数值运算"},{"level":3,"title":"Bash Expansions","slug":"bash-expansions"},{"level":3,"title":"流程控制语句","slug":"流程控制语句"},{"level":3,"title":"Bash Array","slug":"bash-array"},{"level":3,"title":"Bash Function","slug":"bash-function"},{"level":3,"title":"Bash IO","slug":"bash-io"},{"level":3,"title":"信号","slug":"信号"},{"level":3,"title":"Bash Debugging","slug":"bash-debugging"},{"level":3,"title":"Shell Script Best Practices","slug":"shell-script-best-practices"},{"level":3,"title":"Interactive Shell Script Tips","slug":"interactive-shell-script-tips"},{"level":2,"title":"Terminal","slug":"terminal"},{"level":3,"title":"Terminal Basis","slug":"terminal-basis"},{"level":3,"title":"Default Terminal","slug":"default-terminal"},{"level":3,"title":"Tmux","slug":"tmux"},{"level":2,"title":"Perf Tools","slug":"perf-tools"},{"level":3,"title":"Top Command","slug":"top-command"},{"level":3,"title":"dmesg","slug":"dmesg"},{"level":3,"title":"iostat","slug":"iostat"},{"level":3,"title":"free","slug":"free"},{"level":3,"title":"sar","slug":"sar"},{"level":3,"title":"perf","slug":"perf"},{"level":2,"title":"Linux Tools","slug":"linux-tools"},{"level":3,"title":"FFmpeg","slug":"ffmpeg"},{"level":3,"title":"Nginx","slug":"nginx"},{"level":3,"title":"Namespaces and Cgroup","slug":"namespaces-and-cgroup"},{"level":3,"title":"Docker","slug":"docker"}],"relativePath":"programming/linux/linuxBasicNotes.md","lastUpdated":1627731858000}',p={},l=a('',565);p.render=function(a,e,p,t,r,i){return n(),s("div",null,[l])};export default p;export{e as __pageData}; diff --git a/assets/programming_tools_buildTools_CMakeBasicNotes.md.c21de6b5.js b/assets/programming_tools_buildTools_CMakeBasicNotes.md.c21de6b5.js new file mode 100644 index 00000000000..512ce8d8b7b --- /dev/null +++ b/assets/programming_tools_buildTools_CMakeBasicNotes.md.c21de6b5.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const e='{"title":"CMake Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Build System","slug":"basic-build-system"},{"level":3,"title":"Basic Options","slug":"basic-options"},{"level":2,"title":"Flow Control","slug":"flow-control"},{"level":3,"title":"if control","slug":"if-control"},{"level":3,"title":"foreach control","slug":"foreach-control"},{"level":3,"title":"while control","slug":"while-control"},{"level":3,"title":"function control","slug":"function-control"},{"level":2,"title":"Useful Command","slug":"useful-command"},{"level":3,"title":"Checking Command","slug":"checking-command"},{"level":3,"title":"Testing Command","slug":"testing-command"},{"level":3,"title":"Option Command","slug":"option-command"},{"level":3,"title":"Math Command","slug":"math-command"},{"level":3,"title":"List Command","slug":"list-command"},{"level":3,"title":"Package Command","slug":"package-command"},{"level":3,"title":"Install Command","slug":"install-command"},{"level":3,"title":"find packages","slug":"find-packages"},{"level":2,"title":"Useful Tools","slug":"useful-tools"},{"level":2,"title":"Config Command","slug":"config-command"},{"level":3,"title":"Version Config","slug":"version-config"},{"level":3,"title":"Project Config","slug":"project-config"},{"level":3,"title":"Environment Config","slug":"environment-config"},{"level":3,"title":"Library for Clients Usage","slug":"library-for-clients-usage"},{"level":2,"title":"Test Setting","slug":"test-setting"},{"level":2,"title":"Generator Expression","slug":"generator-expression"},{"level":2,"title":"Submodule and Dependencies","slug":"submodule-and-dependencies"},{"level":2,"title":"CMake Patterns","slug":"cmake-patterns"},{"level":3,"title":"Modern CMake","slug":"modern-cmake"},{"level":3,"title":"Nice Patterns","slug":"nice-patterns"},{"level":3,"title":"Anti Patterns","slug":"anti-patterns"},{"level":2,"title":"Makefile Notes","slug":"makefile-notes"},{"level":3,"title":"Makefile Macro","slug":"makefile-macro"},{"level":3,"title":"Built-in Makefile Variable","slug":"built-in-makefile-variable"},{"level":3,"title":"Makefile Inexplicit Rules","slug":"makefile-inexplicit-rules"},{"level":3,"title":"Makefile Function","slug":"makefile-function"},{"level":3,"title":"Makefile Best Practice","slug":"makefile-best-practice"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"programming/tools/buildTools/CMakeBasicNotes.md","lastUpdated":1627196177000}',p={},t=a('

CMake Basic Notes

Basic Build System

build for executable

cmake_minimum_required(VERSION 2.8.9)\nproject(directory_test)\n\nadd_definitions(-DUSEXX)\nadd_compile_options(-std=c++11 -Wall -Wextra)\n\n# Bring the headers, such as Student.h into the project\ninclude_directories(include)\n\n# Can manually add the sources using the set command as follows:\n# set(SOURCES src/mainApp.cpp src/Student.cpp)\n\n# However, the file(GLOB...) allows for wildcard additions:\nfile(GLOB SOURCES "src/*.cpp")\n\nmessage(STATUS "CMake demo: build for executable")\n\nadd_executable(testStudent ${SOURCES})\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

build for library

cmake_minimum_required(VERSION 2.8.9)\nproject(directory_test)\nset(CMAKE_BUILD_TYPE Release)\n\n# Bring the headers, such as Student.h into the project\ninclude_directories(include)\n\n# However, the file(GLOB...) allows for wildcard additions:\nfile(GLOB SOURCES "src/*.cpp")\n\n# Generate the shared library from the sources\n# flag: SHARED, STATIC, MODULE\nadd_library(testStudent SHARED ${SOURCES})\n\nmessage(STATUS "CMake demo: build for library")\n\n# Set the location for library installation -- i.e., /usr/lib in this case\n# not really necessary in this example. Use "sudo make install" to apply\ninstall(TARGETS testStudent DESTINATION /usr/lib)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

use a shared or static library

cmake_minimum_required(VERSION 2.8.9)\nproject(TestLibrary)\n\n# For the shared library:\nset(PROJECT_LINK_LIBS libTestStudent.so)\nlink_directories(~/exploringBB/extras/cmake/studentLib_shared/build)\n\n# For the static library:\n# set (PROJECT_LINK_LIBS libTestStudent.a)\n# link_directories(~/exploringBB/extras/cmake/studentLib_static/build)\n\ninclude_directories(~/exploringBB/extras/cmake/studentLib_shared/include)\n\nadd_executable(libTest libTest.cpp)\ntarget_link_libraries(libTest ${PROJECT_LINK_LIBS} )\n\nmessage(STATUS "CMake demo: use library")\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

set output of library

# 指定lib输出目录\nset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)\nset(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)\nset(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)\n\n# 指定版本\nset(DEMO5_VERSION_MAJOR 1)\nset(DEMO5_VERSION_MINOR 1)\nset(DEMO5_VERSION_PATCH 1)\nset(DEMO5_VERSION ${DEMO5_VERSION_MAJOR}.${DEMO5_VERSION_MINOR}.${DEMO5_VERSION_PATCH})\n\naux_source_directory(. SRC_LIST)\n\nadd_library(demo5 SHARED ${SRC_LIST})\nset_target_properties(\n    demo5 PROPERTIES\n    VERSION ${DEMO5_VERSION}\n    SOVERSION ${DEMO5_VERSION_MAJOR}\n)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

build for library and executable

cmake_minimum_required(VERSION 3.5)\nproject(MiniSat VERSION 2.2 LANGUAGES CXX)\n\nadd_library(libMiniSat STATIC\n    miniSat/core/Solver.cc\n    miniSat/utils/Options.cc\n    miniSat/utils/System.cc\n    miniSat/simp/SimpSolver.cc\n)\n\ntarget_compile_features(libMiniSat\n    PUBLIC\n      cxx_attributes\n      cxx_defaulted_functions\n      cxx_deleted_functions\n      cxx_final\n)\n\ntarget_include_directories(libMiniSat PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})\n\ntarget_compile_definitions(libMiniSat PUBLIC __STDC_LIMIT_MACROS __STDC_FORMAT_MACROS)\n\n# Also build the two MiniSat executables\nadd_executable(miniSat miniSat/core/Main.cc)\ntarget_link_libraries(miniSat libMiniSat)\n\nadd_executable(miniSat-simp miniSat/simp/Main.cc)\ntarget_link_libraries(miniSat-simp libMiniSat)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

Basic Options

make VERBOSE=1\n
1

Standard options:

  • -DCMAKE_BUILD_TYPE= Pick from Release, RelWithDebInfo, Debug, or sometimes more
  • -DCMAKE_INSTALL_PREFIX= /usr/local (the default), ~/.local
  • -D BUILD_SHARED_LIBS=
  • --trace print every line of CMake

Flow Control

if control

  • Unary: NOT, TARGET, EXISTS (file), DEFINED
  • Binary: STREQUAL, AND, OR, MATCHES(regular expression), VERSION_LESS, VERSION_LESS_EQUAL
if(WIN32)\n    message("This is win32 platform")\nelse()\n    message("This is not win32 platform")\nendif()\n
1
2
3
4
5

foreach control

set(FOR_LIST demo1.cpp demo2.cpp demo3.cpp)\nforeach(f ${FOR_LIST})\n    message("now is file: " ${f})\nendforeach ()\n
1
2
3
4

while control

set(A "1")\nset(B "1")\nwhile(A LESS "1000000")\n    message("${A}")                 # Print A\n    math(EXPR T "${A} + ${B}")      # Add values of A and B; store result in T\n    set(A "${B}")                   # Assign the value of B to A\n    set(B "${T}")                   # Assign the value of T to B\nendwhile()\n
1
2
3
4
5
6
7
8

function control

Basic Usage of Function

function(doubleIt VALUE)\n    math(EXPR RESULT "${VALUE} * 2")\n    message("${RESULT}")\nendfunction()\n\ndoubleIt("4")          # Prints: 8\n
1
2
3
4
5
6
function(doubleIt VARNAME VALUE)\n    math(EXPR RESULT "${VALUE} * 2")\n    set(${VARNAME} "${RESULT}" PARENT_SCOPE)\nendfunction()\n\ndoubleIt(RESULT "4")   # Tell the function to set the variable named RESULT\nmessage("${RESULT}")   # Prints: 8\n
1
2
3
4
5
6
7
function(doubleEach)\n    foreach(ARG ${ARGN})           # Iterate over each argument\n        math(EXPR N "${ARG} * 2")  # Double ARG numeric value\n        message("${N}")            # Print N\n    endforeach()\nendfunction()\n\ndoubleEach(5 6 7 8)                # Prints 10, 12, 14, 16 on separate lines\n
1
2
3
4
5
6
7
8

Parse Arguments of Function

function(COMPLEX)\ncmake_parse_arguments(\n    COMPLEX_PREFIX\n    "SINGLE;ANOTHER"\n    "ONE_VALUE;ALSO_ONE_VALUE"\n    "MULTI_VALUES"\n    ${ARGN}\n)\n\nendfunction()\n\ncomplex(SINGLE ONE_VALUE value MULTI_VALUES some other values)\n
1
2
3
4
5
6
7
8
9
10
11
12

Inside the function after this call, you'll find:

COMPLEX_PREFIX_SINGLE = TRUE\nCOMPLEX_PREFIX_ANOTHER = FALSE\nCOMPLEX_PREFIX_ONE_VALUE = "value"\nCOMPLEX_PREFIX_ALSO_ONE_VALUE = <UNDEFINED>\nCOMPLEX_PREFIX_MULTI_VALUES = "some;other;values"\n
1
2
3
4
5

Useful Command

Checking Command

# does this system provide the log and exp functions?\ninclude(CheckFunctionExists)\ncheck_function_exists(log HAVE_LOG)\ncheck_function_exists(exp HAVE_EXP)\n
1
2
3
4

Testing Command

#define a macro to simplify adding tests, then use it\nmacro(do_test arg result)\n  add_test(TutorialComp${arg} Tutorial ${arg})\n  set_tests_properties(TutorialComp${arg}\n    PROPERTIES PASS_REGULAR_EXPRESSION ${result})\nendmacro(do_test)\n\n# do a bunch of result based tests\ndo_test(25 "25 is 5")\ndo_test(-25 "-25 is 0")\n
1
2
3
4
5
6
7
8
9
10
include(CTest)\n\n# does the application run\nadd_test (TutorialRuns Tutorial 25)\n\n# does it sqrt of 25\nadd_test (TutorialComp25 Tutorial 25)\nset_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")\n\n# does it handle negative numbers\nadd_test (TutorialNegative Tutorial -25)\nset_tests_properties (\n    TutorialNegative PROPERTIES\n    PASS_REGULAR_EXPRESSION "-25 is 0"\n)\n\n# does it handle small numbers\nadd_test (TutorialSmall Tutorial 0.0001)\nset_tests_properties (\n    TutorialSmall PROPERTIES\n    PASS_REGULAR_EXPRESSION "0.0001 is 0.01"\n)\n\n# does the usage message work?\nadd_test (TutorialUsage Tutorial)\nset_tests_properties (\n    TutorialUsage PROPERTIES\n    PASS_REGULAR_EXPRESSION "Usage:.*number"\n)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Option Command

# 是否使用我们自己的函数?\noption(USE_MATH\n        "Use tutorial provided math implementation" ON)\n\n# add the MathFunctions library?\nif(USE_MATH)\n  include_directories("${PROJECT_SOURCE_DIR}/MathFunctions")\n  add_subdirectory(MathFunctions)\n  set(EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)\nendif(USE_MATH)\n\n# add the executable\nadd_executable(Tutorial tutorial.cxx)\ntarget_link_libraries(Tutorial  ${EXTRA_LIBS})\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Math Command

set(ARGS "EXPR;T;1 + 1")\nmath(${ARGS})                      # Equivalent to calling math(EXPR T "1 + 1")\n
1
2

List Command

set(MY_LIST These are separate arguments)\nlist(REMOVE_ITEM MY_LIST "separate")  # Removes "separate" from the list\nmessage("${MY_LIST}")                 # Prints: These;are;arguments\n
1
2
3

Package Command

# build a CPack driven installer package\ninclude(InstallRequiredSystemLibraries)\nset(CPACK_RESOURCE_FILE_LICENSE\n     "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")\nset(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")\nset(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")\ninclude(CPack)\n
1
2
3
4
5
6
7
cpack --config CPackConfig.cmake\ncpack --config CPackSourceConfig.cmake\n
1
2

Install Command

install binaries

INSTALL(TARGETS targets...\n        [[ARCHIVE|LIBRARY|RUNTIME]\n        [DESTINATION < dir >]\n        [PERMISSIONS permissions...]\n        [CONFIGURATIONS\n        [Debug|Release|...]]\n        [COMPONENT < component >]\n        [OPTIONAL]\n        ] [...])\n\nINSTALL(TARGETS myRun myLib myStaticLib\n        RUNTIME DESTINATION bin\n        LIBRARY DESTINATION lib\n        ARCHIVE DESTINATION libStatic)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

install normal files

INSTALL(FILES files... DESTINATION <dir>\n     [PERMISSIONS permissions...]\n     [CONFIGURATIONS [Debug|Release|...]]\n     [COMPONENT <component>]\n     [RENAME <name>] [OPTIONAL])\n\n\nINSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/t2)\n
1
2
3
4
5
6
7
8

install scripts

INSTALL(PROGRAMS files... DESTINATION <dir>\n     [PERMISSIONS permissions...]\n     [CONFIGURATIONS [Debug|Release|...]]\n     [COMPONENT <component>]\n     [RENAME <name>] [OPTIONAL])\n\n\nINSTALL(PROGRAMS runHello.sh DESTINATION bin)\n
1
2
3
4
5
6
7
8

install directories

INSTALL(DIRECTORY dirs... DESTINATION <dir>\n     [FILE_PERMISSIONS permissions...]\n     [DIRECTORY_PERMISSIONS permissions...]\n     [USE_SOURCE_PERMISSIONS]\n     [CONFIGURATIONS [Debug|Release|...]]\n     [COMPONENT <component>]\n     [[PATTERN <pattern> | REGEX <regex>]\n     [EXCLUDE] [PERMISSIONS permissions...]] [...])\n\nINSTALL(DIRECTORY icons scripts/ DESTINATION share/myProj\n     PATTERN "CVS" EXCLUDE\n     PATTERN "scripts/*"\n     PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ\n     GROUP_EXECUTE GROUP_READ)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Install Demo

find_package(Bar 2.0 REQUIRED)\nadd_library(Foo ...)\ntarget_link_libraries(Foo PRIVATE Bar::Bar)\n\ninstall(TARGETS Foo EXPORT FooTargets\n  LIBRARY DESTINATION lib\n  ARCHIVE DESTINATION lib\n  RUNTIME DESTINATION bin\n  INCLUDES DESTINATION include\n)\n\ninstall(EXPORT FooTargets\n  FILE FooTargets.cmake\n  NAMESPACE Foo::\n  DESTINATION lib/cmake/Foo\n)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

find packages

find modules

cmake –-help-module-list\nls /usr/share/cmake/Modules/\n\ncmake --help-module FindBZip2\n
1
2
3
4

Basic Usage of Find

project(helloWorld)\nadd_executable(helloWorld hello.c)\nfind_package (BZip2)\nif (BZIP2_FOUND)\n    include_directories(${BZIP_INCLUDE_DIRS})\n    target_link_libraries(helloWorld ${BZIP2_LIBRARIES})\nendif (BZIP2_FOUND)\n
1
2
3
4
5
6
7
find_path(Foo_INCLUDE_DIR foo.h)\nfind_library(Foo_LIBRARY foo)\nmark_as_advanced(Foo_INCLUDE_DIR Foo_LIBRARY)\n\ninclude(FindPackageHandleStandardArgs)\nfind_package_handle_standard_args(Foo\n  REQUIRED_VARS Foo_LIBRARY Foo_INCLUDE_DIR\n)\n\nif(Foo_FOUND AND NOT TARGET Foo::Foo)\n  add_library(Foo::Foo UNKNOWN IMPORTED)\n  set_target_properties(Foo::Foo PROPERTIES\n    IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"\n    IMPORTED_LOCATION "${Foo_LIBRARY}"\n    INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"\n  )\nendif()\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Find CMake

Find.cmake: add find module for project

# cmake/FindDEMO9LIB.cmake\n# 辅助输出信息\nmessage("now using FindDEMO9LIB.cmake find demo9 lib")\n\n# 将demo9.h文件路径赋值给DEMO9LIB_INCLUDE_DIR\nFIND_PATH(DEMO9LIB_INCLUDE_DIR demo9.h /usr/include/demo9/\n        /usr/local/demo9/include/)\nmessage("./h dir ${DEMO9LIB_INCLUDE_DIR}")\n\n# 将libDemo9_lib.a文件路径赋值给DEMO9LIB_LIBRARY\nFIND_LIBRARY(DEMO9LIB_LIBRARY libDemo9_lib.a /usr/local/demo9/lib/)\nmessage("lib dir: ${DEMO9LIB_LIBRARY}")\n\nif(DEMO9LIB_INCLUDE_DIR AND DEMO9LIB_LIBRARY)\n    # 设置变量结果\n    set(DEMO9LIB_FOUND TRUE)\nendif(DEMO9LIB_INCLUDE_DIR AND DEMO9LIB_LIBRARY)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Full Find Demo

# CMakeLists.txt\ncmake_minimum_required(VERSION 3.5)\n\nproject(demo9)\n\n# create libDemo9_lib.a\nset(SRC_LIB demo9.cpp)\nadd_library(demo9_lib STATIC ${SRC_LIB})\n\n# install it\ninstall(TARGETS demo9_lib DESTINATION demo9/lib)\ninstall(FILES demo9.h DESTINATION demo9/include)\n\n# create demo9_main executable\nset(SRC_EXE demo9_main.cpp)\n\n# set demo9_lib cmake module path\nset(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)\nmessage("cmake_module_path: ${CMAKE_MODULE_PATH}")\nfind_package(DEMO9LIB)\n\nif(DEMO9LIB_FOUND)\n    add_executable(demo9_main ${SRC_EXE})\n    message("found demo9 ${DEMO9LIB_INCLUDE_DIR} ${DEMO9LIB_LIBRARY}")\n    include_directories(${DEMO9LIB_INCLUDE_DIR})\n    target_link_libraries(demo9_main ${DEMO9LIB_LIBRARY})\nelse()\n    message("not found DEMO9LIB_FOUND")\nendif(DEMO9LIB_FOUND)\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Useful Tools

ldd and ar

ldd library.so\nar -t library.a\n
1
2

Makefile

# -----------------------------------------------------------------------------\n# CMake project wrapper Makefile ----------------------------------------------\n# -----------------------------------------------------------------------------\n\nSHELL := /bin/bash\nRM    := rm -rf\nMKDIR := mkdir -p\n\nall: ./build/Makefile\n    @ $(MAKE) -C build\n\n./build/Makefile:\n    @  ($(MKDIR) build > /dev/null)\n    @  (cd build > /dev/null 2>&1 && cmake ..)\n\nclean:\n    @  ($(MKDIR) build > /dev/null)\n    @  (cd build > /dev/null 2>&1 && cmake .. > /dev/null 2>&1)\n    @- $(MAKE) --silent -C build clean || true\n    @- $(RM) ./build/Makefile\n    @- $(RM) ./build/src\n    @- $(RM) ./build/test\n    @- $(RM) ./build/CMake*\n    @- $(RM) ./build/cmake.*\n    @- $(RM) ./build/*.cmake\n    @- $(RM) ./build/*.txt\n\nifeq ($(findstring clean,$(MAKECMDGOALS)),)\n    $(MAKECMDGOALS): ./build/Makefile\n    @ $(MAKE) -C build $(MAKECMDGOALS)\nendif\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Config Command

Version Config

cmake_minimum_required(VERSION 3.1)\n\nif(${CMAKE_VERSION} VERSION_LESS 3.13)\n  cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})\nelse()\n  cmake_policy(VERSION 3.13)\nendif()\n
1
2
3
4
5
6
7

Project Config

project(MyProject VERSION 1.0 DESCRIPTION "Very nice project" LANGUAGES CXX)\n
1

Environment Config

set(ENV{variable_name} value) and get $ENV{variable_name} environment variables

Library for Clients Usage

include(CMakePackageConfigHelpers)\nwrite_basic_package_version_file("FooConfigVersion.cmake"\n  VERSION ${Foo_VERSION}\n  COMPATIBILITY SameMajorVersion\n)\n\ninstall(FILES "FooConfig.cmake" "FooConfigVersion.cmake"\n  DESTINATION lib/cmake/Foo\n)\n
1
2
3
4
5
6
7
8
9
include(CMakeFindDependencyMacro)\nfind_dependency(Bar 2.0)\ninclude("${CMAKE_CURRENT_LIST_DIR}/FooTargets.cmake")\n
1
2
3

Test Setting

set(CTEST_SOURCE_DIRECTORY "/source")\nset(CTEST_BINARY_DIRECTORY "/binary")\n\nset(ENV{CXXFLAGS} "--coverage")\nset(CTEST_CMAKE_GENERATOR "Ninja")\nset(CTEST_USE_LAUNCHERS 1)\n\nset(CTEST_COVERAGE_COMMAND "gcov")\nset(CTEST_MEMORYCHECK_COMMAND "valgrind")\n#set(CTEST_MEMORYCHECK_TYPE "ThreadSanitizer")\n\nctest_start("Continuous")\nctest_configure()\nctest_build()\nctest_test()\nctest_coverage()\nctest_memcheck()\nctest_submit()\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
macro(package_add_test TESTNAME)\n    add_executable(${TESTNAME} ${ARGN})\n    target_link_libraries(${TESTNAME} gtest gmock gtest_main)\n    add_test(${TESTNAME} COMMAND ${TESTNAME})\n    set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)\nendmacro()\n\npackage_add_test(test1 test1.cpp)\n
1
2
3
4
5
6
7
8

Generator Expression

Most CMake commands happen at configure time, include the if statements seen above. Generator expressions were added at runtime. They are evaluated in target properties:

  • If you want to put a compile flag only for the DEBUG configuration
  • Limiting an item to a certain language only, such as CXX
  • Accessing configuration dependent properties, like target file location
  • Giving a different location for build and install directories
target_include_directories(MyTarget PUBLIC\n  $<BUILD_INTERFACE:"${CMAKE_CURRENT_SOURCE_DIR}/include">\n  $<INSTALL_INTERFACE:include>\n)\n
1
2
3
4

Submodule and Dependencies

find_package(Git QUIET)\n\nif(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")\n  execute_process(COMMAND\n    ${GIT_EXECUTABLE} submodule update --init --recursive\n    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE GIT_SUBMOD_RESULT\n  )\n\n  if(NOT GIT_SUBMOD_RESULT EQUAL "0")\n    message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}")\n  endif()\nendif()\n
1
2
3
4
5
6
7
8
9
10
11
12

CMake Patterns

Modern CMake

Modern CMake is all about targets and properties.

Constructors of Targets:

  • add_executable()
  • add_library()

Member variables of Targets:

  • Target properties

Member functions:

  • get_target_property()
  • set_target_properties()
  • get_property(TARGET)
  • set_property(TARGET)
  • target_compile_definitions()
  • target_compile_features()
  • target_compile_options()
  • target_include_directories()
  • target_link_libraries()
  • target_sources()

Interface vs Private

interface properties model usage requirements, whereas private properties model build requirements of targets.

Nice Patterns

  • Think in targets (Object-Oriented)
  • Export your interface: You should be able to run from build or install
  • Write a Config.cmake file: This is what a library author should do to support clients
  • Make ALIAS targets to keep usage consistent
  • Combine common functionality into clearly documented functions
  • Use lowercase function names
  • Upper case is for variables
  • Use cmake_policy and/or range of versions

Anti Patterns

  • Do not use global functions: e.g link_directories, include_libraries
  • Don't add unneeded PUBLIC requirements e.g -Wall
  • Don't GLOB files
  • Link to built files directly: Always link to targets if available
  • Never skip PUBLIC/PRIVATE when linking

Makefile Notes

Makefile Macro

foo := a.o b.o c.o\nbar := $(foo:.o=.c)\nfirst_second = Hello\na = first\nb = second\nall = $($a_$b)\n
1
2
3
4
5
6

这里的$a_$b 组成了 first_second, 于是, $(all)的值就是“Hello”。

Built-in Makefile Macro

  • AR: 函数库打包程序。默认命令是“ar”
  • AS: 汇编语言编译程序。默认命令是“as”
  • CC: C 语言编译程序。默认命令是“cc”
  • CXX: C++语言编译程序。默认命令是“g++”
  • CO: 从 RCS 文件中扩展文件程序。默认命令是“co”
  • CPP: C 程序的预处理器(输出是标准输出设备)。默认命令是“$(CC) –E”
  • FC: Fortran 和 RatFor 的编译器和预处理程序。默认命令是“f77”
  • GET: 从 SCCS 文件中扩展文件的程序。默认命令是“get”。
  • LEX: Lex 方法分析器程序(针对于 C 或 RatFor)。默认命令是“lex”
  • PC: Pascal 语言编译程序。默认命令是“pc”
  • YACC: Yacc 文法分析器(针对于 C 程序)。默认命令是“yacc”
  • YACCR: Yacc 文法分析器(针对于 RatFor 程序)。默认命令是“yacc –r”
  • MAKEINFO: 转换 TexInfo 源文件(.texi)到 Info 文件程序。默认命令是“makeinfo”
  • TEX: 从 TeX 源文件创建 TeX DVI 文件的程序。默认命令是“tex”
  • TEXI2DVI: 从 TexInfo 源文件创建军 TeX DVI 文件的程序。默认命令是“texi2dvi”
  • WEAVE: 转换 Web 到 TeX 的程序。默认命令是“weave”
  • CWEAVE: 转换 C Web 到 TeX 的程序。默认命令是“cweave”
  • TANGLE: 转换 Web 到 Pascal 语言的程序。默认命令是“tangle”
  • CTANGLE: 转换 C Web 到 C。默认命令是“ctangle”
  • RM: 删除文件命令。默认命令是“rm –f”
  • ARFLAGS: 函数库打包程序 AR 命令的参数。默认值是“rv”
  • ASFLAGS: 汇编语言编译器参数。(当明显地调用“.s”或“.S”文件时)
  • CFLAGS: C 语言编译器参数
  • CXXFLAGS: C++语言编译器参数
  • COFLAGS: RCS 命令参数
  • CPPFLAGS: C 预处理器参数。( C 和 Fortran 编译器也会用到)
  • FFLAGS: Fortran 语言编译器参数
  • GFLAGS: SCCS “get”程序参数
  • LDFLAGS: 链接器参数。(如:“ld”)
  • LFLAGS: Lex 文法分析器参数
  • PFLAGS: Pascal 语言编译器参数
  • RFLAGS: RatFor 程序的 Fortran 编译器参数
  • YFLAGS: Yacc 文法分析器参数

Built-in Makefile Variable

  • $@: 表示规则中的目标文件集。在模式规则中,如果有多个目标,那么,"$@"就是匹配于目标中模式定义的集合
  • $%: 仅当目标是函数库文件中,表示规则中的目标成员名。 例如,如果一个目标是"foo.a(bar.o)",那么,"$%"就是"bar.o","$@"就是"foo.a"。 如果目标不是函数库文件(Unix 下是[.a],Windows 下是[.lib]),那么,其值为空
  • $<: 依赖目标中的第一个目标名字。如果依赖目标是以模式(即"%")定义的, 那么"$<"将是符合模式的一系列的文件集。注意,其是一个一个取出来的
  • $?: 所有比目标新的依赖目标的集合。以空格分隔
  • $^: 所有的依赖目标的集合。以空格分隔。如果在依赖目标中有多个重复的,那个这个变量会去除重复的依赖目标,只保留一份
  • $+: 这个变量很像"$^",也是所有依赖目标的集合。只是它不去除重复的依赖目标
  • $* : 这个变量表示目标模式中"%"及其之前的部分。如果目标是"dir/a.foo.b", 并且目标的模式是a.%.b,那么, $*的值就是dir/a.foo。这个变量对于构造有关联的文件名是比较有较。如果目标中没有模式的定义,那么$*也就不能被推导出, 但是,如果目标文件的后缀是 make 所识别的,那么$*就是除了后缀的那一部分。例如:如果目标是foo.c, 因为".c"是 make 所能识别的后缀名,所以,$*的值就是"foo"。这个特性是 GNU make 的, 很有可能不兼容于其它版本的 make, 所以,你应该尽量避免使用$*, 除非是在隐含规则或是静态模式中。 如果目标中的后缀是 make 所不能识别的,那么$*就是空值
  • $(@D): 表示"$@"的目录部分(不以斜杠作为结尾), 如果"$@"值是"dir/foo.o",那么"$(@D)"就是"dir", 而如果"$@"中没有包含斜杠的话,其值就是"."(当前目录)
  • $(@F): 表示"$@"的文件部分,如果"$@"值是"dir/foo.o",那么"$(@F)"就是"foo.o", "$(@F)"相当于函数"$(notdir $@)"
  • $(*D)"/"$(*F): 和上面所述的同理,也是取文件的目录部分和文件部分。对于上面的那个例子,$(*D)返回"dir",而$(*F)返回"foo"
  • "$(%D)"/"$(%F)": 分别表示了函数包文件成员的目录部分和文件部分。 这对于形同"archive(member)"形式的目标中的"member"中包含了不同的目录很有用。
  • "$(<D)"/"$(<F)": 分别表示依赖文件的目录部分和文件部分。
  • "$(^D)"/"$(^F)": 分别表示所有依赖文件的目录部分和文件部分。(无相同的)
  • "$(+D)"/"$(+F)": 分别表示所有依赖文件的目录部分和文件部分。(可以有相同的)
  • "$(?D)"/"$(?F)": 分别表示被更新的依赖文件的目录部分和文件部分。

Makefile Inexplicit Rules

Makefile C Rules

<n>.o的目标的依赖目标会自动推导为<n>.c,并且其生成命令是“$(CC) –c $(CPPFLAGS) $(CFLAGS)”

Makefile C++ Rules

<n>.o的目标的依赖目标会自动推导为<n>.cc或是<n>.C, 并且其生成命令是$(CXX) –c $(CPPFLAGS) \\$(CFLAGS) (建议使用.cc作为 C++源文件的后缀,而不是.C).

Makefile ASM Rules

<n>.o 的目标的依赖目标会自动推导为<n>.s,默认使用编译品“as”,并且其生成命令是:“$(AS) $(ASFLAGS)”。 <n>.s 的目标的依赖目标会自动推导为<n>.S,默认使用 C 预编译器“cpp”,并且其生成命令是:“$(AS) $(ASFLAGS)”。

Makefile Object Linking

<n>目标依赖于<n>.o,通过运行 C 的编译器来运行链接程序生成(一般是“ld”),其生成命令是: $(CC) $(LDFLAGS) <n>.o $(LOADLIBES) $(LDLIBS) 这个规则对于只有一个源文件的工程有效,同时也对多个 Object 文件(由不同的源文件生成)的也有效。例如如下规则:

x : y.o z.o

并且“x.c”、“y.c”和“z.c”都存在时,隐含规则将执行如下命令:

cc -c x.c -o x.o cc -c y.c -o y.o cc -c z.c -o z.o cc x.o y.o z.o -o x rm -f x.o rm -f y.o rm -f z.o

Makefile Function

  • filter
  • shell
  • subst
  • wildcard

Makefile Best Practice

$(filter %.o,$(files)): %.o: %.c\n$(filter %.elc,$(files)): %.elc: %.el\n
1
2
$(CC) -c $(CFLAGS) $< -o $@\n
1
(%.o) : %.c\n$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o\n$(AR) r $@ $*.o\n$(RM) $*.o\n
1
2
3
4
%.d: %.c\n@set -e; rm -f $@; /\n$(CC) -M $(CPPFLAGS) $< > $@.$$$$; /\nsed 's,/($*/)/.o[ :]*,/1.o $@ : ,g' < $@.$$$$ > $@; /\n$(RM) -f $@.$$$$\n
1
2
3
4
5

Reference

',139);p.render=function(a,e,p,l,o,c){return n(),s("div",null,[t])};export default p;export{e as __pageData}; diff --git a/assets/programming_tools_buildTools_CMakeBasicNotes.md.c21de6b5.lean.js b/assets/programming_tools_buildTools_CMakeBasicNotes.md.c21de6b5.lean.js new file mode 100644 index 00000000000..ea0a709d1d8 --- /dev/null +++ b/assets/programming_tools_buildTools_CMakeBasicNotes.md.c21de6b5.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const e='{"title":"CMake Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic Build System","slug":"basic-build-system"},{"level":3,"title":"Basic Options","slug":"basic-options"},{"level":2,"title":"Flow Control","slug":"flow-control"},{"level":3,"title":"if control","slug":"if-control"},{"level":3,"title":"foreach control","slug":"foreach-control"},{"level":3,"title":"while control","slug":"while-control"},{"level":3,"title":"function control","slug":"function-control"},{"level":2,"title":"Useful Command","slug":"useful-command"},{"level":3,"title":"Checking Command","slug":"checking-command"},{"level":3,"title":"Testing Command","slug":"testing-command"},{"level":3,"title":"Option Command","slug":"option-command"},{"level":3,"title":"Math Command","slug":"math-command"},{"level":3,"title":"List Command","slug":"list-command"},{"level":3,"title":"Package Command","slug":"package-command"},{"level":3,"title":"Install Command","slug":"install-command"},{"level":3,"title":"find packages","slug":"find-packages"},{"level":2,"title":"Useful Tools","slug":"useful-tools"},{"level":2,"title":"Config Command","slug":"config-command"},{"level":3,"title":"Version Config","slug":"version-config"},{"level":3,"title":"Project Config","slug":"project-config"},{"level":3,"title":"Environment Config","slug":"environment-config"},{"level":3,"title":"Library for Clients Usage","slug":"library-for-clients-usage"},{"level":2,"title":"Test Setting","slug":"test-setting"},{"level":2,"title":"Generator Expression","slug":"generator-expression"},{"level":2,"title":"Submodule and Dependencies","slug":"submodule-and-dependencies"},{"level":2,"title":"CMake Patterns","slug":"cmake-patterns"},{"level":3,"title":"Modern CMake","slug":"modern-cmake"},{"level":3,"title":"Nice Patterns","slug":"nice-patterns"},{"level":3,"title":"Anti Patterns","slug":"anti-patterns"},{"level":2,"title":"Makefile Notes","slug":"makefile-notes"},{"level":3,"title":"Makefile Macro","slug":"makefile-macro"},{"level":3,"title":"Built-in Makefile Variable","slug":"built-in-makefile-variable"},{"level":3,"title":"Makefile Inexplicit Rules","slug":"makefile-inexplicit-rules"},{"level":3,"title":"Makefile Function","slug":"makefile-function"},{"level":3,"title":"Makefile Best Practice","slug":"makefile-best-practice"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"programming/tools/buildTools/CMakeBasicNotes.md","lastUpdated":1627196177000}',p={},t=a('',139);p.render=function(a,e,p,l,o,c){return n(),s("div",null,[t])};export default p;export{e as __pageData}; diff --git a/assets/programming_tools_git_gitBasicNotes.md.abd5ba70.js b/assets/programming_tools_git_gitBasicNotes.md.abd5ba70.js new file mode 100644 index 00000000000..07d85e20c30 --- /dev/null +++ b/assets/programming_tools_git_gitBasicNotes.md.abd5ba70.js @@ -0,0 +1 @@ +import{o as n,c as e,b as s,d as a}from"./app.6dd2a1b7.js";const l='{"title":"Git Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Git Configuration","slug":"git-configuration"},{"level":3,"title":"Basic Configuration","slug":"basic-configuration"},{"level":3,"title":"Proxy Configuration","slug":"proxy-configuration"},{"level":3,"title":"List and Help","slug":"list-and-help"},{"level":2,"title":"File State","slug":"file-state"},{"level":2,"title":"Git Ignore File","slug":"git-ignore-file"},{"level":2,"title":"Diff","slug":"diff"},{"level":2,"title":"Add","slug":"add"},{"level":2,"title":"Commit","slug":"commit"},{"level":3,"title":"Commit Style Guide","slug":"commit-style-guide"},{"level":2,"title":"Stash","slug":"stash"},{"level":2,"title":"Revert","slug":"revert"},{"level":2,"title":"Remove","slug":"remove"},{"level":2,"title":"Move","slug":"move"},{"level":2,"title":"Log","slug":"log"},{"level":3,"title":"Pretty Format","slug":"pretty-format"},{"level":3,"title":"Log Options","slug":"log-options"},{"level":2,"title":"Reflog","slug":"reflog"},{"level":2,"title":"Show","slug":"show"},{"level":2,"title":"Remote","slug":"remote"},{"level":2,"title":"Tag","slug":"tag"},{"level":2,"title":"Alias","slug":"alias"},{"level":2,"title":"Merge","slug":"merge"},{"level":2,"title":"Rebase","slug":"rebase"},{"level":2,"title":"Branch","slug":"branch"},{"level":3,"title":"Basic Workflow Commands","slug":"basic-workflow-commands"},{"level":3,"title":"Advanced Branch Workflow","slug":"advanced-branch-workflow"},{"level":2,"title":"Git Inside","slug":"git-inside"},{"level":3,"title":"Add Inside","slug":"add-inside"},{"level":3,"title":"Commit Inside","slug":"commit-inside"},{"level":3,"title":"Checkout Inside","slug":"checkout-inside"},{"level":3,"title":"Merge Inside","slug":"merge-inside"},{"level":3,"title":"Fetch Inside","slug":"fetch-inside"},{"level":3,"title":"Clone Inside","slug":"clone-inside"},{"level":3,"title":"Push Inside","slug":"push-inside"},{"level":3,"title":"HEAD Branch Inside","slug":"head-branch-inside"},{"level":3,"title":"Git Objects","slug":"git-objects"},{"level":2,"title":"GitHub","slug":"github"},{"level":3,"title":"GPG Usage","slug":"gpg-usage"},{"level":3,"title":"LICENSE","slug":"license"},{"level":3,"title":"Teamwork","slug":"teamwork"},{"level":3,"title":"GitHub CLI Tool","slug":"github-cli-tool"},{"level":3,"title":"Wiki","slug":"wiki"},{"level":3,"title":"Shorten GitHub URL","slug":"shorten-github-url"},{"level":3,"title":"GitHub Flavored Markdown","slug":"github-flavored-markdown"},{"level":3,"title":"GitHub Pages","slug":"github-pages"},{"level":2,"title":"Git Tools","slug":"git-tools"},{"level":3,"title":"Diff and Patch","slug":"diff-and-patch"},{"level":3,"title":"Semantic Git Commit Message","slug":"semantic-git-commit-message"},{"level":3,"title":"Changelog Generator","slug":"changelog-generator"},{"level":3,"title":"Purge Tool","slug":"purge-tool"},{"level":2,"title":"Commands List","slug":"commands-list"},{"level":3,"title":"Basic Commands","slug":"basic-commands"},{"level":3,"title":"检查与比较","slug":"检查与比较"},{"level":3,"title":"调试","slug":"调试"},{"level":3,"title":"补丁","slug":"补丁"},{"level":3,"title":"邮件","slug":"邮件"},{"level":3,"title":"外部系统","slug":"外部系统"},{"level":3,"title":"管理","slug":"管理"}],"relativePath":"programming/tools/git/gitBasicNotes.md","lastUpdated":1627953616000}',t={},i=s("h1",{id:"git-basic-notes"},[s("a",{class:"header-anchor",href:"#git-basic-notes","aria-hidden":"true"},"#"),a(" Git Basic Notes")],-1),r=s("p",null,[s("div",{class:"table-of-contents"},[s("ul",null,[s("li",null,[s("a",{href:"#git-basic-notes"},"Git Basic Notes"),s("ul",null,[s("li",null,[s("a",{href:"#git-configuration"},"Git Configuration")]),s("li",null,[s("a",{href:"#file-state"},"File State")]),s("li",null,[s("a",{href:"#git-ignore-file"},"Git Ignore File")]),s("li",null,[s("a",{href:"#diff"},"Diff")]),s("li",null,[s("a",{href:"#add"},"Add")]),s("li",null,[s("a",{href:"#commit"},"Commit")]),s("li",null,[s("a",{href:"#stash"},"Stash")]),s("li",null,[s("a",{href:"#revert"},"Revert")]),s("li",null,[s("a",{href:"#remove"},"Remove")]),s("li",null,[s("a",{href:"#move"},"Move")]),s("li",null,[s("a",{href:"#log"},"Log")]),s("li",null,[s("a",{href:"#reflog"},"Reflog")]),s("li",null,[s("a",{href:"#show"},"Show")]),s("li",null,[s("a",{href:"#remote"},"Remote")]),s("li",null,[s("a",{href:"#tag"},"Tag")]),s("li",null,[s("a",{href:"#alias"},"Alias")]),s("li",null,[s("a",{href:"#merge"},"Merge")]),s("li",null,[s("a",{href:"#rebase"},"Rebase")]),s("li",null,[s("a",{href:"#branch"},"Branch")]),s("li",null,[s("a",{href:"#git-inside"},"Git Inside")]),s("li",null,[s("a",{href:"#github"},"GitHub")]),s("li",null,[s("a",{href:"#git-tools"},"Git Tools")]),s("li",null,[s("a",{href:"#commands-list"},"Commands List")])])])])])],-1),o=s("h2",{id:"git-configuration"},[s("a",{class:"header-anchor",href:"#git-configuration","aria-hidden":"true"},"#"),a(" Git Configuration")],-1),c=s("ul",null,[s("li",null,"/etc/gitconfig"),s("li",null,"~/.gitconfig 或 ~/.config/git/config"),s("li",null,"repo/.git/config")],-1),u=s("h3",{id:"basic-configuration"},[s("a",{class:"header-anchor",href:"#basic-configuration","aria-hidden":"true"},"#"),a(" Basic Configuration")],-1),p=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --global user.name "),s("span",{class:"token string"},'"sabertazimi"'),a("\n"),s("span",{class:"token function"},"git"),a(" config --global user.email sabertazimi@gmail.com\n"),s("span",{class:"token function"},"git"),a(" config --global core.editor "),s("span",{class:"token function"},"vim"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global credential.helper store\n"),s("span",{class:"token function"},"git"),a(" config --global color.ui "),s("span",{class:"token boolean"},"true"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br")])],-1),b=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --global commit.template "),s("span",{class:"token environment constant"},"$HOME"),a("/.GitCommit.md\n"),s("span",{class:"token function"},"git"),a(" config --global commit.gpgsign "),s("span",{class:"token boolean"},"true"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global user.signingkey "),s("span",{class:"token operator"},"<"),a("pub-keyID"),s("span",{class:"token operator"},">"),a("\n\n"),s("span",{class:"token function"},"git"),a(" config --global push.default simple\n"),s("span",{class:"token function"},"git"),a(" config --global merge.conflictstyle "),s("span",{class:"token function"},"diff3"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global pull.rebase "),s("span",{class:"token boolean"},"true"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global rebase.autoStash "),s("span",{class:"token boolean"},"true"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br")])],-1),d=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# after 1s, git auto correct wrong command"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global help.autocorrect "),s("span",{class:"token number"},"10"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),m=s("h3",{id:"proxy-configuration"},[s("a",{class:"header-anchor",href:"#proxy-configuration","aria-hidden":"true"},"#"),a(" Proxy Configuration")],-1),h=s("ul",null,[s("li",null,[s("a",{href:"http://github.com.cnpmjs.org",target:"_blank",rel:"noopener noreferrer"},"github.com.cnpmjs.org")]),s("li",null,[s("a",{href:"http://hub.fastgit.org",target:"_blank",rel:"noopener noreferrer"},"hub.fastgit.org")]),s("li",null,[s("a",{href:"http://raw.fastgit.org",target:"_blank",rel:"noopener noreferrer"},"raw.fastgit.org")])],-1),g=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# speed up by cnpmjs"),a("\n"),s("span",{class:"token comment"},"# git clone/push/pull works"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global url."),s("span",{class:"token string"},'"https://github.com.cnpmjs.org/"'),a(".insteadOf "),s("span",{class:"token string"},'"https://github.com/"'),a("\n\n"),s("span",{class:"token comment"},"# post buffer config"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global http.postbuffer "),s("span",{class:"token number"},"524288000"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global http.postbuffer "),s("span",{class:"token number"},"1048576000"),a("\n\n"),s("span",{class:"token comment"},"# proxy"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global http.proxy "),s("span",{class:"token string"},"'socks5://127.0.0.1:1080'"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global https.proxy "),s("span",{class:"token string"},"'socks5://127.0.0.1:1080'"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br")])],-1),k=s("h3",{id:"list-and-help"},[s("a",{class:"header-anchor",href:"#list-and-help","aria-hidden":"true"},"#"),a(" List and Help")],-1),f=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --list\n"),s("span",{class:"token function"},"git"),a(" --help\n"),s("span",{class:"token function"},"man"),a(" git-\n"),s("span",{class:"token function"},"git"),a(),s("span",{class:"token builtin class-name"},"help"),a("\n"),s("span",{class:"token function"},"git"),a(),s("span",{class:"token builtin class-name"},"help"),a(" config\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br")])],-1),v=s("h2",{id:"file-state"},[s("a",{class:"header-anchor",href:"#file-state","aria-hidden":"true"},"#"),a(" File State")],-1),w=s("ul",null,[s("li",null,"Untracked"),s("li",null,[a("Unmodified("),s("strong",null,"Stable State"),a(")")]),s("li",null,"Modified"),s("li",null,"Staged")],-1),y=s("h2",{id:"git-ignore-file"},[s("a",{class:"header-anchor",href:"#git-ignore-file","aria-hidden":"true"},"#"),a(" Git Ignore File")],-1),x=s("p",null,"文件 .gitignore 的格式规范如下:",-1),C=s("ul",null,[s("li",null,"所有空行或者以 # 开头的行都会被 Git 忽略"),s("li",null,"可以使用标准的 glob 模式(简化正则表达式)匹配"),s("li",null,"匹配模式可以以( / )开头防止递归"),s("li",null,"匹配模式可以以( / )结尾指定目录"),s("li",null,"要跟踪指定模式以外的文件或目录,可以在模式前加上惊叹号( ! )取反"),s("li",null,[s("a",{href:"https://github.com/github/gitignore",target:"_blank",rel:"noopener noreferrer"},"GitHub gitignore Style")])],-1),A=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# no .a files"),a("\n*.a\n\n"),s("span",{class:"token comment"},"# but do track lib.a, even though you're ignoring .a files above"),a("\n"),s("span",{class:"token operator"},"!"),a("lib.a\n\n"),s("span",{class:"token comment"},"# only ignore the TODO file in the current directory, not subDir/TODO"),a("\n/TODO\n\n"),s("span",{class:"token comment"},"# ignore all files in the build/ directory"),a("\nbuild/\n\n"),s("span",{class:"token comment"},"# ignore doc/notes.txt, but not doc/server/arch.txt"),a("\ndoc/*.txt\n\n"),s("span",{class:"token comment"},"# ignore all .pdf files in the doc/ directory"),a("\ndoc/**/*.pdf\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br")])],-1),G=s("h2",{id:"diff"},[s("a",{class:"header-anchor",href:"#diff","aria-hidden":"true"},"#"),a(" Diff")],-1),I=s("p",null,"查看未暂存(un-staged)差异",-1),E=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"diff"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),T=s("p",null,"查看已暂存(staged)差异",-1),N=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"diff"),a(" --staged\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),O=s("p",null,"显示空白字符错误(space/tab/return)",-1),R=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"diff"),a(" --check\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),S=s("h2",{id:"add"},[s("a",{class:"header-anchor",href:"#add","aria-hidden":"true"},"#"),a(" Add")],-1),H=s("ul",null,[s("li",null,"交互式的选择 add 特定部分")],-1),L=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"add"),a(" -p\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),j=s("h2",{id:"commit"},[s("a",{class:"header-anchor",href:"#commit","aria-hidden":"true"},"#"),a(" Commit")],-1),D=s("ul",null,[s("li",null,"-a: 跳过暂存阶段(git add)"),s("li",null,"-v: 显示详细 diff 信息")],-1),P=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" commit -a -v\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),B=s("p",null,"重新提交",-1),_=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" commit --amend -a -v\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),M=s("h3",{id:"commit-style-guide"},[s("a",{class:"header-anchor",href:"#commit-style-guide","aria-hidden":"true"},"#"),a(" Commit Style Guide")],-1),F=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/conventional-commits/conventionalcommits.org",target:"_blank",rel:"noopener noreferrer"},"Conventional Commits Specification")]),s("li",null,[s("a",{href:"https://github.com/conventional-changelog/commitlint",target:"_blank",rel:"noopener noreferrer"},"Commit Lint")]),s("li",null,[s("a",{href:"https://github.com/commitizen/cz-cli",target:"_blank",rel:"noopener noreferrer"},"Commitizen: Conventional commits CLI tool")]),s("li",null,[s("a",{href:"https://github.com/conventional-changelog/standard-version",target:"_blank",rel:"noopener noreferrer"},"Standard Version: Automate versioning and CHANGELOG generation")])],-1),U=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"npm"),a(" i -D standard-version\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),$=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,"npx commitizen init cz-conventional-changelog --save-dev --save-exact\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),q=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("type")]),s("span",{class:"token punctuation"},">")]),a("("),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("scope")]),s("span",{class:"token punctuation"},">")]),a("): "),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("subject")]),s("span",{class:"token punctuation"},">")]),a("\n(emptyLine)\n\n"),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("body")]),s("span",{class:"token punctuation"},">")]),a("\n (emptyLine)\n"),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("footer")]),s("span",{class:"token punctuation"},">")]),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),z=s("h4",{id:"message-subject"},[s("a",{class:"header-anchor",href:"#message-subject","aria-hidden":"true"},"#"),a(" Message Subject")],-1),W=s("p",null,"no more than 50 characters",-1),Y=s("h4",{id:"commit-type"},[s("a",{class:"header-anchor",href:"#commit-type","aria-hidden":"true"},"#"),a(" Commit Type")],-1),V=s("ul",null,[s("li",null,"feat: 新增了一个功能 (MINOR Version)."),s("li",null,"fix: 修复了一个 bug (PATCH Version)."),s("li",null,"docs: 只是更改文档."),s("li",null,"style: 不影响代码含义的变化 (空白、格式化、缺少分号等)."),s("li",null,"refactor: 代码重构, 既不修复错误也不添加功能."),s("li",null,"perf: 改进性能的代码更改."),s("li",null,"test: 添加确实测试或更正现有的测试."),s("li",null,"build: 影响构建系统或外部依赖关系的更改 (示例范围: gulp, broccoli, NPM)."),s("li",null,"ci: 更改持续集成文件和脚本 (示例范围: Travis, Circle, BrowserStack, SauceLabs)."),s("li",null,[a("chore: 其他不修改 src 或 test 文件 e.g "),s("code",null,"chore(release)"),a(".")]),s("li",null,"revert: commit 回退.")],-1),K=s("h4",{id:"scope-values"},[s("a",{class:"header-anchor",href:"#scope-values","aria-hidden":"true"},"#"),a(" Scope Values")],-1),J=s("ul",null,[s("li",null,"init"),s("li",null,"runner"),s("li",null,"watcher"),s("li",null,"config"),s("li",null,"web-server"),s("li",null,"proxy"),s("li",null,"empty")],-1),X=s("h4",{id:"message-body"},[s("a",{class:"header-anchor",href:"#message-body","aria-hidden":"true"},"#"),a(" Message Body")],-1),Q=s("ul",null,[s("li",null,"uses the imperative, present tense: “change” not “changed” nor “changes”"),s("li",null,[a("includes "),s("strong",null,"motivation"),a(" for the change and contrasts with previous behavior")])],-1),Z=s("h4",{id:"message-footer"},[s("a",{class:"header-anchor",href:"#message-footer","aria-hidden":"true"},"#"),a(" Message Footer")],-1),nn=s("ul",null,[s("li",null,"referencing issues e.g. close #666, #888"),s("li",null,[a("BREAKING CHANGE ("),s("code",null,"!"),a(") (MAJOR Version) e.g."),s("code",null,"port-runner"),a(" command line option has changed to "),s("code",null,"runner-port"),a(", so that it is consistent with the configuration file syntax. To migrate your project, change all the commands, where you use "),s("code",null,"--port-runner"),a(" to "),s("code",null,"--runner-port"),a(".")])],-1),en=s("h2",{id:"stash"},[s("a",{class:"header-anchor",href:"#stash","aria-hidden":"true"},"#"),a(" Stash")],-1),sn=s("ul",null,[s("li",null,"git stash: 备份当前的工作区的内容,将当前的工作区内容保存到 Git 栈"),s("li",null,"git stash apply/pop: 从 Git 栈中读取最近一次保存的内容,恢复工作区的相关内容"),s("li",null,[a("git stash branch "),s("code",null,""),a(": 新建分支,并在该分支上恢复储藏内容")]),s("li",null,"git stash list: 显示 Git 栈内的所有备份"),s("li",null,"git stash clear: 清空 Git 栈")],-1),an=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# git stash popup"),a("\n"),s("span",{class:"token function"},"git"),a(" stash show -p stash@"),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},"}"),a(),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"git"),a(" apply -R\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),ln=s("p",null,"Pop a single file:",-1),tn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" restore -s stash@"),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},"}"),a(" -- "),s("span",{class:"token operator"},"<"),a("filename"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" checkout stash@"),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},"}"),a(" -- "),s("span",{class:"token operator"},"<"),a("filename"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),rn=s("h2",{id:"revert"},[s("a",{class:"header-anchor",href:"#revert","aria-hidden":"true"},"#"),a(" Revert")],-1),on=s("ul",null,[s("li",null,"重新提交前 n 次的 commit")],-1),cn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" revert -n\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),un=s("h2",{id:"remove"},[s("a",{class:"header-anchor",href:"#remove","aria-hidden":"true"},"#"),a(" Remove")],-1),pn=s("p",null,"完全删除文件",-1),bn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"rm"),a(" filename\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),dn=s("p",null,"--cached: 保留磁盘文件(仅从 git 库移除文件)",-1),mn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"rm"),a(" --cached filename\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),hn=s("h2",{id:"move"},[s("a",{class:"header-anchor",href:"#move","aria-hidden":"true"},"#"),a(" Move")],-1),gn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"mv"),a(" old_path new_path\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),kn=s("h2",{id:"log"},[s("a",{class:"header-anchor",href:"#log","aria-hidden":"true"},"#"),a(" Log")],-1),fn=s("ul",null,[s("li",null,"-p: 打印 diff 差异信息"),s("li",null,"-n: n 为十进制数字,显示最近 n 次信息"),s("li",null,"--stat: 打印简略统计信息"),s("li",null,"--graph: 显示分支合并历史"),s("li",null,"--pretty=: 设置日志格式"),s("li",null,"--author=: 指定作者"),s("li",null,"--committer=: 指定提交者"),s("li",null,"--after=/--since=: 限制日志时间"),s("li",null,'--before=/--until=: 限制日志时间 "2008-01-15" "2 years 1 day 3 minutes ago"'),s("li",null,"--decorate: 查看各个分支当前所指的对象(commit object)"),s("li",null,"--help")],-1),vn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" log -p --stat --graph --pretty"),s("span",{class:"token operator"},"="),a("format:"),s("span",{class:"token string"},'"%h - %an, %ar : %s"'),a(" --since"),s("span",{class:"token operator"},"="),s("span",{class:"token number"},"2"),a(".weeks path_name\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),wn=s("h3",{id:"pretty-format"},[s("a",{class:"header-anchor",href:"#pretty-format","aria-hidden":"true"},"#"),a(" Pretty Format")],-1),yn=s("table",null,[s("thead",null,[s("tr",null,[s("th",{style:{"text-align":"left"}},"选项"),s("th",{style:{"text-align":"left"}},"说明")])]),s("tbody",null,[s("tr",null,[s("td",{style:{"text-align":"left"}},"%H"),s("td",{style:{"text-align":"left"}},"提交对象(commit)的完整哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%h"),s("td",{style:{"text-align":"left"}},"提交对象的简短哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%T"),s("td",{style:{"text-align":"left"}},"树对象(tree)的完整哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%t"),s("td",{style:{"text-align":"left"}},"树对象的简短哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%P"),s("td",{style:{"text-align":"left"}},"父对象(parent)的完整哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%p"),s("td",{style:{"text-align":"left"}},"父对象的简短哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%an"),s("td",{style:{"text-align":"left"}},"作者(author)的名字")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%ae"),s("td",{style:{"text-align":"left"}},"作者的电子邮件地址")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%ad"),s("td",{style:{"text-align":"left"}},"作者修订日期 (可以用|-date=|选项定制格式)")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%at"),s("td",{style:{"text-align":"left"}},"作者修订日期 (ms)")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%ar"),s("td",{style:{"text-align":"left"}},"作者修订日期,按多久以前的方式显示")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%cn"),s("td",{style:{"text-align":"left"}},"提交者(committer)的名字")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%ce"),s("td",{style:{"text-align":"left"}},"提交者的电子邮件地址")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%cd"),s("td",{style:{"text-align":"left"}},"提交日期")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%cr"),s("td",{style:{"text-align":"left"}},"提交日期,按多久以前的方式显示")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%s"),s("td",{style:{"text-align":"left"}},"提交说明")])])],-1),xn=s("h3",{id:"log-options"},[s("a",{class:"header-anchor",href:"#log-options","aria-hidden":"true"},"#"),a(" Log Options")],-1),Cn=s("table",null,[s("thead",null,[s("tr",null,[s("th",{style:{"text-align":"left"}},"选项"),s("th",{style:{"text-align":"left"}},"说明")])]),s("tbody",null,[s("tr",null,[s("td",{style:{"text-align":"left"}},"-p"),s("td",{style:{"text-align":"left"}},"打印 diff 差异信息")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"-n"),s("td",{style:{"text-align":"left"}},"n 为十进制数字,显示最近 n 次信息")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--stat"),s("td",{style:{"text-align":"left"}},"打印简略统计信息")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--graph"),s("td",{style:{"text-align":"left"}},"显示分支合并历史")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--pretty="),s("td",{style:{"text-align":"left"}},"设置日志格式")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--author="),s("td",{style:{"text-align":"left"}},"指定作者")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--committer="),s("td",{style:{"text-align":"left"}},"指定提交者")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--after=/--since="),s("td",{style:{"text-align":"left"}},"限制日志时间")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--before=/--until="),s("td",{style:{"text-align":"left"}},'限制日志时间 "2008-01-15" "2 years 1 day 3 minutes ago"')]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--help"),s("td",{style:{"text-align":"left"}})])])],-1),An=s("h2",{id:"reflog"},[s("a",{class:"header-anchor",href:"#reflog","aria-hidden":"true"},"#"),a(" Reflog")],-1),Gn=s("p",null,[s("code",null,"git reflog show"),a(" is an alias for "),s("code",null,"git log -g --abbrev-commit --pretty=oneline"),a(".")],-1),In=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" reflog\n"),s("span",{class:"token function"},"git"),a(" reset HEAD@"),s("span",{class:"token punctuation"},"{"),a("index"),s("span",{class:"token punctuation"},"}"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),En=s("h2",{id:"show"},[s("a",{class:"header-anchor",href:"#show","aria-hidden":"true"},"#"),a(" Show")],-1),Tn=s("ul",null,[s("li",null,"查看其他分支 或 提交点的文件状态")],-1),Nn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" show branchName/commitHash:fileName\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),On=s("h2",{id:"remote"},[s("a",{class:"header-anchor",href:"#remote","aria-hidden":"true"},"#"),a(" Remote")],-1),Rn=s("p",null,"添加与删除远程仓库源",-1),Sn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" remote "),s("span",{class:"token function"},"add"),a(),s("span",{class:"token operator"},"<"),a("shortname"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("remote-url"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" remote "),s("span",{class:"token function"},"rm"),a(),s("span",{class:"token operator"},"<"),a("shortname"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Hn=s("p",null,"拉取和推送变更",-1),Ln=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" pull "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a("\n"),s("span",{class:"token function"},"git"),a(" push "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token punctuation"},"["),a("local-branch-name"),s("span",{class:"token punctuation"},"]"),a(":"),s("span",{class:"token punctuation"},"["),a("remote-branch-name"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),jn=s("p",null,"显示仓库信息",-1),Dn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" remote show "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Pn=s("p",null,"重命名仓库缩写名",-1),Bn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" remote "),s("span",{class:"token function"},"rename"),a(),s("span",{class:"token operator"},"<"),a("old"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("new"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),_n=s("p",null,"从本地操作,删除远程仓库的分支",-1),Mn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" push origin --delete "),s("span",{class:"token punctuation"},"["),a("remote-branch-name"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Fn=s("p",null,"保存推送密码",-1),Un=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --global credential.helper store\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),$n=s("h2",{id:"tag"},[s("a",{class:"header-anchor",href:"#tag","aria-hidden":"true"},"#"),a(" Tag")],-1),qn=s("p",null,"列出标记及其信息",-1),zn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" tag\n"),s("span",{class:"token function"},"git"),a(" tag -l "),s("span",{class:"token string"},'"v1.8-"'),a("\n"),s("span",{class:"token function"},"git"),a(" show "),s("span",{class:"token operator"},"<"),a("tagName"),s("span",{class:"token punctuation"},"("),a("v1.4"),s("span",{class:"token punctuation"},")"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),Wn=s("p",null,"创建标签:",-1),Yn=s("ul",null,[s("li",null,"不加-m 会调用 core.editor)"),s("li",null,"省略 commit 序列,标签添加至最新提交")],-1),Vn=s("p",null,"创建附注(annotated)标签",-1),Kn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" tag -a "),s("span",{class:"token operator"},"<"),a("tagName"),s("span",{class:"token punctuation"},"("),a("v1.4"),s("span",{class:"token punctuation"},")"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token punctuation"},"["),a("commit序列"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Jn=s("p",null,"创建轻量(lightweight)标签",-1),Xn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" tag "),s("span",{class:"token operator"},"<"),a("tagName"),s("span",{class:"token punctuation"},"("),a("v1.4"),s("span",{class:"token punctuation"},")"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token punctuation"},"["),a("commit序列"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Qn=s("p",null,"共享标签至远程库",-1),Zn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" push "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token operator"},"<"),a("tagName"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" push "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a(" --tags\n"),s("span",{class:"token function"},"git"),a(" push --follow-tags\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),ne=s("h2",{id:"alias"},[s("a",{class:"header-anchor",href:"#alias","aria-hidden":"true"},"#"),a(" Alias")],-1),ee=s("ul",null,[s("li",null,"!: 执行外部命令")],-1),se=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --global alias.co checkout\n"),s("span",{class:"token function"},"git"),a(" config --global alias.br branch\n"),s("span",{class:"token function"},"git"),a(" config --global alias.ci commit\n"),s("span",{class:"token function"},"git"),a(" config --global alias.st status\n\n"),s("span",{class:"token function"},"git"),a(" config --global alias.unstage "),s("span",{class:"token string"},"'reset HEAD --'"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global alias.last "),s("span",{class:"token string"},"'log -1 HEAD'"),a("\n\n"),s("span",{class:"token function"},"git"),a(" config --global alias.visual "),s("span",{class:"token string"},"'!gitk'"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br")])],-1),ae=s("h2",{id:"merge"},[s("a",{class:"header-anchor",href:"#merge","aria-hidden":"true"},"#"),a(" Merge")],-1),le=s("p",null,"合并的结果是生成一个新的快照(并提交)(新的提交对象)",-1),te=s("h2",{id:"rebase"},[s("a",{class:"header-anchor",href:"#rebase","aria-hidden":"true"},"#"),a(" Rebase")],-1),ie=s("p",null,"切换到工作分支,编码开发新特性",-1),re=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout feature-branch\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),oe=s("p",null,"新特性开发完毕,变基操作以简洁提交历史",-1),ce=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" rebase master\n\n"),s("span",{class:"token function"},"git"),a(" rebase "),s("span",{class:"token punctuation"},"["),a("baseBranch"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token punctuation"},"["),a("topicBranch"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),ue=s("p",null,"切换到主分支,合并特性分支",-1),pe=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout master\n"),s("span",{class:"token function"},"git"),a(" merge feature-branch\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),be=s("p",null,"pull with auto rebase and auto stash",-1),de=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" pull --rebase --autostash\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),me=s("h2",{id:"branch"},[s("a",{class:"header-anchor",href:"#branch","aria-hidden":"true"},"#"),a(" Branch")],-1),he=s("h3",{id:"basic-workflow-commands"},[s("a",{class:"header-anchor",href:"#basic-workflow-commands","aria-hidden":"true"},"#"),a(" Basic Workflow Commands")],-1),ge=s("h4",{id:"basic-branch"},[s("a",{class:"header-anchor",href:"#basic-branch","aria-hidden":"true"},"#"),a(" Basic Branch")],-1),ke=s("p",null,"创建新分支",-1),fe=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch "),s("span",{class:"token operator"},"<"),a("new-branch-name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),ve=s("p",null,"删除分支",-1),we=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch -d "),s("span",{class:"token operator"},"<"),a("branch-name"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" push origin --delete "),s("span",{class:"token operator"},"<"),a("remote-branch-name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),ye=s("p",null,"切换分支",-1),xe=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout "),s("span",{class:"token operator"},"<"),a("branch-name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ce=s("p",null,"切换到新分支",-1),Ae=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout -b "),s("span",{class:"token operator"},"<"),a("new-branch-name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ge=s("p",null,"打印分支信息",-1),Ie=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch -v"),s("span",{class:"token punctuation"},"("),a("详细信息"),s("span",{class:"token punctuation"},")"),a(" -vv"),s("span",{class:"token punctuation"},"("),a("详细远程信息"),s("span",{class:"token punctuation"},")"),a(" --merged"),s("span",{class:"token punctuation"},"("),a("显示合并至当前分支的分支"),s("span",{class:"token punctuation"},")"),a(" --no-merged"),s("span",{class:"token punctuation"},"("),a("显示未合并至当前分支的分支"),s("span",{class:"token punctuation"},")"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ee=s("h4",{id:"remote-branch"},[s("a",{class:"header-anchor",href:"#remote-branch","aria-hidden":"true"},"#"),a(" Remote Branch")],-1),Te=s("ul",null,[s("li",null,"Show all remote branch:")],-1),Ne=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch -r\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Oe=s("p",null,"本地分支跟踪远程分支(在此本地分支上运行 git pull 自动抓取), 2 种方式:",-1),Re=s("ul",null,[s("li",null,"设置当前所在本地分支跟踪某一远程分支")],-1),Se=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch -u "),s("span",{class:"token punctuation"},"["),a("remoteName"),s("span",{class:"token punctuation"},"]"),a("/"),s("span",{class:"token punctuation"},"["),a("branch"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),He=s("ul",null,[s("li",null,[a("创建并切换至新的本地分支(跟踪某一远程分支) "),s("ul",null,[s("li",null,"--track: 本地分支由 git 自动命名"),s("li",null,"-b: 本地分支由创建者命名")])])],-1),Le=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout --track "),s("span",{class:"token punctuation"},"["),a("new-local-branch"),s("span",{class:"token punctuation"},"]"),a("\n\n"),s("span",{class:"token function"},"git"),a(" checkout -b "),s("span",{class:"token punctuation"},"["),a("new-local-branch"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token punctuation"},"["),a("remoteName"),s("span",{class:"token punctuation"},"]"),a("/"),s("span",{class:"token punctuation"},"["),a("branch"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),je=s("ul",null,[s("li",null,"Delete remote branch")],-1),De=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" push --delete origin "),s("span",{class:"token punctuation"},"["),a("remote-branch-name"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Pe=s("h3",{id:"advanced-branch-workflow"},[s("a",{class:"header-anchor",href:"#advanced-branch-workflow","aria-hidden":"true"},"#"),a(" Advanced Branch Workflow")],-1),Be=s("p",null,[s("a",{href:"https://github.com/nvie/gitflow",target:"_blank",rel:"noopener noreferrer"},"Git Flow Extension"),a(":")],-1),_e=s("ol",null,[s("li",null,"master 类型分支,名为?|master 或 master,其中?为开发代号"),s("li",null,"develop 类型分支,名为?|develop 或 develop,其中?为开发代号"),s("li",null,[a("feature 类型分支,名为 feature/"),s("em",null,"或?|feature/"),a(",其中*为特征描述")]),s("li",null,[a("release 类型分支,名为 release-"),s("em",null,"或?|release-"),a(",其中*为要发布的版本号")]),s("li",null,[a("hotfix 类型分支,名为 hotfix-"),s("em",null,"或?|hotfix-"),a(",其中*为要发布的版本号")]),s("li",null,[a("issues 类型分支,名为 issues/"),s("em",null,"或?|issues/"),a(",其中*为问题描述")]),s("li",null,"trials 类型分支,名为?%trials.*,?为此分支的父分支,*为描述的名称(或直接为?%trials)"),s("li",null,"basedOn 类型分支,名为 basedOn 或?|basedOn,?为其来源的 master 分支的开发代号"),s("li",null,[a("work 类型分支,名为 work.**"),s("em",null,"/basedOn-?-"),a(",***代表此描述此 work 的名称, ?为其所基于的分支的开发代号,最后一个*代表其在?|basedOn 上所基于的分支的版本号或状态名")])],-1),Me=s("p",null,"下面介绍模型中的约定,并定义 gg-*这样的抽象动作来完成约定中的行为",-1),Fe=s("h4",{id:"master-branch-and-develop-branch"},[s("a",{class:"header-anchor",href:"#master-branch-and-develop-branch","aria-hidden":"true"},"#"),a(" Master Branch and Develop Branch")],-1),Ue=s("p",null,[s("em",null,"多长期分支模式"),a(": master 分支与 develop 分支都是长期分支,区别在于分支的"),s("strong",null,"稳定性等级"),a(" - master > develop")],-1),$e=s("p",null,"e.g master/develop/next",-1),qe=s("ul",null,[s("li",null,"每一次的提交都必须有意义")],-1),ze=s("p",null,"git 在每次提交的时候要求输入对此提交的概括,这个概括不能为空。",-1),We=s("p",null,"正确的提交概括:更新了程序 doc 错误的提交概括:updates",-1),Ye=s("ul",null,[s("li",null,"开发型任务中的 master 类型与 develop 类型分支必须成对出现, master 分支的推进只能来源与 release 分支和 hotfix 分支的合并,禁止在 master 分支上直接提交")],-1),Ve=s("blockquote",null,[s("p",null,"master 分支上只有我们推送上去的稳定版本的程序,develop 分支上的程序一直处于开发状态,不稳定。 在开发型任务中使用 gg-init 进行版本控制的初始化,建立配套的 master ~ develop 分支对。 在使用型任务中使用 gg-work-init 进行版本控制的初始化, 拉取需要使用的稳定版本程序的 master 分支,并初始化对应的 basedOn 分支(见 9).")],-1),Ke=s("h4",{id:"feature-branch"},[s("a",{class:"header-anchor",href:"#feature-branch","aria-hidden":"true"},"#"),a(" Feature Branch")],-1),Je=s("ol",null,[s("li",null,"只能从 develop 类型分支上创建"),s("li",null,"最终必须合并到 develop 类型分支"),s("li",null,"最终分支被删除")],-1),Xe=s("blockquote",null,[s("p",null,"每当有新特性需要加入的时候,我们应该从 develop 类型分支上新建一个 feature 类型分支,完成新特性的开发和测试后将特性合并到 develop 类型分支上。 在 develop 类型分支上使用 gg-feature-open featureName 建立并转向一个名为 feature/featureName 的新分支 在一个 feature 类型分支上使用 gg-feature-close 把这个分支的工作合并到 develop 类型分支上,删除此分支,完成一个特性的开发")],-1),Qe=s("h4",{id:"release-branch"},[s("a",{class:"header-anchor",href:"#release-branch","aria-hidden":"true"},"#"),a(" Release Branch")],-1),Ze=s("ol",null,[s("li",null,"只能从 develop 类型分支上创建"),s("li",null,"最终必须同时合并到 master 类型分支(发布新的版本)和 develop 类型分支(基于新版本的进一步开发)"),s("li",null,"最终分支被删除")],-1),ns=s("blockquote",null,[s("p",null,"每当工作进入到一个较为稳定阶段的时候,可以使用 gg-release-open versionNum 建立一个名为 release-versionNum 的临时分支, 在这个分支上允许进行小的改动(比如修改一下 readme 文件中的版本号), 然后使用 gg-release-close 将此版本合并(发布)到 master 类型分支上,同时合并到 develop 类型分支上,然后删除此分支.")],-1),es=s("h4",{id:"hotfix-branch"},[s("a",{class:"header-anchor",href:"#hotfix-branch","aria-hidden":"true"},"#"),a(" Hotfix Branch")],-1),ss=s("ol",null,[s("li",null,"只能从 master 类型分支上创建"),s("li",null,"最终必须同时合并到 master 类型分支(发布新的热补丁版本)和 develop 类型分支(基于新版本的进一步开发)"),s("li",null,"最终分支被删除")],-1),as=s("blockquote",null,[s("p",null,"当新版本发布后发现必须马上解决的严重 bug 时,使用 gg-hotfix-open versionNum 建立名为 hotfix-versionNum 的临时分支, 在这个分支上完成 bug 的修复,然后使用 gg-hotfix-close 将此版本合并(发布)到 master 类型分支上,同时合并到 develop 类型分支上,然后删除此分支.")],-1),ls=s("h4",{id:"issues-branch"},[s("a",{class:"header-anchor",href:"#issues-branch","aria-hidden":"true"},"#"),a(" Issues Branch")],-1),ts=s("ol",null,[s("li",null,"只能从 develop 类型分支上创建"),s("li",null,"最终必须合并到 develop 类型分支"),s("li",null,"最终分支被删除")],-1),is=s("blockquote",null,[s("p",null,"注解:每当有(比较复杂的)问题需要解决的时候,应该从 develop 类型分支上新建一个 issues 类型分支,完成问题的调试后合并到 develop 类型分支上。 在 develop 类型分支上使用 gg-issues-open featureName 建立并转向一个名为 issues/issuesName 的新分支 在一个 issues 类型分支上使用 gg-issues-close 把这个分支的工作合并到 develop 类型分支上,然后删除此分支,解决了一个复杂的问题 issues 类型和 feature 类型的实现方式一模一样,仅仅有名字上面的差别。")],-1),rs=s("h4",{id:"trials-branch"},[s("a",{class:"header-anchor",href:"#trials-branch","aria-hidden":"true"},"#"),a(" Trials Branch")],-1),os=s("ul",null,[s("li",null,"可以从除了 release 类型分支以外的任何类型分支上创建"),s("li",null,[a("在这个分支上请发挥想象力大胆实验 "),s("ul",null,[s("li",null,"接受实验结果,把实验过程并入父分支,称为 good-close"),s("li",null,"实验结果不理想,放弃实验结果,从实验开始前重新来过,称为 bad-close")])]),s("li",null,"最终分支被删除")],-1),cs=s("blockquote",null,[s("p",null,"在满足条件的分支 A 上工作,时不时会冒出一些大胆的想法 这个时候使用 gg-trials-open trialsName 创建并转向一个名为 A/trials.trialsName 的实验分支,在这个分支上进行疯狂的实验")],-1),us=s("h4",{id:"basedon-branch"},[s("a",{class:"header-anchor",href:"#basedon-branch","aria-hidden":"true"},"#"),a(" BasedOn Branch")],-1),ps=s("ol",null,[s("li",null,"从 name|master 建立并初始化为 name|basedOn"),s("li",null,"只能从对应的 master 分支 fork 到此分支"),s("li",null,"禁止在这个分支上提交")],-1),bs=s("blockquote",null,[s("p",null,"这个分支是一个为了使工作流程更为清晰的缓存分支, 分支上只有从 master 稳定分支上挑选出来的自己在工作中将要(尝试)使用的稳定版本。 在 basedOn 类型分支上使用 gg-select 版本号 从对应的 master 分支上选出一个稳定版本或使用 gg-select-the-latest 从对应的 master 分支上选择最新的版本, fork 到这个分支,并加上 inUse-versionNum 的标签 从 master 到此分支的行为是 fork, 即有可能此分支的 log 为 (init)v1.0===>v0.9=====>v0.8======>v1.3, 这个分支上的 commit 来源于 master,但是其分支提交历史与 master 分支无关")],-1),ds=s("h4",{id:"work-branch"},[s("a",{class:"header-anchor",href:"#work-branch","aria-hidden":"true"},"#"),a(" Work Branch")],-1),ms=s("ol",null,[s("li",null,"只能从 basedOn 类型分支上创建"),s("li",null,"可以借助 basedOn 分支升级")],-1),hs=s("h2",{id:"git-inside"},[s("a",{class:"header-anchor",href:"#git-inside","aria-hidden":"true"},"#"),a(" Git Inside")],-1),gs=s("h3",{id:"add-inside"},[s("a",{class:"header-anchor",href:"#add-inside","aria-hidden":"true"},"#"),a(" Add Inside")],-1),ks=s("ul",null,[s("li",null,"create blob objects: contains content of files"),s("li",null,"add files to index list (.git/index)")],-1),fs=s("h3",{id:"commit-inside"},[s("a",{class:"header-anchor",href:"#commit-inside","aria-hidden":"true"},"#"),a(" Commit Inside")],-1),vs=s("ul",null,[s("li",null,"create tree objects: each object represent a directory, contains blob object refs in this directory"),s("li",null,"create commit object: contains root tree object hash number and parent commit object hash number")],-1),ws=s("h3",{id:"checkout-inside"},[s("a",{class:"header-anchor",href:"#checkout-inside","aria-hidden":"true"},"#"),a(" Checkout Inside")],-1),ys=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout "),s("span",{class:"token operator"},"<"),a("commit-hash-id"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),xs=s("ul",null,[s("li",null,"get commit object by commit hash id"),s("li",null,"get root tree object in commit object"),s("li",null,"write file entries by root tree object (tree graph)"),s("li",null,"write .git/index"),s("li",null,"set HEAD to that commit (detached HEAD state)")],-1),Cs=s("div",{class:"language-js line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"// Get file commit history"),a("\n"),s("span",{class:"token keyword"},"const"),a(" Git "),s("span",{class:"token operator"},"="),a(),s("span",{class:"token function"},"require"),s("span",{class:"token punctuation"},"("),s("span",{class:"token string"},"'nodegit'"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n"),s("span",{class:"token keyword"},"let"),a(" repo"),s("span",{class:"token punctuation"},";"),a("\n\nGit"),s("span",{class:"token punctuation"},"."),a("Repository"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"open"),s("span",{class:"token punctuation"},"("),a("path"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"resolve"),s("span",{class:"token punctuation"},"("),s("span",{class:"token string"},"'./.git'"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"then"),s("span",{class:"token punctuation"},"("),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token punctuation"},"("),s("span",{class:"token parameter"},"r"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n repo "),s("span",{class:"token operator"},"="),a(" r"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token keyword"},"return"),a(" repo"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"getMasterCommit"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"then"),s("span",{class:"token punctuation"},"("),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token punctuation"},"("),s("span",{class:"token parameter"},"firstCommitOnMaster"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"const"),a(" walker "),s("span",{class:"token operator"},"="),a(" repo"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"createRevWalk"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n walker"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"push"),s("span",{class:"token punctuation"},"("),a("firstCommitOnMaster"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"sha"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n walker"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"sorting"),s("span",{class:"token punctuation"},"("),a("Git"),s("span",{class:"token punctuation"},"."),a("Revwalk"),s("span",{class:"token punctuation"},"."),s("span",{class:"token constant"},"SORT"),s("span",{class:"token punctuation"},"."),a("Time"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n\n "),s("span",{class:"token keyword"},"return"),a(" walker"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"fileHistoryWalk"),s("span",{class:"token punctuation"},"("),a("historyFile"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token number"},"2"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"then"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},"("),s("span",{class:"token parameter"},"resultingArrayOfCommits"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token operator"},"=>"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"if"),a(),s("span",{class:"token punctuation"},"("),a("resultingArrayOfCommits"),s("span",{class:"token punctuation"},"."),a("length "),s("span",{class:"token operator"},">"),a(),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"const"),a(" commit "),s("span",{class:"token operator"},"="),a(" resultingArrayOfCommits"),s("span",{class:"token punctuation"},"["),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},"."),a("commit"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token keyword"},"const"),a(" date "),s("span",{class:"token operator"},"="),a(" commit"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"date"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token punctuation"},"}"),a("\n "),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n\n"),s("span",{class:"token keyword"},"const"),a(),s("span",{class:"token function-variable function"},"getGitLastUpdatedTimeStamp"),a(),s("span",{class:"token operator"},"="),a(),s("span",{class:"token punctuation"},"("),s("span",{class:"token parameter"},"filePath"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token operator"},"=>"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"let"),a(" lastUpdated "),s("span",{class:"token operator"},"="),a(),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},";"),a("\n\n "),s("span",{class:"token keyword"},"try"),a(),s("span",{class:"token punctuation"},"{"),a("\n lastUpdated "),s("span",{class:"token operator"},"="),a("\n "),s("span",{class:"token function"},"parseInt"),s("span",{class:"token punctuation"},"("),a("\n spawn\n "),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"sync"),s("span",{class:"token punctuation"},"("),s("span",{class:"token string"},"'git'"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token punctuation"},"["),s("span",{class:"token string"},"'log'"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token string"},"'-1'"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token string"},"'--format=%at'"),s("span",{class:"token punctuation"},","),a(" path"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"basename"),s("span",{class:"token punctuation"},"("),a("filePath"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token punctuation"},"{"),a("\n cwd"),s("span",{class:"token operator"},":"),a(" path"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"dirname"),s("span",{class:"token punctuation"},"("),a("filePath"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},","),a("\n "),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},"."),a("stdout"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"toString"),s("span",{class:"token punctuation"},"("),s("span",{class:"token string"},"'utf-8'"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token operator"},"*"),a(),s("span",{class:"token number"},"1000"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token punctuation"},"}"),a(),s("span",{class:"token keyword"},"catch"),a(),s("span",{class:"token punctuation"},"("),a("e"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token comment"},"/* do not handle for now */"),a("\n "),s("span",{class:"token punctuation"},"}"),a("\n\n "),s("span",{class:"token keyword"},"return"),a(" lastUpdated"),s("span",{class:"token punctuation"},";"),a("\n"),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},";"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br"),s("span",{class:"line-number"},"19"),s("br"),s("span",{class:"line-number"},"20"),s("br"),s("span",{class:"line-number"},"21"),s("br"),s("span",{class:"line-number"},"22"),s("br"),s("span",{class:"line-number"},"23"),s("br"),s("span",{class:"line-number"},"24"),s("br"),s("span",{class:"line-number"},"25"),s("br"),s("span",{class:"line-number"},"26"),s("br"),s("span",{class:"line-number"},"27"),s("br"),s("span",{class:"line-number"},"28"),s("br"),s("span",{class:"line-number"},"29"),s("br"),s("span",{class:"line-number"},"30"),s("br"),s("span",{class:"line-number"},"31"),s("br"),s("span",{class:"line-number"},"32"),s("br"),s("span",{class:"line-number"},"33"),s("br"),s("span",{class:"line-number"},"34"),s("br"),s("span",{class:"line-number"},"35"),s("br"),s("span",{class:"line-number"},"36"),s("br"),s("span",{class:"line-number"},"37"),s("br"),s("span",{class:"line-number"},"38"),s("br"),s("span",{class:"line-number"},"39"),s("br"),s("span",{class:"line-number"},"40"),s("br"),s("span",{class:"line-number"},"41"),s("br")])],-1),As=s("h3",{id:"merge-inside"},[s("a",{class:"header-anchor",href:"#merge-inside","aria-hidden":"true"},"#"),a(" Merge Inside")],-1),Gs=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" merge "),s("span",{class:"token operator"},"<"),a("giver-branch"),s("span",{class:"token operator"},">"),a("/"),s("span",{class:"token operator"},"<"),a("giver-commit"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Is=s("ul",null,[s("li",null,[a("write giver commit hash to "),s("code",null,".git/MERGE_HEAD")]),s("li",null,"find base commit (the most recent common ancestor commit)"),s("li",null,"diff and apply according to base commit, giver commit, receiver commit"),s("li",null,[a("do what "),s("code",null,"git checkout"),a(" do")]),s("li",null,[a("remove "),s("code",null,".git/MERGE_HEAD")])],-1),Es=s("h3",{id:"fetch-inside"},[s("a",{class:"header-anchor",href:"#fetch-inside","aria-hidden":"true"},"#"),a(" Fetch Inside")],-1),Ts=s("ul",null,[s("li",null,"get hash of remote commit and its root tree object"),s("li",null,"copy all diff objects in tree graph into .git/objects"),s("li",null,[a("update "),s("code",null,".git/refs/remotes/origin/"),a(", set "),s("code",null,".git/FETCH_HEAD"),a(" to it")])],-1),Ns=s("h3",{id:"clone-inside"},[s("a",{class:"header-anchor",href:"#clone-inside","aria-hidden":"true"},"#"),a(" Clone Inside")],-1),Os=s("p",null,[s("code",null,"git init"),a(" + "),s("code",null,"git remote add origin "),a(" + "),s("code",null,"git pull origin")],-1),Rs=s("h3",{id:"push-inside"},[s("a",{class:"header-anchor",href:"#push-inside","aria-hidden":"true"},"#"),a(" Push Inside")],-1),Ss=s("ul",null,[s("li",null,"apply commit to remote repo"),s("li",null,[a("update remote repo "),s("code",null,".git/refs/heads/"),a(" to new commit")]),s("li",null,[a("update local repo "),s("code",null,".git/refs/remotes/origin/"),a(" to new commit")])],-1),Hs=s("h3",{id:"head-branch-inside"},[s("a",{class:"header-anchor",href:"#head-branch-inside","aria-hidden":"true"},"#"),a(" HEAD Branch Inside")],-1),Ls=s("ul",null,[s("li",null,"HEAD -> refs/heads/master -> commit object"),s("li",null,"branches are just refs, refs are just files (contain commit hash id)")],-1),js=s("h3",{id:"git-objects"},[s("a",{class:"header-anchor",href:"#git-objects","aria-hidden":"true"},"#"),a(" Git Objects")],-1),Ds=s("p",null,[s("code",null,".git/objects"),a(" is immutable, "),s("code",null,".git/refs"),a(" is mutable")],-1),Ps=s("p",null,[s("code",null,"blob"),a("持有文件的内容,"),s("code",null,"树对象"),a("是一个包含"),s("code",null,"blob"),a("对象和"),s("code",null,"子树对象"),a("的目录列表. "),s("code",null,"提交对象"),a("是工作目录的一个快照, 包含了一些像时间或提交信息这样的元数据. "),s("code",null,"分支"),a("是"),s("code",null,"提交对象"),a("的命名引用. "),s("code",null,"工作目录"),a("是一个目录, 有着相应的仓库, "),s("code",null,"暂存区"),a("(索引)为下一个"),s("code",null,"提交对象"),a("持有对应的"),s("code",null,"树对象"),a(", 而仓库就是一个"),s("code",null,"提交对象"),a("的集合.")],-1),Bs=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" hash-object 创建blob对象\n"),s("span",{class:"token function"},"git"),a(" cat-file -t\n"),s("span",{class:"token function"},"git"),a(" cat-file -p\n"),s("span",{class:"token function"},"git"),a(" update-index --add --cache-info 将文件添加至暂存区\n"),s("span",{class:"token function"},"git"),a(" write-tree 创建tree对象\n"),s("span",{class:"token function"},"git"),a(" commit-tree 创建commit对象\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),_s=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# -w for write into codebase,"),a("\n"),s("span",{class:"token comment"},"# --stdin for reading from stdin not file"),a("\n"),s("span",{class:"token builtin class-name"},"echo"),a(),s("span",{class:"token string"},"'test content'"),a(),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"git"),a(" hash-object -w --stdin\n"),s("span",{class:"token function"},"git"),a(" cat-file -p "),s("span",{class:"token operator"},"<"),a("object-hash-number"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br")])],-1),Ms=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token shebang important"},"#!/bin/bash"),a("\n\n"),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token function-name function"},"separator"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"for"),a(),s("span",{class:"token for-or-select variable"},"i"),a(),s("span",{class:"token keyword"},"in"),a(),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"1"),s("span",{class:"token punctuation"},".."),s("span",{class:"token number"},"20"),s("span",{class:"token punctuation"},"}"),a("\n "),s("span",{class:"token keyword"},"do"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},'"-"'),a("\n "),s("span",{class:"token keyword"},"done"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token variable"},"$1"),a("\n "),s("span",{class:"token keyword"},"for"),a(),s("span",{class:"token for-or-select variable"},"i"),a(),s("span",{class:"token keyword"},"in"),a(),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"1"),s("span",{class:"token punctuation"},".."),s("span",{class:"token number"},"20"),s("span",{class:"token punctuation"},"}"),a("\n "),s("span",{class:"token keyword"},"do"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},'"-"'),a("\n "),s("span",{class:"token keyword"},"done"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},[a('"'),s("span",{class:"token entity",title:"\\n"},"\\n"),a('"')]),a("\n"),s("span",{class:"token punctuation"},"}"),a("\n\n"),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token function-name function"},"git_object_type"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},'"type => "'),a("\n "),s("span",{class:"token function"},"git"),a(" cat-file -t "),s("span",{class:"token variable"},"$1"),a("\n "),s("span",{class:"token keyword"},"for"),a(),s("span",{class:"token for-or-select variable"},"i"),a(),s("span",{class:"token keyword"},"in"),a(),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"1"),s("span",{class:"token punctuation"},".."),s("span",{class:"token number"},"40"),s("span",{class:"token punctuation"},"}"),a("\n "),s("span",{class:"token keyword"},"do"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},'"-"'),a("\n "),s("span",{class:"token keyword"},"done"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},[a('"'),s("span",{class:"token entity",title:"\\n"},"\\n"),a('"')]),a("\n"),s("span",{class:"token punctuation"},"}"),a("\n\n"),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token function-name function"},"git_object_content"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token function"},"git"),a(" cat-file -p "),s("span",{class:"token variable"},"$1"),a("\n"),s("span",{class:"token punctuation"},"}"),a("\n\n"),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token function-name function"},"print_git_objects"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token assign-left variable"},"files"),s("span",{class:"token operator"},"="),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"git"),a(" rev-list --parents --objects HEAD "),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"awk"),a(),s("span",{class:"token string"},"'{print $1}'"),s("span",{class:"token variable"},")")]),a("\n "),s("span",{class:"token assign-left variable"},"index"),s("span",{class:"token operator"},"="),s("span",{class:"token number"},"0"),a("\n\n "),s("span",{class:"token keyword"},"for"),a(),s("span",{class:"token for-or-select variable"},"file"),a(),s("span",{class:"token keyword"},"in"),a(),s("span",{class:"token variable"},"$files"),a("\n "),s("span",{class:"token keyword"},"do"),a("\n "),s("span",{class:"token assign-left variable"},"len"),s("span",{class:"token operator"},"="),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"expr"),a(" length "),s("span",{class:"token string"},[a('"'),s("span",{class:"token variable"},"$file"),a('"')]),s("span",{class:"token variable"},")")]),a("\n\n "),s("span",{class:"token keyword"},"if"),a(),s("span",{class:"token punctuation"},"["),a(),s("span",{class:"token variable"},"$len"),a(" -gt "),s("span",{class:"token number"},"30"),a(),s("span",{class:"token punctuation"},"]"),a("\n "),s("span",{class:"token keyword"},"then"),a("\n "),s("span",{class:"token assign-left variable"},"index"),s("span",{class:"token operator"},"="),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"expr"),a(" $index + "),s("span",{class:"token number"},"1"),s("span",{class:"token variable"},")")]),a("\n separator "),s("span",{class:"token variable"},"$index"),a("\n "),s("span",{class:"token builtin class-name"},"echo"),a(),s("span",{class:"token variable"},"$file"),a("\n git_object_type "),s("span",{class:"token variable"},"$file"),a("\n git_object_content "),s("span",{class:"token variable"},"$file"),a("\n "),s("span",{class:"token keyword"},"fi"),a("\n "),s("span",{class:"token keyword"},"done"),a("\n"),s("span",{class:"token punctuation"},"}"),a("\n\nprint_git_objects\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br"),s("span",{class:"line-number"},"19"),s("br"),s("span",{class:"line-number"},"20"),s("br"),s("span",{class:"line-number"},"21"),s("br"),s("span",{class:"line-number"},"22"),s("br"),s("span",{class:"line-number"},"23"),s("br"),s("span",{class:"line-number"},"24"),s("br"),s("span",{class:"line-number"},"25"),s("br"),s("span",{class:"line-number"},"26"),s("br"),s("span",{class:"line-number"},"27"),s("br"),s("span",{class:"line-number"},"28"),s("br"),s("span",{class:"line-number"},"29"),s("br"),s("span",{class:"line-number"},"30"),s("br"),s("span",{class:"line-number"},"31"),s("br"),s("span",{class:"line-number"},"32"),s("br"),s("span",{class:"line-number"},"33"),s("br"),s("span",{class:"line-number"},"34"),s("br"),s("span",{class:"line-number"},"35"),s("br"),s("span",{class:"line-number"},"36"),s("br"),s("span",{class:"line-number"},"37"),s("br"),s("span",{class:"line-number"},"38"),s("br"),s("span",{class:"line-number"},"39"),s("br"),s("span",{class:"line-number"},"40"),s("br"),s("span",{class:"line-number"},"41"),s("br"),s("span",{class:"line-number"},"42"),s("br"),s("span",{class:"line-number"},"43"),s("br"),s("span",{class:"line-number"},"44"),s("br"),s("span",{class:"line-number"},"45"),s("br"),s("span",{class:"line-number"},"46"),s("br"),s("span",{class:"line-number"},"47"),s("br"),s("span",{class:"line-number"},"48"),s("br"),s("span",{class:"line-number"},"49"),s("br")])],-1),Fs=s("h2",{id:"github"},[s("a",{class:"header-anchor",href:"#github","aria-hidden":"true"},"#"),a(" GitHub")],-1),Us=s("h3",{id:"gpg-usage"},[s("a",{class:"header-anchor",href:"#gpg-usage","aria-hidden":"true"},"#"),a(" GPG Usage")],-1),$s=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# Generate GPG key"),a("\ngpg --full-generate-key\n"),s("span",{class:"token comment"},"# List GPG keys"),a("\ngpg --list-keys\n\n"),s("span",{class:"token comment"},"# Generate GPG public key string"),a("\ngpg --armor --export "),s("span",{class:"token operator"},"<"),a("pub-keyID"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token comment"},"# Copy output to GitHub GPG textarea"),a("\n\n"),s("span",{class:"token comment"},"# Git global configuration for GPG signature commits"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global user.signingkey "),s("span",{class:"token operator"},"<"),a("pub-keyID"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global commit.gpgsign "),s("span",{class:"token boolean"},"true"),a("\n\n"),s("span",{class:"token comment"},"# Single signature commit"),a("\n"),s("span",{class:"token function"},"git"),a(" commit -S -m "),s("span",{class:"token string"},'"..."'),a("\n\n"),s("span",{class:"token comment"},"# Import GitHub signature"),a("\n"),s("span",{class:"token function"},"curl"),a(" https://github.com/web-flow.gpg "),s("span",{class:"token operator"},"|"),a(" gpg --import\ngpg --sign-key "),s("span",{class:"token operator"},"<"),a("GitHub-keyID"),s("span",{class:"token operator"},">"),a("\n\n"),s("span",{class:"token comment"},"# Log git signature"),a("\n"),s("span",{class:"token function"},"git"),a(" log --show-signature\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br"),s("span",{class:"line-number"},"19"),s("br"),s("span",{class:"line-number"},"20"),s("br"),s("span",{class:"line-number"},"21"),s("br"),s("span",{class:"line-number"},"22"),s("br")])],-1),qs=s("h3",{id:"license"},[s("a",{class:"header-anchor",href:"#license","aria-hidden":"true"},"#"),a(" LICENSE")],-1),zs=s("h4",{id:"popular-license"},[s("a",{class:"header-anchor",href:"#popular-license","aria-hidden":"true"},"#"),a(" Popular LICENSE")],-1),Ws=s("div",{class:"language-mermaid line-numbers-mode"},[s("pre",null,[s("code",null,"graph TD\nLicense --\x3e A{Open Source}\nA --\x3e|Yes| B{Same License}\nA --\x3e|No| D{List Copyright on Changed}\nB --\x3e|Yes| GPL\nB --\x3e|No| C{Change Docs}\nC --\x3e|Yes| Mozilla\nC --\x3e|No| LGPL\nD --\x3e|Yes| Apache\nD --\x3e|No| E{Enterprise}\nE --\x3e|Yes| MIT\nE --\x3e|No| BSD\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br")])],-1),Ys=s("p",null,[s("img",{src:"/awesome-notes/assets/6_free_software_licenses.344bcf8c.png",alt:"Free Software License"})],-1),Vs=s("h4",{id:"unique-license"},[s("a",{class:"header-anchor",href:"#unique-license","aria-hidden":"true"},"#"),a(" Unique LICENSE")],-1),Ks=s("ul",null,[s("li",null,"CC BY-NC-SA 3.0 License")],-1),Js=s("div",{class:"language-html line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("a")]),a(),s("span",{class:"token attr-name"},"rel"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("license"),s("span",{class:"token punctuation"},'"')]),a(),s("span",{class:"token attr-name"},"href"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("http://creativecommons.org/licenses/by-nc-sa/3.0/"),s("span",{class:"token punctuation"},'"')]),a("\n "),s("span",{class:"token punctuation"},">")]),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("img")]),a("\n "),s("span",{class:"token attr-name"},"alt"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("Creative\nCommons License"),s("span",{class:"token punctuation"},'"')]),a("\n "),s("span",{class:"token special-attr"},[s("span",{class:"token attr-name"},"style"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),s("span",{class:"token value css language-css"},[s("span",{class:"token property"},"border-width"),s("span",{class:"token punctuation"},":"),a("0")]),s("span",{class:"token punctuation"},'"')])]),a("\n "),s("span",{class:"token attr-name"},"src"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("https://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png"),s("span",{class:"token punctuation"},'"')]),a(),s("span",{class:"token punctuation"},"/>")]),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"")]),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("br")]),a(),s("span",{class:"token punctuation"},"/>")]),a("This work is licensed under a\n"),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("a")]),a(),s("span",{class:"token attr-name"},"rel"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("license"),s("span",{class:"token punctuation"},'"')]),a(),s("span",{class:"token attr-name"},"href"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("http://creativecommons.org/licenses/by-nc-sa/3.0/"),s("span",{class:"token punctuation"},'"')]),a("\n "),s("span",{class:"token punctuation"},">")]),a("Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License"),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"")]),a(".\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br")])],-1),Xs=s("div",{class:"language-markdown line-numbers-mode"},[s("pre",null,[s("code",null,[a("\\*\\*\n"),s("span",{class:"token bold"},[s("span",{class:"token punctuation"},"**"),s("span",{class:"token content"}," May you do good and not evil.\n"),s("span",{class:"token punctuation"},"**")]),a(" May you find forgiveness for yourself and forgive others.\n"),s("span",{class:"token bold"},[s("span",{class:"token punctuation"},"**"),s("span",{class:"token content"}," May you share freely, never taking more than you give.\n"),s("span",{class:"token punctuation"},"**")]),a("\n\\*\\*\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),Qs=s("div",{class:"language-markdown line-numbers-mode"},[s("pre",null,[s("code",null,"DBAD : DON'T BE A DICK PUBLIC LICENSE:\n\nDo whatever you like with the original work, just don't be a dick.\n\nBeing a dick includes - but is not limited to - the following instances:\n\n1a. Outright copyright infringement - Don't just copy this and change the name.\n1b. Selling the unmodified original with no work done what-so-ever,\nthat's REALLY being a dick.\n1c. Modifying the original work to contain hidden harmful content.\nThat would make you a PROPER dick.\n\nIf you become rich through modifications, related work services, or supporting\nthe original work, share the love. Only a dick would make loads off this work\nand not buy the original works creator(s) a pint.Code is provided with no\nwarranty. Using somebody else's code and bitching when it goes wrong makes\nyou a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix\nback.\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br")])],-1),Zs=s("div",{class:"language-markdown line-numbers-mode"},[s("pre",null,[s("code",null,"Homework Public License(HPL)\n\nCopyright (c) 2016 Yilong Liu\n\nThis is for your reference only,not for your cheating - Just don't be a dick.\n\nBeing a dick includes - but is not limited to - the following instances:\n\n1a. Outright copyright infringement - Don't just copy this and change the name.\n1b. Reserve a copy of this project and tell your teacher\nthat it is your own homework - Plagiarism is shame.\n\nIf you become rich through modifications, related work services,\nor supporting the original work, share the love. Only a dick would make loads\noff this work and not buy the original works creator(s) a pint.Code is\nprovided with no warranty. Using somebody else's code and bitching when it\ngoes wrong makes you a DONKEY dick. Fix the problem yourself. A non-dick\nwould submit the fix back.\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br")])],-1),na=s("div",{class:"language-markdown line-numbers-mode"},[s("pre",null,[s("code",null,[a('The Star And Thank Author License (SATA)\n\nCopyright (c) 2016 sabertazimi(sabertazimi@gmail.com)\n\nProject Url: https://github.com/sabertazimi/Awesome-Notes\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nAnd wait, the most important, you shall star/+1/like the project(s) in project url\nsection above first, and then thank the author(s) in Copyright section.\n\nHere are some suggested ways:\n\n'),s("span",{class:"token list punctuation"},"-"),a(" Email the authors a thank-you letter, and make friends with him/her/them.\n"),s("span",{class:"token list punctuation"},"-"),a(" Report bugs or issues.\n"),s("span",{class:"token list punctuation"},"-"),a(" Tell friends what a wonderful project this is.\n"),s("span",{class:"token list punctuation"},"-"),a(' And, sure, you can just express thanks in your mind without telling the world.\n\nContributors of this project by forking have the option to add his/her name and\nforked project url at copyright and project url sections, but shall not delete\nor modify anything else in these two sections.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n')])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br"),s("span",{class:"line-number"},"19"),s("br"),s("span",{class:"line-number"},"20"),s("br"),s("span",{class:"line-number"},"21"),s("br"),s("span",{class:"line-number"},"22"),s("br"),s("span",{class:"line-number"},"23"),s("br"),s("span",{class:"line-number"},"24"),s("br"),s("span",{class:"line-number"},"25"),s("br"),s("span",{class:"line-number"},"26"),s("br"),s("span",{class:"line-number"},"27"),s("br"),s("span",{class:"line-number"},"28"),s("br"),s("span",{class:"line-number"},"29"),s("br"),s("span",{class:"line-number"},"30"),s("br"),s("span",{class:"line-number"},"31"),s("br"),s("span",{class:"line-number"},"32"),s("br"),s("span",{class:"line-number"},"33"),s("br"),s("span",{class:"line-number"},"34"),s("br"),s("span",{class:"line-number"},"35"),s("br"),s("span",{class:"line-number"},"36"),s("br"),s("span",{class:"line-number"},"37"),s("br")])],-1),ea=s("h3",{id:"teamwork"},[s("a",{class:"header-anchor",href:"#teamwork","aria-hidden":"true"},"#"),a(" Teamwork")],-1),sa=s("p",null,"如果在组织的托管空间创建版本库,一定要要为版本库指派一个拥有 Push 权限的团队,以免以“Fork + Pull”模式工作时,Pull Request 没有人响应。",-1),aa=s("h4",{id:"pull-request-work-flow"},[s("a",{class:"header-anchor",href:"#pull-request-work-flow","aria-hidden":"true"},"#"),a(" Pull Request Work Flow")],-1),la=s("ol",null,[s("li",null,"Fork it."),s("li",null,[a("Create your feature branch ("),s("code",null,"git checkout -b my-new-feature"),a(").")]),s("li",null,"Ensure tests are passing."),s("li",null,[a("Commit changes ("),s("code",null,"git commit -am 'Add some feature'"),a(").")]),s("li",null,[a("Push to the branch ("),s("code",null,"git push origin my-new-feature"),a(").")]),s("li",null,"Create new Pull Request.")],-1),ta=s("h3",{id:"github-cli-tool"},[s("a",{class:"header-anchor",href:"#github-cli-tool","aria-hidden":"true"},"#"),a(" GitHub CLI Tool")],-1),ia=s("h4",{id:"cli-installation"},[s("a",{class:"header-anchor",href:"#cli-installation","aria-hidden":"true"},"#"),a(" CLI Installation")],-1),ra=s("p",null,[a("Install "),s("code",null,"gh"),a(" by "),s("code",null,"apt"),a(", according of "),s("a",{href:"https://github.com/cli/cli/blob/trunk/docs/install_linux.md",target:"_blank",rel:"noopener noreferrer"},"official introduction"),a(".")],-1),oa=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,"gh auth login\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),ca=s("h4",{id:"gh-issue-usage"},[s("a",{class:"header-anchor",href:"#gh-issue-usage","aria-hidden":"true"},"#"),a(" GH Issue Usage")],-1),ua=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,"gh issue create\ngh issue close\ngh issue status\ngh issue list\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br")])],-1),pa=s("h4",{id:"gh-pr-usage"},[s("a",{class:"header-anchor",href:"#gh-pr-usage","aria-hidden":"true"},"#"),a(" GH PR Usage")],-1),ba=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[a("gh "),s("span",{class:"token function"},"pr"),a(" checkout\ngh "),s("span",{class:"token function"},"pr"),a(" create\ngh "),s("span",{class:"token function"},"pr"),a(" close\ngh "),s("span",{class:"token function"},"pr"),a(" merge\ngh "),s("span",{class:"token function"},"pr"),a(" status\ngh "),s("span",{class:"token function"},"pr"),a(" list\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),da=s("h4",{id:"gh-repo-usage"},[s("a",{class:"header-anchor",href:"#gh-repo-usage","aria-hidden":"true"},"#"),a(" GH Repo Usage")],-1),ma=s("p",null,"Clone repo:",-1),ha=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[a("gh repo clone cli/cli\n\n"),s("span",{class:"token comment"},"# fastest way to clone authorized user repos"),a("\ngh "),s("span",{class:"token builtin class-name"},"alias"),a(),s("span",{class:"token builtin class-name"},"set"),a(" rc "),s("span",{class:"token string"},"'repo clone'"),a("\ngh rc dragon\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br")])],-1),ga=s("p",null,"Create repo:",-1),ka=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# create a repository under your account using the current directory name"),a("\n$ "),s("span",{class:"token function"},"git"),a(" init my-project\n$ "),s("span",{class:"token builtin class-name"},"cd"),a(" my-project\n$ gh repo create\n\n"),s("span",{class:"token comment"},"# create a repository with a specific name"),a("\n$ gh repo create my-project\n\n"),s("span",{class:"token comment"},"# create a repository in an organization"),a("\n$ gh repo create cli/my-project\n\n"),s("span",{class:"token comment"},"# disable issues and wiki"),a("\n$ gh repo create --enable-issues"),s("span",{class:"token operator"},"="),a("false --enable-wiki"),s("span",{class:"token operator"},"="),a("false\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br")])],-1),fa=s("p",null,"Push repo:",-1),va=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" init\n\n"),s("span",{class:"token builtin class-name"},"echo"),a(),s("span",{class:"token string"},'"# RepoName"'),a(),s("span",{class:"token operator"},">>"),a(" README.md\n"),s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"add"),a(" README.md\n"),s("span",{class:"token function"},"git"),a(" commit -m "),s("span",{class:"token string"},'"Initial commit"'),a("\n\n"),s("span",{class:"token function"},"git"),a(" remote "),s("span",{class:"token function"},"add"),a(" origin git@github.com:username/RepoName.git\n"),s("span",{class:"token function"},"git"),a(" push -u origin master\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br")])],-1),wa=s("p",null,"List repo:",-1),ya=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,"gh repo list sabertazimi\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),xa=s("h3",{id:"wiki"},[s("a",{class:"header-anchor",href:"#wiki","aria-hidden":"true"},"#"),a(" Wiki")],-1),Ca=s("h4",{id:"wiki-git-access"},[s("a",{class:"header-anchor",href:"#wiki-git-access","aria-hidden":"true"},"#"),a(" Wiki Git Access")],-1),Aa=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" clone git@github.com:user/repo.wiki.git\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ga=s("h3",{id:"shorten-github-url"},[s("a",{class:"header-anchor",href:"#shorten-github-url","aria-hidden":"true"},"#"),a(" Shorten GitHub URL")],-1),Ia=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"curl"),a(" -i http://git.io -F "),s("span",{class:"token string"},'"url=https://github.com/technoweenie"'),a(" -F "),s("span",{class:"token string"},'"code=t"'),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ea=s("h3",{id:"github-flavored-markdown"},[s("a",{class:"header-anchor",href:"#github-flavored-markdown","aria-hidden":"true"},"#"),a(" GitHub Flavored Markdown")],-1),Ta=s("h4",{id:"link"},[s("a",{class:"header-anchor",href:"#link","aria-hidden":"true"},"#"),a(" Link")],-1),Na=s("h5",{id:"tooltip-of-link"},[s("a",{class:"header-anchor",href:"#tooltip-of-link","aria-hidden":"true"},"#"),a(" Tooltip of Link")],-1),Oa=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,"This is a [link to a web page](https://url.com 'This title will appear as a tooltip').\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ra=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,"![Alt text](https://imageurl.com 'This is a title')\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Sa=s("h5",{id:"label-of-link"},[s("a",{class:"header-anchor",href:"#label-of-link","aria-hidden":"true"},"#"),a(" Label of Link")],-1),Ha=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,[a("This is a "),s("span",{class:"token url"},[a("["),s("span",{class:"token content"},"link to a web page"),a("]["),s("span",{class:"token variable"},"mylabel"),a("]")]),a(".\n\nThen at the end of the document …\n\n"),s("span",{class:"token url-reference url"},[s("span",{class:"token punctuation"},"["),s("span",{class:"token variable"},"mylabel"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},":"),a(" https://url.com "),s("span",{class:"token string"},"'Optional title'")]),a("\n"),s("span",{class:"token url-reference url"},[s("span",{class:"token punctuation"},"["),s("span",{class:"token variable"},"mylabel"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},":"),a(" https://url.com "),s("span",{class:"token string"},"'Optional title'")]),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),La=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token url"},[s("span",{class:"token operator"},"!"),a("["),s("span",{class:"token content"},"Alt text"),a("]["),s("span",{class:"token variable"},"mylabel"),a("]")]),a("\n\n"),s("span",{class:"token url-reference url"},[s("span",{class:"token punctuation"},"["),s("span",{class:"token variable"},"mylabel"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},":"),a(" https://imageurl.com "),s("span",{class:"token string"},"'This is a title'")]),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),ja=s("h3",{id:"github-pages"},[s("a",{class:"header-anchor",href:"#github-pages","aria-hidden":"true"},"#"),a(" GitHub Pages")],-1),Da=s("p",null,[a("In "),s("code",null,"https://github.com///settings/pages"),a(", setup "),s("code",null,"source"),a(" of pages and "),s("code",null,"Enforce HTTPS"),a(".")],-1),Pa=s("h2",{id:"git-tools"},[s("a",{class:"header-anchor",href:"#git-tools","aria-hidden":"true"},"#"),a(" Git Tools")],-1),Ba=s("h3",{id:"diff-and-patch"},[s("a",{class:"header-anchor",href:"#diff-and-patch","aria-hidden":"true"},"#"),a(" Diff and Patch")],-1),_a=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"diff"),a(" -u "),s("span",{class:"token operator"},"<"),a("src"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("dist"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"diff"),a(" -Nur "),s("span",{class:"token operator"},"<"),a("src_dir"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("dist_dir"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Ma=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[a("patch -p"),s("span",{class:"token punctuation"},"["),a("num"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token operator"},"<"),a(" patchFile\npatch -dry -run -p"),s("span",{class:"token punctuation"},"["),a("num"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token operator"},"<"),a(" patchFile\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Fa=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"diff"),a(" -Nur program_1.0 program_2.0 "),s("span",{class:"token operator"},">"),a(" program_2.0.patch\npatch -p1 "),s("span",{class:"token operator"},"<"),s("span",{class:"token punctuation"},".."),a("/program_2.0.patch\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Ua=s("h3",{id:"semantic-git-commit-message"},[s("a",{class:"header-anchor",href:"#semantic-git-commit-message","aria-hidden":"true"},"#"),a(" Semantic Git Commit Message")],-1),$a=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/commitizen/cz-cli",target:"_blank",rel:"noopener noreferrer"},"cz-cli")]),s("li",null,[s("a",{href:"https://github.com/commitizen/cz-conventional-changelog",target:"_blank",rel:"noopener noreferrer"},"cz-conventional-changelog")]),s("li",null,[s("a",{href:"https://github.com/conventional-changelog/commitlint",target:"_blank",rel:"noopener noreferrer"},"Commit Linter")])],-1),qa=s("h3",{id:"changelog-generator"},[s("a",{class:"header-anchor",href:"#changelog-generator","aria-hidden":"true"},"#"),a(" Changelog Generator")],-1),za=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/conventional-changelog/standard-version",target:"_blank",rel:"noopener noreferrer"},"standard-version")])],-1),Wa=s("h3",{id:"purge-tool"},[s("a",{class:"header-anchor",href:"#purge-tool","aria-hidden":"true"},"#"),a(" Purge Tool")],-1),Ya=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" rev-list --objects --all\n"),s("span",{class:"token punctuation"},"\\"),a(),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"grep"),a(),s("span",{class:"token string"},[a('"'),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"git"),a(" verify-pack -v .git/objects/pack/*.idx\n"),s("span",{class:"token punctuation"},"\\"),a(),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"sort"),a(" -k "),s("span",{class:"token number"},"3"),a(" -n "),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"tail"),a(" -5 "),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"awk"),a(),s("span",{class:"token string"},"'{print$1}'"),s("span",{class:"token variable"},")")]),a('"')]),a("\n"),s("span",{class:"token function"},"git"),a(" filter-branch -f --prune-empty --index-filter\n"),s("span",{class:"token punctuation"},"\\"),a(),s("span",{class:"token string"},"'git rm -rf --cached --ignore-unmatch your-file-name'"),a("\n"),s("span",{class:"token punctuation"},"\\"),a(" --tag-name-filter "),s("span",{class:"token function"},"cat"),a(" -- --all\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),Va=s("h2",{id:"commands-list"},[s("a",{class:"header-anchor",href:"#commands-list","aria-hidden":"true"},"#"),a(" Commands List")],-1),Ka=s("h3",{id:"basic-commands"},[s("a",{class:"header-anchor",href:"#basic-commands","aria-hidden":"true"},"#"),a(" Basic Commands")],-1),Ja=s("h4",{id:"git-config"},[s("a",{class:"header-anchor",href:"#git-config","aria-hidden":"true"},"#"),a(" git config")],-1),Xa=s("h4",{id:"git-help"},[s("a",{class:"header-anchor",href:"#git-help","aria-hidden":"true"},"#"),a(" git help")],-1),Qa=s("h4",{id:"git-init"},[s("a",{class:"header-anchor",href:"#git-init","aria-hidden":"true"},"#"),a(" git init")],-1),Za=s("h4",{id:"git-clone"},[s("a",{class:"header-anchor",href:"#git-clone","aria-hidden":"true"},"#"),a(" git clone")],-1),nl=s("p",null,"clone specific branch",-1),el=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" clone -b branch_name repo_url\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),sl=s("h4",{id:"git-add"},[s("a",{class:"header-anchor",href:"#git-add","aria-hidden":"true"},"#"),a(" git add")],-1),al=s("h4",{id:"git-status"},[s("a",{class:"header-anchor",href:"#git-status","aria-hidden":"true"},"#"),a(" git status")],-1),ll=s("h4",{id:"git-diff"},[s("a",{class:"header-anchor",href:"#git-diff","aria-hidden":"true"},"#"),a(" git diff")],-1),tl=s("h4",{id:"git-difftool"},[s("a",{class:"header-anchor",href:"#git-difftool","aria-hidden":"true"},"#"),a(" git difftool")],-1),il=s("p",null,"外置 diff 工具",-1),rl=s("h4",{id:"git-commit"},[s("a",{class:"header-anchor",href:"#git-commit","aria-hidden":"true"},"#"),a(" git commit")],-1),ol=s("h4",{id:"git-reset"},[s("a",{class:"header-anchor",href:"#git-reset","aria-hidden":"true"},"#"),a(" git reset")],-1),cl=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" reset "),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"git"),a(" merge-base master "),s("span",{class:"token punctuation"},"$("),a("git rev-parse --abbrev-ref HEAD"),s("span",{class:"token punctuation"},")"),s("span",{class:"token variable"},")")]),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),ul=s("ul",null,[s("li",null,[s("code",null,"git rev-parse --abbrev-rev HEAD"),a(" will return the name of the branch currently on")]),s("li",null,[s("code",null,"git merge-base master $(name of your branch)"),a(" will find the best common ancestor between master and current branch")]),s("li",null,[s("code",null,"git reset $(hash of the branch creation)"),a(" will undo all the commits, merges, rebase (preserving changes to the code)")])],-1),pl=s("h4",{id:"git-rm"},[s("a",{class:"header-anchor",href:"#git-rm","aria-hidden":"true"},"#"),a(" git rm")],-1),bl=s("h4",{id:"git-mv"},[s("a",{class:"header-anchor",href:"#git-mv","aria-hidden":"true"},"#"),a(" git mv")],-1),dl=s("h4",{id:"git-clean"},[s("a",{class:"header-anchor",href:"#git-clean","aria-hidden":"true"},"#"),a(" git clean")],-1),ml=s("p",null,"从工作区中移除不想要的文件。可以是编译的临时文件或者合并冲突的文件。",-1),hl=s("h4",{id:"git-branch"},[s("a",{class:"header-anchor",href:"#git-branch","aria-hidden":"true"},"#"),a(" git branch")],-1),gl=s("h4",{id:"git-checkout"},[s("a",{class:"header-anchor",href:"#git-checkout","aria-hidden":"true"},"#"),a(" git checkout")],-1),kl=s("h4",{id:"git-merge"},[s("a",{class:"header-anchor",href:"#git-merge","aria-hidden":"true"},"#"),a(" git merge")],-1),fl=s("h4",{id:"git-mergetool"},[s("a",{class:"header-anchor",href:"#git-mergetool","aria-hidden":"true"},"#"),a(" git mergetool")],-1),vl=s("p",null,"外置 merge 工具",-1),wl=s("h4",{id:"git-log"},[s("a",{class:"header-anchor",href:"#git-log","aria-hidden":"true"},"#"),a(" git log")],-1),yl=s("h4",{id:"git-stash"},[s("a",{class:"header-anchor",href:"#git-stash","aria-hidden":"true"},"#"),a(" git stash")],-1),xl=s("p",null,"临时地保存一些还没有提交的工作,以便在分支上不需要提交未完成工作就可以清理工作目录。",-1),Cl=s("h4",{id:"git-tag"},[s("a",{class:"header-anchor",href:"#git-tag","aria-hidden":"true"},"#"),a(" git tag")],-1),Al=s("h4",{id:"git-fetch"},[s("a",{class:"header-anchor",href:"#git-fetch","aria-hidden":"true"},"#"),a(" git fetch")],-1),Gl=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" fetch "),s("span",{class:"token operator"},"<"),a("repo_name"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("branch_name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Il=s("h4",{id:"git-pull"},[s("a",{class:"header-anchor",href:"#git-pull","aria-hidden":"true"},"#"),a(" git pull")],-1),El=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" pull --rebase\n"),s("span",{class:"token function"},"git"),a(" pull --allow-unrelated-histories\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Tl=s("h4",{id:"git-push"},[s("a",{class:"header-anchor",href:"#git-push","aria-hidden":"true"},"#"),a(" git push")],-1),Nl=s("h4",{id:"git-remote"},[s("a",{class:"header-anchor",href:"#git-remote","aria-hidden":"true"},"#"),a(" git remote")],-1),Ol=s("h4",{id:"git-archive"},[s("a",{class:"header-anchor",href:"#git-archive","aria-hidden":"true"},"#"),a(" git archive")],-1),Rl=s("p",null,"创建项目一个指定快照的归档文件",-1),Sl=s("h4",{id:"git-submodule"},[s("a",{class:"header-anchor",href:"#git-submodule","aria-hidden":"true"},"#"),a(" git submodule")],-1),Hl=s("p",null,"管理一个仓库的其他外部仓库。 它可以被用在库或者其他类型的共 享资源上.submodule 命令有几个子命令, 如( add 、 update 、 sync 等等)用来管理这些 资源.",-1),Ll=s("ul",null,[s("li",null,"add submodule")],-1),jl=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" submodule "),s("span",{class:"token function"},"add"),a(" git://github.com/rack/rack.git ./lib/rack\n"),s("span",{class:"token function"},"cat"),a(" .gitmodules\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Dl=s("ul",null,[s("li",null,"get submodule")],-1),Pl=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" submodule init\n"),s("span",{class:"token function"},"git"),a(" submodule update\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Bl=s("ul",null,[s("li",null,"sync submodule")],-1),_l=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" pull origin/master --rebase\n"),s("span",{class:"token function"},"git"),a(" submodule update\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Ml=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" submodule update --init --force --remote\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Fl=s("h3",{id:"检查与比较"},[s("a",{class:"header-anchor",href:"#检查与比较","aria-hidden":"true"},"#"),a(" 检查与比较")],-1),Ul=s("h4",{id:"git-show"},[s("a",{class:"header-anchor",href:"#git-show","aria-hidden":"true"},"#"),a(" git show")],-1),$l=s("h4",{id:"git-shortlog"},[s("a",{class:"header-anchor",href:"#git-shortlog","aria-hidden":"true"},"#"),a(" git shortlog")],-1),ql=s("p",null,"创建一个漂亮的 changelog 文件",-1),zl=s("h4",{id:"git-describe"},[s("a",{class:"header-anchor",href:"#git-describe","aria-hidden":"true"},"#"),a(" git describe")],-1),Wl=s("p",null,"接受任何可以解析成一个提交的东西,然后生成一个人类可读的字符串且不可变。 这是一种获得一个提交的描述的方式,它跟一个提交的 SHA-1 值一样是无歧义,但是更具可读性。",-1),Yl=s("h3",{id:"调试"},[s("a",{class:"header-anchor",href:"#调试","aria-hidden":"true"},"#"),a(" 调试")],-1),Vl=s("h4",{id:"git-bisect"},[s("a",{class:"header-anchor",href:"#git-bisect","aria-hidden":"true"},"#"),a(" git bisect")],-1),Kl=s("p",null,"通过自动进行一个二分查找来找到哪一个特定的提交是导致 bug 或者问题的第一个提交。",-1),Jl=s("h4",{id:"git-blame"},[s("a",{class:"header-anchor",href:"#git-blame","aria-hidden":"true"},"#"),a(" git blame")],-1),Xl=s("h4",{id:"git-grep"},[s("a",{class:"header-anchor",href:"#git-grep","aria-hidden":"true"},"#"),a(" git grep")],-1),Ql=s("p",null,"查找任何字符串或者正则表达式",-1),Zl=s("h3",{id:"补丁"},[s("a",{class:"header-anchor",href:"#补丁","aria-hidden":"true"},"#"),a(" 补丁")],-1),nt=s("h4",{id:"git-cherry-pick"},[s("a",{class:"header-anchor",href:"#git-cherry-pick","aria-hidden":"true"},"#"),a(" git cherry-pick")],-1),et=s("p",null,"获得在单个提交中引入的变更,然后尝试将作为一个新的提交引入到你当前分支上",-1),st=s("h4",{id:"git-rebase"},[s("a",{class:"header-anchor",href:"#git-rebase","aria-hidden":"true"},"#"),a(" git rebase")],-1),at=s("h4",{id:"git-revert"},[s("a",{class:"header-anchor",href:"#git-revert","aria-hidden":"true"},"#"),a(" git revert")],-1),lt=s("h3",{id:"邮件"},[s("a",{class:"header-anchor",href:"#邮件","aria-hidden":"true"},"#"),a(" 邮件")],-1),tt=s("h4",{id:"git-apply"},[s("a",{class:"header-anchor",href:"#git-apply","aria-hidden":"true"},"#"),a(" git apply")],-1),it=s("p",null,"应用一个通过 git diff 或者甚至使用 GNU diff 命令创建的补丁",-1),rt=s("h4",{id:"git-am"},[s("a",{class:"header-anchor",href:"#git-am","aria-hidden":"true"},"#"),a(" git am")],-1),ot=s("p",null,"应用来自邮箱的补丁",-1),ct=s("h4",{id:"git-format-patch"},[s("a",{class:"header-anchor",href:"#git-format-patch","aria-hidden":"true"},"#"),a(" git format-patch")],-1),ut=s("p",null,"mailbox 的格式来生成一系列的补丁以便你可以发送到一个邮件列表中",-1),pt=s("h4",{id:"git-imap-send"},[s("a",{class:"header-anchor",href:"#git-imap-send","aria-hidden":"true"},"#"),a(" git imap-send")],-1),bt=s("p",null,"将一个由 git format-patch 生成的邮箱上传至 IMAP 草稿文件夹",-1),dt=s("h4",{id:"git-send-email"},[s("a",{class:"header-anchor",href:"#git-send-email","aria-hidden":"true"},"#"),a(" git send-email")],-1),mt=s("p",null,"通过邮件发送那些使用 git format-patch 生成的补丁",-1),ht=s("h4",{id:"git-request-pull"},[s("a",{class:"header-anchor",href:"#git-request-pull","aria-hidden":"true"},"#"),a(" git request-pull")],-1),gt=s("h3",{id:"外部系统"},[s("a",{class:"header-anchor",href:"#外部系统","aria-hidden":"true"},"#"),a(" 外部系统")],-1),kt=s("h4",{id:"git-svn"},[s("a",{class:"header-anchor",href:"#git-svn","aria-hidden":"true"},"#"),a(" git svn")],-1),ft=s("h4",{id:"git-fast-import"},[s("a",{class:"header-anchor",href:"#git-fast-import","aria-hidden":"true"},"#"),a(" git fast-import")],-1),vt=s("p",null,"对于其他版本控制系统或者从其他任何的格式导入,你可以使用 git fast-import 快速地将其他格式映射到 Git 可以轻松记录的格式",-1),wt=s("h3",{id:"管理"},[s("a",{class:"header-anchor",href:"#管理","aria-hidden":"true"},"#"),a(" 管理")],-1),yt=s("h4",{id:"git-gc"},[s("a",{class:"header-anchor",href:"#git-gc","aria-hidden":"true"},"#"),a(" git gc")],-1),xt=s("p",null,"在你的仓库中执行 ``garbage collection'' ,删除数据库中不需要的文件和将其他文件打包成一种更有效的格式",-1),Ct=s("h4",{id:"git-fsck"},[s("a",{class:"header-anchor",href:"#git-fsck","aria-hidden":"true"},"#"),a(" git fsck")],-1),At=s("p",null,"检查内部数据库的问题或者不一致性",-1),Gt=s("h4",{id:"git-reflog"},[s("a",{class:"header-anchor",href:"#git-reflog","aria-hidden":"true"},"#"),a(" git reflog")],-1),It=s("p",null,"分析你所有分支的头指针的日志来查找出你在重写历史上可能丢失的提交",-1),Et=s("h4",{id:"git-filter-branch"},[s("a",{class:"header-anchor",href:"#git-filter-branch","aria-hidden":"true"},"#"),a(" git filter-branch")],-1),Tt=s("p",null,"根据某些规则来重写大量的提交记录,例如从任何地方删除文件,或者通过过滤一个仓库中的一个单独的子目录以提取出一个项目",-1),Nt=s("h4",{id:"git-note"},[s("a",{class:"header-anchor",href:"#git-note","aria-hidden":"true"},"#"),a(" git-note")],-1),Ot=s("p",null,"为特定 commit 添加 note,一个 commit 只能有一个 note",-1);t.render=function(s,a,l,t,Rt,St){return n(),e("div",null,[i,r,o,c,u,p,b,d,m,h,g,k,f,v,w,y,x,C,A,G,I,E,T,N,O,R,S,H,L,j,D,P,B,_,M,F,U,$,q,z,W,Y,V,K,J,X,Q,Z,nn,en,sn,an,ln,tn,rn,on,cn,un,pn,bn,dn,mn,hn,gn,kn,fn,vn,wn,yn,xn,Cn,An,Gn,In,En,Tn,Nn,On,Rn,Sn,Hn,Ln,jn,Dn,Pn,Bn,_n,Mn,Fn,Un,$n,qn,zn,Wn,Yn,Vn,Kn,Jn,Xn,Qn,Zn,ne,ee,se,ae,le,te,ie,re,oe,ce,ue,pe,be,de,me,he,ge,ke,fe,ve,we,ye,xe,Ce,Ae,Ge,Ie,Ee,Te,Ne,Oe,Re,Se,He,Le,je,De,Pe,Be,_e,Me,Fe,Ue,$e,qe,ze,We,Ye,Ve,Ke,Je,Xe,Qe,Ze,ns,es,ss,as,ls,ts,is,rs,os,cs,us,ps,bs,ds,ms,hs,gs,ks,fs,vs,ws,ys,xs,Cs,As,Gs,Is,Es,Ts,Ns,Os,Rs,Ss,Hs,Ls,js,Ds,Ps,Bs,_s,Ms,Fs,Us,$s,qs,zs,Ws,Ys,Vs,Ks,Js,Xs,Qs,Zs,na,ea,sa,aa,la,ta,ia,ra,oa,ca,ua,pa,ba,da,ma,ha,ga,ka,fa,va,wa,ya,xa,Ca,Aa,Ga,Ia,Ea,Ta,Na,Oa,Ra,Sa,Ha,La,ja,Da,Pa,Ba,_a,Ma,Fa,Ua,$a,qa,za,Wa,Ya,Va,Ka,Ja,Xa,Qa,Za,nl,el,sl,al,ll,tl,il,rl,ol,cl,ul,pl,bl,dl,ml,hl,gl,kl,fl,vl,wl,yl,xl,Cl,Al,Gl,Il,El,Tl,Nl,Ol,Rl,Sl,Hl,Ll,jl,Dl,Pl,Bl,_l,Ml,Fl,Ul,$l,ql,zl,Wl,Yl,Vl,Kl,Jl,Xl,Ql,Zl,nt,et,st,at,lt,tt,it,rt,ot,ct,ut,pt,bt,dt,mt,ht,gt,kt,ft,vt,wt,yt,xt,Ct,At,Gt,It,Et,Tt,Nt,Ot])};export default t;export{l as __pageData}; diff --git a/assets/programming_tools_git_gitBasicNotes.md.abd5ba70.lean.js b/assets/programming_tools_git_gitBasicNotes.md.abd5ba70.lean.js new file mode 100644 index 00000000000..07d85e20c30 --- /dev/null +++ b/assets/programming_tools_git_gitBasicNotes.md.abd5ba70.lean.js @@ -0,0 +1 @@ +import{o as n,c as e,b as s,d as a}from"./app.6dd2a1b7.js";const l='{"title":"Git Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Git Configuration","slug":"git-configuration"},{"level":3,"title":"Basic Configuration","slug":"basic-configuration"},{"level":3,"title":"Proxy Configuration","slug":"proxy-configuration"},{"level":3,"title":"List and Help","slug":"list-and-help"},{"level":2,"title":"File State","slug":"file-state"},{"level":2,"title":"Git Ignore File","slug":"git-ignore-file"},{"level":2,"title":"Diff","slug":"diff"},{"level":2,"title":"Add","slug":"add"},{"level":2,"title":"Commit","slug":"commit"},{"level":3,"title":"Commit Style Guide","slug":"commit-style-guide"},{"level":2,"title":"Stash","slug":"stash"},{"level":2,"title":"Revert","slug":"revert"},{"level":2,"title":"Remove","slug":"remove"},{"level":2,"title":"Move","slug":"move"},{"level":2,"title":"Log","slug":"log"},{"level":3,"title":"Pretty Format","slug":"pretty-format"},{"level":3,"title":"Log Options","slug":"log-options"},{"level":2,"title":"Reflog","slug":"reflog"},{"level":2,"title":"Show","slug":"show"},{"level":2,"title":"Remote","slug":"remote"},{"level":2,"title":"Tag","slug":"tag"},{"level":2,"title":"Alias","slug":"alias"},{"level":2,"title":"Merge","slug":"merge"},{"level":2,"title":"Rebase","slug":"rebase"},{"level":2,"title":"Branch","slug":"branch"},{"level":3,"title":"Basic Workflow Commands","slug":"basic-workflow-commands"},{"level":3,"title":"Advanced Branch Workflow","slug":"advanced-branch-workflow"},{"level":2,"title":"Git Inside","slug":"git-inside"},{"level":3,"title":"Add Inside","slug":"add-inside"},{"level":3,"title":"Commit Inside","slug":"commit-inside"},{"level":3,"title":"Checkout Inside","slug":"checkout-inside"},{"level":3,"title":"Merge Inside","slug":"merge-inside"},{"level":3,"title":"Fetch Inside","slug":"fetch-inside"},{"level":3,"title":"Clone Inside","slug":"clone-inside"},{"level":3,"title":"Push Inside","slug":"push-inside"},{"level":3,"title":"HEAD Branch Inside","slug":"head-branch-inside"},{"level":3,"title":"Git Objects","slug":"git-objects"},{"level":2,"title":"GitHub","slug":"github"},{"level":3,"title":"GPG Usage","slug":"gpg-usage"},{"level":3,"title":"LICENSE","slug":"license"},{"level":3,"title":"Teamwork","slug":"teamwork"},{"level":3,"title":"GitHub CLI Tool","slug":"github-cli-tool"},{"level":3,"title":"Wiki","slug":"wiki"},{"level":3,"title":"Shorten GitHub URL","slug":"shorten-github-url"},{"level":3,"title":"GitHub Flavored Markdown","slug":"github-flavored-markdown"},{"level":3,"title":"GitHub Pages","slug":"github-pages"},{"level":2,"title":"Git Tools","slug":"git-tools"},{"level":3,"title":"Diff and Patch","slug":"diff-and-patch"},{"level":3,"title":"Semantic Git Commit Message","slug":"semantic-git-commit-message"},{"level":3,"title":"Changelog Generator","slug":"changelog-generator"},{"level":3,"title":"Purge Tool","slug":"purge-tool"},{"level":2,"title":"Commands List","slug":"commands-list"},{"level":3,"title":"Basic Commands","slug":"basic-commands"},{"level":3,"title":"检查与比较","slug":"检查与比较"},{"level":3,"title":"调试","slug":"调试"},{"level":3,"title":"补丁","slug":"补丁"},{"level":3,"title":"邮件","slug":"邮件"},{"level":3,"title":"外部系统","slug":"外部系统"},{"level":3,"title":"管理","slug":"管理"}],"relativePath":"programming/tools/git/gitBasicNotes.md","lastUpdated":1627953616000}',t={},i=s("h1",{id:"git-basic-notes"},[s("a",{class:"header-anchor",href:"#git-basic-notes","aria-hidden":"true"},"#"),a(" Git Basic Notes")],-1),r=s("p",null,[s("div",{class:"table-of-contents"},[s("ul",null,[s("li",null,[s("a",{href:"#git-basic-notes"},"Git Basic Notes"),s("ul",null,[s("li",null,[s("a",{href:"#git-configuration"},"Git Configuration")]),s("li",null,[s("a",{href:"#file-state"},"File State")]),s("li",null,[s("a",{href:"#git-ignore-file"},"Git Ignore File")]),s("li",null,[s("a",{href:"#diff"},"Diff")]),s("li",null,[s("a",{href:"#add"},"Add")]),s("li",null,[s("a",{href:"#commit"},"Commit")]),s("li",null,[s("a",{href:"#stash"},"Stash")]),s("li",null,[s("a",{href:"#revert"},"Revert")]),s("li",null,[s("a",{href:"#remove"},"Remove")]),s("li",null,[s("a",{href:"#move"},"Move")]),s("li",null,[s("a",{href:"#log"},"Log")]),s("li",null,[s("a",{href:"#reflog"},"Reflog")]),s("li",null,[s("a",{href:"#show"},"Show")]),s("li",null,[s("a",{href:"#remote"},"Remote")]),s("li",null,[s("a",{href:"#tag"},"Tag")]),s("li",null,[s("a",{href:"#alias"},"Alias")]),s("li",null,[s("a",{href:"#merge"},"Merge")]),s("li",null,[s("a",{href:"#rebase"},"Rebase")]),s("li",null,[s("a",{href:"#branch"},"Branch")]),s("li",null,[s("a",{href:"#git-inside"},"Git Inside")]),s("li",null,[s("a",{href:"#github"},"GitHub")]),s("li",null,[s("a",{href:"#git-tools"},"Git Tools")]),s("li",null,[s("a",{href:"#commands-list"},"Commands List")])])])])])],-1),o=s("h2",{id:"git-configuration"},[s("a",{class:"header-anchor",href:"#git-configuration","aria-hidden":"true"},"#"),a(" Git Configuration")],-1),c=s("ul",null,[s("li",null,"/etc/gitconfig"),s("li",null,"~/.gitconfig 或 ~/.config/git/config"),s("li",null,"repo/.git/config")],-1),u=s("h3",{id:"basic-configuration"},[s("a",{class:"header-anchor",href:"#basic-configuration","aria-hidden":"true"},"#"),a(" Basic Configuration")],-1),p=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --global user.name "),s("span",{class:"token string"},'"sabertazimi"'),a("\n"),s("span",{class:"token function"},"git"),a(" config --global user.email sabertazimi@gmail.com\n"),s("span",{class:"token function"},"git"),a(" config --global core.editor "),s("span",{class:"token function"},"vim"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global credential.helper store\n"),s("span",{class:"token function"},"git"),a(" config --global color.ui "),s("span",{class:"token boolean"},"true"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br")])],-1),b=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --global commit.template "),s("span",{class:"token environment constant"},"$HOME"),a("/.GitCommit.md\n"),s("span",{class:"token function"},"git"),a(" config --global commit.gpgsign "),s("span",{class:"token boolean"},"true"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global user.signingkey "),s("span",{class:"token operator"},"<"),a("pub-keyID"),s("span",{class:"token operator"},">"),a("\n\n"),s("span",{class:"token function"},"git"),a(" config --global push.default simple\n"),s("span",{class:"token function"},"git"),a(" config --global merge.conflictstyle "),s("span",{class:"token function"},"diff3"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global pull.rebase "),s("span",{class:"token boolean"},"true"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global rebase.autoStash "),s("span",{class:"token boolean"},"true"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br")])],-1),d=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# after 1s, git auto correct wrong command"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global help.autocorrect "),s("span",{class:"token number"},"10"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),m=s("h3",{id:"proxy-configuration"},[s("a",{class:"header-anchor",href:"#proxy-configuration","aria-hidden":"true"},"#"),a(" Proxy Configuration")],-1),h=s("ul",null,[s("li",null,[s("a",{href:"http://github.com.cnpmjs.org",target:"_blank",rel:"noopener noreferrer"},"github.com.cnpmjs.org")]),s("li",null,[s("a",{href:"http://hub.fastgit.org",target:"_blank",rel:"noopener noreferrer"},"hub.fastgit.org")]),s("li",null,[s("a",{href:"http://raw.fastgit.org",target:"_blank",rel:"noopener noreferrer"},"raw.fastgit.org")])],-1),g=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# speed up by cnpmjs"),a("\n"),s("span",{class:"token comment"},"# git clone/push/pull works"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global url."),s("span",{class:"token string"},'"https://github.com.cnpmjs.org/"'),a(".insteadOf "),s("span",{class:"token string"},'"https://github.com/"'),a("\n\n"),s("span",{class:"token comment"},"# post buffer config"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global http.postbuffer "),s("span",{class:"token number"},"524288000"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global http.postbuffer "),s("span",{class:"token number"},"1048576000"),a("\n\n"),s("span",{class:"token comment"},"# proxy"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global http.proxy "),s("span",{class:"token string"},"'socks5://127.0.0.1:1080'"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global https.proxy "),s("span",{class:"token string"},"'socks5://127.0.0.1:1080'"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br")])],-1),k=s("h3",{id:"list-and-help"},[s("a",{class:"header-anchor",href:"#list-and-help","aria-hidden":"true"},"#"),a(" List and Help")],-1),f=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --list\n"),s("span",{class:"token function"},"git"),a(" --help\n"),s("span",{class:"token function"},"man"),a(" git-\n"),s("span",{class:"token function"},"git"),a(),s("span",{class:"token builtin class-name"},"help"),a("\n"),s("span",{class:"token function"},"git"),a(),s("span",{class:"token builtin class-name"},"help"),a(" config\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br")])],-1),v=s("h2",{id:"file-state"},[s("a",{class:"header-anchor",href:"#file-state","aria-hidden":"true"},"#"),a(" File State")],-1),w=s("ul",null,[s("li",null,"Untracked"),s("li",null,[a("Unmodified("),s("strong",null,"Stable State"),a(")")]),s("li",null,"Modified"),s("li",null,"Staged")],-1),y=s("h2",{id:"git-ignore-file"},[s("a",{class:"header-anchor",href:"#git-ignore-file","aria-hidden":"true"},"#"),a(" Git Ignore File")],-1),x=s("p",null,"文件 .gitignore 的格式规范如下:",-1),C=s("ul",null,[s("li",null,"所有空行或者以 # 开头的行都会被 Git 忽略"),s("li",null,"可以使用标准的 glob 模式(简化正则表达式)匹配"),s("li",null,"匹配模式可以以( / )开头防止递归"),s("li",null,"匹配模式可以以( / )结尾指定目录"),s("li",null,"要跟踪指定模式以外的文件或目录,可以在模式前加上惊叹号( ! )取反"),s("li",null,[s("a",{href:"https://github.com/github/gitignore",target:"_blank",rel:"noopener noreferrer"},"GitHub gitignore Style")])],-1),A=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# no .a files"),a("\n*.a\n\n"),s("span",{class:"token comment"},"# but do track lib.a, even though you're ignoring .a files above"),a("\n"),s("span",{class:"token operator"},"!"),a("lib.a\n\n"),s("span",{class:"token comment"},"# only ignore the TODO file in the current directory, not subDir/TODO"),a("\n/TODO\n\n"),s("span",{class:"token comment"},"# ignore all files in the build/ directory"),a("\nbuild/\n\n"),s("span",{class:"token comment"},"# ignore doc/notes.txt, but not doc/server/arch.txt"),a("\ndoc/*.txt\n\n"),s("span",{class:"token comment"},"# ignore all .pdf files in the doc/ directory"),a("\ndoc/**/*.pdf\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br")])],-1),G=s("h2",{id:"diff"},[s("a",{class:"header-anchor",href:"#diff","aria-hidden":"true"},"#"),a(" Diff")],-1),I=s("p",null,"查看未暂存(un-staged)差异",-1),E=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"diff"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),T=s("p",null,"查看已暂存(staged)差异",-1),N=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"diff"),a(" --staged\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),O=s("p",null,"显示空白字符错误(space/tab/return)",-1),R=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"diff"),a(" --check\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),S=s("h2",{id:"add"},[s("a",{class:"header-anchor",href:"#add","aria-hidden":"true"},"#"),a(" Add")],-1),H=s("ul",null,[s("li",null,"交互式的选择 add 特定部分")],-1),L=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"add"),a(" -p\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),j=s("h2",{id:"commit"},[s("a",{class:"header-anchor",href:"#commit","aria-hidden":"true"},"#"),a(" Commit")],-1),D=s("ul",null,[s("li",null,"-a: 跳过暂存阶段(git add)"),s("li",null,"-v: 显示详细 diff 信息")],-1),P=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" commit -a -v\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),B=s("p",null,"重新提交",-1),_=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" commit --amend -a -v\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),M=s("h3",{id:"commit-style-guide"},[s("a",{class:"header-anchor",href:"#commit-style-guide","aria-hidden":"true"},"#"),a(" Commit Style Guide")],-1),F=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/conventional-commits/conventionalcommits.org",target:"_blank",rel:"noopener noreferrer"},"Conventional Commits Specification")]),s("li",null,[s("a",{href:"https://github.com/conventional-changelog/commitlint",target:"_blank",rel:"noopener noreferrer"},"Commit Lint")]),s("li",null,[s("a",{href:"https://github.com/commitizen/cz-cli",target:"_blank",rel:"noopener noreferrer"},"Commitizen: Conventional commits CLI tool")]),s("li",null,[s("a",{href:"https://github.com/conventional-changelog/standard-version",target:"_blank",rel:"noopener noreferrer"},"Standard Version: Automate versioning and CHANGELOG generation")])],-1),U=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"npm"),a(" i -D standard-version\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),$=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,"npx commitizen init cz-conventional-changelog --save-dev --save-exact\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),q=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("type")]),s("span",{class:"token punctuation"},">")]),a("("),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("scope")]),s("span",{class:"token punctuation"},">")]),a("): "),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("subject")]),s("span",{class:"token punctuation"},">")]),a("\n(emptyLine)\n\n"),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("body")]),s("span",{class:"token punctuation"},">")]),a("\n (emptyLine)\n"),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("footer")]),s("span",{class:"token punctuation"},">")]),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),z=s("h4",{id:"message-subject"},[s("a",{class:"header-anchor",href:"#message-subject","aria-hidden":"true"},"#"),a(" Message Subject")],-1),W=s("p",null,"no more than 50 characters",-1),Y=s("h4",{id:"commit-type"},[s("a",{class:"header-anchor",href:"#commit-type","aria-hidden":"true"},"#"),a(" Commit Type")],-1),V=s("ul",null,[s("li",null,"feat: 新增了一个功能 (MINOR Version)."),s("li",null,"fix: 修复了一个 bug (PATCH Version)."),s("li",null,"docs: 只是更改文档."),s("li",null,"style: 不影响代码含义的变化 (空白、格式化、缺少分号等)."),s("li",null,"refactor: 代码重构, 既不修复错误也不添加功能."),s("li",null,"perf: 改进性能的代码更改."),s("li",null,"test: 添加确实测试或更正现有的测试."),s("li",null,"build: 影响构建系统或外部依赖关系的更改 (示例范围: gulp, broccoli, NPM)."),s("li",null,"ci: 更改持续集成文件和脚本 (示例范围: Travis, Circle, BrowserStack, SauceLabs)."),s("li",null,[a("chore: 其他不修改 src 或 test 文件 e.g "),s("code",null,"chore(release)"),a(".")]),s("li",null,"revert: commit 回退.")],-1),K=s("h4",{id:"scope-values"},[s("a",{class:"header-anchor",href:"#scope-values","aria-hidden":"true"},"#"),a(" Scope Values")],-1),J=s("ul",null,[s("li",null,"init"),s("li",null,"runner"),s("li",null,"watcher"),s("li",null,"config"),s("li",null,"web-server"),s("li",null,"proxy"),s("li",null,"empty")],-1),X=s("h4",{id:"message-body"},[s("a",{class:"header-anchor",href:"#message-body","aria-hidden":"true"},"#"),a(" Message Body")],-1),Q=s("ul",null,[s("li",null,"uses the imperative, present tense: “change” not “changed” nor “changes”"),s("li",null,[a("includes "),s("strong",null,"motivation"),a(" for the change and contrasts with previous behavior")])],-1),Z=s("h4",{id:"message-footer"},[s("a",{class:"header-anchor",href:"#message-footer","aria-hidden":"true"},"#"),a(" Message Footer")],-1),nn=s("ul",null,[s("li",null,"referencing issues e.g. close #666, #888"),s("li",null,[a("BREAKING CHANGE ("),s("code",null,"!"),a(") (MAJOR Version) e.g."),s("code",null,"port-runner"),a(" command line option has changed to "),s("code",null,"runner-port"),a(", so that it is consistent with the configuration file syntax. To migrate your project, change all the commands, where you use "),s("code",null,"--port-runner"),a(" to "),s("code",null,"--runner-port"),a(".")])],-1),en=s("h2",{id:"stash"},[s("a",{class:"header-anchor",href:"#stash","aria-hidden":"true"},"#"),a(" Stash")],-1),sn=s("ul",null,[s("li",null,"git stash: 备份当前的工作区的内容,将当前的工作区内容保存到 Git 栈"),s("li",null,"git stash apply/pop: 从 Git 栈中读取最近一次保存的内容,恢复工作区的相关内容"),s("li",null,[a("git stash branch "),s("code",null,""),a(": 新建分支,并在该分支上恢复储藏内容")]),s("li",null,"git stash list: 显示 Git 栈内的所有备份"),s("li",null,"git stash clear: 清空 Git 栈")],-1),an=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# git stash popup"),a("\n"),s("span",{class:"token function"},"git"),a(" stash show -p stash@"),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},"}"),a(),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"git"),a(" apply -R\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),ln=s("p",null,"Pop a single file:",-1),tn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" restore -s stash@"),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},"}"),a(" -- "),s("span",{class:"token operator"},"<"),a("filename"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" checkout stash@"),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},"}"),a(" -- "),s("span",{class:"token operator"},"<"),a("filename"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),rn=s("h2",{id:"revert"},[s("a",{class:"header-anchor",href:"#revert","aria-hidden":"true"},"#"),a(" Revert")],-1),on=s("ul",null,[s("li",null,"重新提交前 n 次的 commit")],-1),cn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" revert -n\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),un=s("h2",{id:"remove"},[s("a",{class:"header-anchor",href:"#remove","aria-hidden":"true"},"#"),a(" Remove")],-1),pn=s("p",null,"完全删除文件",-1),bn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"rm"),a(" filename\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),dn=s("p",null,"--cached: 保留磁盘文件(仅从 git 库移除文件)",-1),mn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"rm"),a(" --cached filename\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),hn=s("h2",{id:"move"},[s("a",{class:"header-anchor",href:"#move","aria-hidden":"true"},"#"),a(" Move")],-1),gn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"mv"),a(" old_path new_path\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),kn=s("h2",{id:"log"},[s("a",{class:"header-anchor",href:"#log","aria-hidden":"true"},"#"),a(" Log")],-1),fn=s("ul",null,[s("li",null,"-p: 打印 diff 差异信息"),s("li",null,"-n: n 为十进制数字,显示最近 n 次信息"),s("li",null,"--stat: 打印简略统计信息"),s("li",null,"--graph: 显示分支合并历史"),s("li",null,"--pretty=: 设置日志格式"),s("li",null,"--author=: 指定作者"),s("li",null,"--committer=: 指定提交者"),s("li",null,"--after=/--since=: 限制日志时间"),s("li",null,'--before=/--until=: 限制日志时间 "2008-01-15" "2 years 1 day 3 minutes ago"'),s("li",null,"--decorate: 查看各个分支当前所指的对象(commit object)"),s("li",null,"--help")],-1),vn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" log -p --stat --graph --pretty"),s("span",{class:"token operator"},"="),a("format:"),s("span",{class:"token string"},'"%h - %an, %ar : %s"'),a(" --since"),s("span",{class:"token operator"},"="),s("span",{class:"token number"},"2"),a(".weeks path_name\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),wn=s("h3",{id:"pretty-format"},[s("a",{class:"header-anchor",href:"#pretty-format","aria-hidden":"true"},"#"),a(" Pretty Format")],-1),yn=s("table",null,[s("thead",null,[s("tr",null,[s("th",{style:{"text-align":"left"}},"选项"),s("th",{style:{"text-align":"left"}},"说明")])]),s("tbody",null,[s("tr",null,[s("td",{style:{"text-align":"left"}},"%H"),s("td",{style:{"text-align":"left"}},"提交对象(commit)的完整哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%h"),s("td",{style:{"text-align":"left"}},"提交对象的简短哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%T"),s("td",{style:{"text-align":"left"}},"树对象(tree)的完整哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%t"),s("td",{style:{"text-align":"left"}},"树对象的简短哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%P"),s("td",{style:{"text-align":"left"}},"父对象(parent)的完整哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%p"),s("td",{style:{"text-align":"left"}},"父对象的简短哈希字串")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%an"),s("td",{style:{"text-align":"left"}},"作者(author)的名字")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%ae"),s("td",{style:{"text-align":"left"}},"作者的电子邮件地址")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%ad"),s("td",{style:{"text-align":"left"}},"作者修订日期 (可以用|-date=|选项定制格式)")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%at"),s("td",{style:{"text-align":"left"}},"作者修订日期 (ms)")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%ar"),s("td",{style:{"text-align":"left"}},"作者修订日期,按多久以前的方式显示")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%cn"),s("td",{style:{"text-align":"left"}},"提交者(committer)的名字")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%ce"),s("td",{style:{"text-align":"left"}},"提交者的电子邮件地址")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%cd"),s("td",{style:{"text-align":"left"}},"提交日期")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%cr"),s("td",{style:{"text-align":"left"}},"提交日期,按多久以前的方式显示")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"%s"),s("td",{style:{"text-align":"left"}},"提交说明")])])],-1),xn=s("h3",{id:"log-options"},[s("a",{class:"header-anchor",href:"#log-options","aria-hidden":"true"},"#"),a(" Log Options")],-1),Cn=s("table",null,[s("thead",null,[s("tr",null,[s("th",{style:{"text-align":"left"}},"选项"),s("th",{style:{"text-align":"left"}},"说明")])]),s("tbody",null,[s("tr",null,[s("td",{style:{"text-align":"left"}},"-p"),s("td",{style:{"text-align":"left"}},"打印 diff 差异信息")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"-n"),s("td",{style:{"text-align":"left"}},"n 为十进制数字,显示最近 n 次信息")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--stat"),s("td",{style:{"text-align":"left"}},"打印简略统计信息")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--graph"),s("td",{style:{"text-align":"left"}},"显示分支合并历史")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--pretty="),s("td",{style:{"text-align":"left"}},"设置日志格式")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--author="),s("td",{style:{"text-align":"left"}},"指定作者")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--committer="),s("td",{style:{"text-align":"left"}},"指定提交者")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--after=/--since="),s("td",{style:{"text-align":"left"}},"限制日志时间")]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--before=/--until="),s("td",{style:{"text-align":"left"}},'限制日志时间 "2008-01-15" "2 years 1 day 3 minutes ago"')]),s("tr",null,[s("td",{style:{"text-align":"left"}},"--help"),s("td",{style:{"text-align":"left"}})])])],-1),An=s("h2",{id:"reflog"},[s("a",{class:"header-anchor",href:"#reflog","aria-hidden":"true"},"#"),a(" Reflog")],-1),Gn=s("p",null,[s("code",null,"git reflog show"),a(" is an alias for "),s("code",null,"git log -g --abbrev-commit --pretty=oneline"),a(".")],-1),In=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" reflog\n"),s("span",{class:"token function"},"git"),a(" reset HEAD@"),s("span",{class:"token punctuation"},"{"),a("index"),s("span",{class:"token punctuation"},"}"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),En=s("h2",{id:"show"},[s("a",{class:"header-anchor",href:"#show","aria-hidden":"true"},"#"),a(" Show")],-1),Tn=s("ul",null,[s("li",null,"查看其他分支 或 提交点的文件状态")],-1),Nn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" show branchName/commitHash:fileName\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),On=s("h2",{id:"remote"},[s("a",{class:"header-anchor",href:"#remote","aria-hidden":"true"},"#"),a(" Remote")],-1),Rn=s("p",null,"添加与删除远程仓库源",-1),Sn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" remote "),s("span",{class:"token function"},"add"),a(),s("span",{class:"token operator"},"<"),a("shortname"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("remote-url"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" remote "),s("span",{class:"token function"},"rm"),a(),s("span",{class:"token operator"},"<"),a("shortname"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Hn=s("p",null,"拉取和推送变更",-1),Ln=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" pull "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a("\n"),s("span",{class:"token function"},"git"),a(" push "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token punctuation"},"["),a("local-branch-name"),s("span",{class:"token punctuation"},"]"),a(":"),s("span",{class:"token punctuation"},"["),a("remote-branch-name"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),jn=s("p",null,"显示仓库信息",-1),Dn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" remote show "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Pn=s("p",null,"重命名仓库缩写名",-1),Bn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" remote "),s("span",{class:"token function"},"rename"),a(),s("span",{class:"token operator"},"<"),a("old"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("new"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),_n=s("p",null,"从本地操作,删除远程仓库的分支",-1),Mn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" push origin --delete "),s("span",{class:"token punctuation"},"["),a("remote-branch-name"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Fn=s("p",null,"保存推送密码",-1),Un=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --global credential.helper store\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),$n=s("h2",{id:"tag"},[s("a",{class:"header-anchor",href:"#tag","aria-hidden":"true"},"#"),a(" Tag")],-1),qn=s("p",null,"列出标记及其信息",-1),zn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" tag\n"),s("span",{class:"token function"},"git"),a(" tag -l "),s("span",{class:"token string"},'"v1.8-"'),a("\n"),s("span",{class:"token function"},"git"),a(" show "),s("span",{class:"token operator"},"<"),a("tagName"),s("span",{class:"token punctuation"},"("),a("v1.4"),s("span",{class:"token punctuation"},")"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),Wn=s("p",null,"创建标签:",-1),Yn=s("ul",null,[s("li",null,"不加-m 会调用 core.editor)"),s("li",null,"省略 commit 序列,标签添加至最新提交")],-1),Vn=s("p",null,"创建附注(annotated)标签",-1),Kn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" tag -a "),s("span",{class:"token operator"},"<"),a("tagName"),s("span",{class:"token punctuation"},"("),a("v1.4"),s("span",{class:"token punctuation"},")"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token punctuation"},"["),a("commit序列"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Jn=s("p",null,"创建轻量(lightweight)标签",-1),Xn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" tag "),s("span",{class:"token operator"},"<"),a("tagName"),s("span",{class:"token punctuation"},"("),a("v1.4"),s("span",{class:"token punctuation"},")"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token punctuation"},"["),a("commit序列"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Qn=s("p",null,"共享标签至远程库",-1),Zn=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" push "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token operator"},"<"),a("tagName"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" push "),s("span",{class:"token punctuation"},"["),a("remote-name"),s("span",{class:"token punctuation"},"]"),a(" --tags\n"),s("span",{class:"token function"},"git"),a(" push --follow-tags\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),ne=s("h2",{id:"alias"},[s("a",{class:"header-anchor",href:"#alias","aria-hidden":"true"},"#"),a(" Alias")],-1),ee=s("ul",null,[s("li",null,"!: 执行外部命令")],-1),se=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" config --global alias.co checkout\n"),s("span",{class:"token function"},"git"),a(" config --global alias.br branch\n"),s("span",{class:"token function"},"git"),a(" config --global alias.ci commit\n"),s("span",{class:"token function"},"git"),a(" config --global alias.st status\n\n"),s("span",{class:"token function"},"git"),a(" config --global alias.unstage "),s("span",{class:"token string"},"'reset HEAD --'"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global alias.last "),s("span",{class:"token string"},"'log -1 HEAD'"),a("\n\n"),s("span",{class:"token function"},"git"),a(" config --global alias.visual "),s("span",{class:"token string"},"'!gitk'"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br")])],-1),ae=s("h2",{id:"merge"},[s("a",{class:"header-anchor",href:"#merge","aria-hidden":"true"},"#"),a(" Merge")],-1),le=s("p",null,"合并的结果是生成一个新的快照(并提交)(新的提交对象)",-1),te=s("h2",{id:"rebase"},[s("a",{class:"header-anchor",href:"#rebase","aria-hidden":"true"},"#"),a(" Rebase")],-1),ie=s("p",null,"切换到工作分支,编码开发新特性",-1),re=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout feature-branch\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),oe=s("p",null,"新特性开发完毕,变基操作以简洁提交历史",-1),ce=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" rebase master\n\n"),s("span",{class:"token function"},"git"),a(" rebase "),s("span",{class:"token punctuation"},"["),a("baseBranch"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token punctuation"},"["),a("topicBranch"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),ue=s("p",null,"切换到主分支,合并特性分支",-1),pe=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout master\n"),s("span",{class:"token function"},"git"),a(" merge feature-branch\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),be=s("p",null,"pull with auto rebase and auto stash",-1),de=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" pull --rebase --autostash\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),me=s("h2",{id:"branch"},[s("a",{class:"header-anchor",href:"#branch","aria-hidden":"true"},"#"),a(" Branch")],-1),he=s("h3",{id:"basic-workflow-commands"},[s("a",{class:"header-anchor",href:"#basic-workflow-commands","aria-hidden":"true"},"#"),a(" Basic Workflow Commands")],-1),ge=s("h4",{id:"basic-branch"},[s("a",{class:"header-anchor",href:"#basic-branch","aria-hidden":"true"},"#"),a(" Basic Branch")],-1),ke=s("p",null,"创建新分支",-1),fe=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch "),s("span",{class:"token operator"},"<"),a("new-branch-name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),ve=s("p",null,"删除分支",-1),we=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch -d "),s("span",{class:"token operator"},"<"),a("branch-name"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" push origin --delete "),s("span",{class:"token operator"},"<"),a("remote-branch-name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),ye=s("p",null,"切换分支",-1),xe=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout "),s("span",{class:"token operator"},"<"),a("branch-name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ce=s("p",null,"切换到新分支",-1),Ae=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout -b "),s("span",{class:"token operator"},"<"),a("new-branch-name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ge=s("p",null,"打印分支信息",-1),Ie=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch -v"),s("span",{class:"token punctuation"},"("),a("详细信息"),s("span",{class:"token punctuation"},")"),a(" -vv"),s("span",{class:"token punctuation"},"("),a("详细远程信息"),s("span",{class:"token punctuation"},")"),a(" --merged"),s("span",{class:"token punctuation"},"("),a("显示合并至当前分支的分支"),s("span",{class:"token punctuation"},")"),a(" --no-merged"),s("span",{class:"token punctuation"},"("),a("显示未合并至当前分支的分支"),s("span",{class:"token punctuation"},")"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ee=s("h4",{id:"remote-branch"},[s("a",{class:"header-anchor",href:"#remote-branch","aria-hidden":"true"},"#"),a(" Remote Branch")],-1),Te=s("ul",null,[s("li",null,"Show all remote branch:")],-1),Ne=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch -r\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Oe=s("p",null,"本地分支跟踪远程分支(在此本地分支上运行 git pull 自动抓取), 2 种方式:",-1),Re=s("ul",null,[s("li",null,"设置当前所在本地分支跟踪某一远程分支")],-1),Se=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" branch -u "),s("span",{class:"token punctuation"},"["),a("remoteName"),s("span",{class:"token punctuation"},"]"),a("/"),s("span",{class:"token punctuation"},"["),a("branch"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),He=s("ul",null,[s("li",null,[a("创建并切换至新的本地分支(跟踪某一远程分支) "),s("ul",null,[s("li",null,"--track: 本地分支由 git 自动命名"),s("li",null,"-b: 本地分支由创建者命名")])])],-1),Le=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout --track "),s("span",{class:"token punctuation"},"["),a("new-local-branch"),s("span",{class:"token punctuation"},"]"),a("\n\n"),s("span",{class:"token function"},"git"),a(" checkout -b "),s("span",{class:"token punctuation"},"["),a("new-local-branch"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token punctuation"},"["),a("remoteName"),s("span",{class:"token punctuation"},"]"),a("/"),s("span",{class:"token punctuation"},"["),a("branch"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),je=s("ul",null,[s("li",null,"Delete remote branch")],-1),De=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" push --delete origin "),s("span",{class:"token punctuation"},"["),a("remote-branch-name"),s("span",{class:"token punctuation"},"]"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Pe=s("h3",{id:"advanced-branch-workflow"},[s("a",{class:"header-anchor",href:"#advanced-branch-workflow","aria-hidden":"true"},"#"),a(" Advanced Branch Workflow")],-1),Be=s("p",null,[s("a",{href:"https://github.com/nvie/gitflow",target:"_blank",rel:"noopener noreferrer"},"Git Flow Extension"),a(":")],-1),_e=s("ol",null,[s("li",null,"master 类型分支,名为?|master 或 master,其中?为开发代号"),s("li",null,"develop 类型分支,名为?|develop 或 develop,其中?为开发代号"),s("li",null,[a("feature 类型分支,名为 feature/"),s("em",null,"或?|feature/"),a(",其中*为特征描述")]),s("li",null,[a("release 类型分支,名为 release-"),s("em",null,"或?|release-"),a(",其中*为要发布的版本号")]),s("li",null,[a("hotfix 类型分支,名为 hotfix-"),s("em",null,"或?|hotfix-"),a(",其中*为要发布的版本号")]),s("li",null,[a("issues 类型分支,名为 issues/"),s("em",null,"或?|issues/"),a(",其中*为问题描述")]),s("li",null,"trials 类型分支,名为?%trials.*,?为此分支的父分支,*为描述的名称(或直接为?%trials)"),s("li",null,"basedOn 类型分支,名为 basedOn 或?|basedOn,?为其来源的 master 分支的开发代号"),s("li",null,[a("work 类型分支,名为 work.**"),s("em",null,"/basedOn-?-"),a(",***代表此描述此 work 的名称, ?为其所基于的分支的开发代号,最后一个*代表其在?|basedOn 上所基于的分支的版本号或状态名")])],-1),Me=s("p",null,"下面介绍模型中的约定,并定义 gg-*这样的抽象动作来完成约定中的行为",-1),Fe=s("h4",{id:"master-branch-and-develop-branch"},[s("a",{class:"header-anchor",href:"#master-branch-and-develop-branch","aria-hidden":"true"},"#"),a(" Master Branch and Develop Branch")],-1),Ue=s("p",null,[s("em",null,"多长期分支模式"),a(": master 分支与 develop 分支都是长期分支,区别在于分支的"),s("strong",null,"稳定性等级"),a(" - master > develop")],-1),$e=s("p",null,"e.g master/develop/next",-1),qe=s("ul",null,[s("li",null,"每一次的提交都必须有意义")],-1),ze=s("p",null,"git 在每次提交的时候要求输入对此提交的概括,这个概括不能为空。",-1),We=s("p",null,"正确的提交概括:更新了程序 doc 错误的提交概括:updates",-1),Ye=s("ul",null,[s("li",null,"开发型任务中的 master 类型与 develop 类型分支必须成对出现, master 分支的推进只能来源与 release 分支和 hotfix 分支的合并,禁止在 master 分支上直接提交")],-1),Ve=s("blockquote",null,[s("p",null,"master 分支上只有我们推送上去的稳定版本的程序,develop 分支上的程序一直处于开发状态,不稳定。 在开发型任务中使用 gg-init 进行版本控制的初始化,建立配套的 master ~ develop 分支对。 在使用型任务中使用 gg-work-init 进行版本控制的初始化, 拉取需要使用的稳定版本程序的 master 分支,并初始化对应的 basedOn 分支(见 9).")],-1),Ke=s("h4",{id:"feature-branch"},[s("a",{class:"header-anchor",href:"#feature-branch","aria-hidden":"true"},"#"),a(" Feature Branch")],-1),Je=s("ol",null,[s("li",null,"只能从 develop 类型分支上创建"),s("li",null,"最终必须合并到 develop 类型分支"),s("li",null,"最终分支被删除")],-1),Xe=s("blockquote",null,[s("p",null,"每当有新特性需要加入的时候,我们应该从 develop 类型分支上新建一个 feature 类型分支,完成新特性的开发和测试后将特性合并到 develop 类型分支上。 在 develop 类型分支上使用 gg-feature-open featureName 建立并转向一个名为 feature/featureName 的新分支 在一个 feature 类型分支上使用 gg-feature-close 把这个分支的工作合并到 develop 类型分支上,删除此分支,完成一个特性的开发")],-1),Qe=s("h4",{id:"release-branch"},[s("a",{class:"header-anchor",href:"#release-branch","aria-hidden":"true"},"#"),a(" Release Branch")],-1),Ze=s("ol",null,[s("li",null,"只能从 develop 类型分支上创建"),s("li",null,"最终必须同时合并到 master 类型分支(发布新的版本)和 develop 类型分支(基于新版本的进一步开发)"),s("li",null,"最终分支被删除")],-1),ns=s("blockquote",null,[s("p",null,"每当工作进入到一个较为稳定阶段的时候,可以使用 gg-release-open versionNum 建立一个名为 release-versionNum 的临时分支, 在这个分支上允许进行小的改动(比如修改一下 readme 文件中的版本号), 然后使用 gg-release-close 将此版本合并(发布)到 master 类型分支上,同时合并到 develop 类型分支上,然后删除此分支.")],-1),es=s("h4",{id:"hotfix-branch"},[s("a",{class:"header-anchor",href:"#hotfix-branch","aria-hidden":"true"},"#"),a(" Hotfix Branch")],-1),ss=s("ol",null,[s("li",null,"只能从 master 类型分支上创建"),s("li",null,"最终必须同时合并到 master 类型分支(发布新的热补丁版本)和 develop 类型分支(基于新版本的进一步开发)"),s("li",null,"最终分支被删除")],-1),as=s("blockquote",null,[s("p",null,"当新版本发布后发现必须马上解决的严重 bug 时,使用 gg-hotfix-open versionNum 建立名为 hotfix-versionNum 的临时分支, 在这个分支上完成 bug 的修复,然后使用 gg-hotfix-close 将此版本合并(发布)到 master 类型分支上,同时合并到 develop 类型分支上,然后删除此分支.")],-1),ls=s("h4",{id:"issues-branch"},[s("a",{class:"header-anchor",href:"#issues-branch","aria-hidden":"true"},"#"),a(" Issues Branch")],-1),ts=s("ol",null,[s("li",null,"只能从 develop 类型分支上创建"),s("li",null,"最终必须合并到 develop 类型分支"),s("li",null,"最终分支被删除")],-1),is=s("blockquote",null,[s("p",null,"注解:每当有(比较复杂的)问题需要解决的时候,应该从 develop 类型分支上新建一个 issues 类型分支,完成问题的调试后合并到 develop 类型分支上。 在 develop 类型分支上使用 gg-issues-open featureName 建立并转向一个名为 issues/issuesName 的新分支 在一个 issues 类型分支上使用 gg-issues-close 把这个分支的工作合并到 develop 类型分支上,然后删除此分支,解决了一个复杂的问题 issues 类型和 feature 类型的实现方式一模一样,仅仅有名字上面的差别。")],-1),rs=s("h4",{id:"trials-branch"},[s("a",{class:"header-anchor",href:"#trials-branch","aria-hidden":"true"},"#"),a(" Trials Branch")],-1),os=s("ul",null,[s("li",null,"可以从除了 release 类型分支以外的任何类型分支上创建"),s("li",null,[a("在这个分支上请发挥想象力大胆实验 "),s("ul",null,[s("li",null,"接受实验结果,把实验过程并入父分支,称为 good-close"),s("li",null,"实验结果不理想,放弃实验结果,从实验开始前重新来过,称为 bad-close")])]),s("li",null,"最终分支被删除")],-1),cs=s("blockquote",null,[s("p",null,"在满足条件的分支 A 上工作,时不时会冒出一些大胆的想法 这个时候使用 gg-trials-open trialsName 创建并转向一个名为 A/trials.trialsName 的实验分支,在这个分支上进行疯狂的实验")],-1),us=s("h4",{id:"basedon-branch"},[s("a",{class:"header-anchor",href:"#basedon-branch","aria-hidden":"true"},"#"),a(" BasedOn Branch")],-1),ps=s("ol",null,[s("li",null,"从 name|master 建立并初始化为 name|basedOn"),s("li",null,"只能从对应的 master 分支 fork 到此分支"),s("li",null,"禁止在这个分支上提交")],-1),bs=s("blockquote",null,[s("p",null,"这个分支是一个为了使工作流程更为清晰的缓存分支, 分支上只有从 master 稳定分支上挑选出来的自己在工作中将要(尝试)使用的稳定版本。 在 basedOn 类型分支上使用 gg-select 版本号 从对应的 master 分支上选出一个稳定版本或使用 gg-select-the-latest 从对应的 master 分支上选择最新的版本, fork 到这个分支,并加上 inUse-versionNum 的标签 从 master 到此分支的行为是 fork, 即有可能此分支的 log 为 (init)v1.0===>v0.9=====>v0.8======>v1.3, 这个分支上的 commit 来源于 master,但是其分支提交历史与 master 分支无关")],-1),ds=s("h4",{id:"work-branch"},[s("a",{class:"header-anchor",href:"#work-branch","aria-hidden":"true"},"#"),a(" Work Branch")],-1),ms=s("ol",null,[s("li",null,"只能从 basedOn 类型分支上创建"),s("li",null,"可以借助 basedOn 分支升级")],-1),hs=s("h2",{id:"git-inside"},[s("a",{class:"header-anchor",href:"#git-inside","aria-hidden":"true"},"#"),a(" Git Inside")],-1),gs=s("h3",{id:"add-inside"},[s("a",{class:"header-anchor",href:"#add-inside","aria-hidden":"true"},"#"),a(" Add Inside")],-1),ks=s("ul",null,[s("li",null,"create blob objects: contains content of files"),s("li",null,"add files to index list (.git/index)")],-1),fs=s("h3",{id:"commit-inside"},[s("a",{class:"header-anchor",href:"#commit-inside","aria-hidden":"true"},"#"),a(" Commit Inside")],-1),vs=s("ul",null,[s("li",null,"create tree objects: each object represent a directory, contains blob object refs in this directory"),s("li",null,"create commit object: contains root tree object hash number and parent commit object hash number")],-1),ws=s("h3",{id:"checkout-inside"},[s("a",{class:"header-anchor",href:"#checkout-inside","aria-hidden":"true"},"#"),a(" Checkout Inside")],-1),ys=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" checkout "),s("span",{class:"token operator"},"<"),a("commit-hash-id"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),xs=s("ul",null,[s("li",null,"get commit object by commit hash id"),s("li",null,"get root tree object in commit object"),s("li",null,"write file entries by root tree object (tree graph)"),s("li",null,"write .git/index"),s("li",null,"set HEAD to that commit (detached HEAD state)")],-1),Cs=s("div",{class:"language-js line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"// Get file commit history"),a("\n"),s("span",{class:"token keyword"},"const"),a(" Git "),s("span",{class:"token operator"},"="),a(),s("span",{class:"token function"},"require"),s("span",{class:"token punctuation"},"("),s("span",{class:"token string"},"'nodegit'"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n"),s("span",{class:"token keyword"},"let"),a(" repo"),s("span",{class:"token punctuation"},";"),a("\n\nGit"),s("span",{class:"token punctuation"},"."),a("Repository"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"open"),s("span",{class:"token punctuation"},"("),a("path"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"resolve"),s("span",{class:"token punctuation"},"("),s("span",{class:"token string"},"'./.git'"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"then"),s("span",{class:"token punctuation"},"("),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token punctuation"},"("),s("span",{class:"token parameter"},"r"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n repo "),s("span",{class:"token operator"},"="),a(" r"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token keyword"},"return"),a(" repo"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"getMasterCommit"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"then"),s("span",{class:"token punctuation"},"("),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token punctuation"},"("),s("span",{class:"token parameter"},"firstCommitOnMaster"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"const"),a(" walker "),s("span",{class:"token operator"},"="),a(" repo"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"createRevWalk"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n walker"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"push"),s("span",{class:"token punctuation"},"("),a("firstCommitOnMaster"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"sha"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n walker"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"sorting"),s("span",{class:"token punctuation"},"("),a("Git"),s("span",{class:"token punctuation"},"."),a("Revwalk"),s("span",{class:"token punctuation"},"."),s("span",{class:"token constant"},"SORT"),s("span",{class:"token punctuation"},"."),a("Time"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n\n "),s("span",{class:"token keyword"},"return"),a(" walker"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"fileHistoryWalk"),s("span",{class:"token punctuation"},"("),a("historyFile"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token number"},"2"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"then"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},"("),s("span",{class:"token parameter"},"resultingArrayOfCommits"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token operator"},"=>"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"if"),a(),s("span",{class:"token punctuation"},"("),a("resultingArrayOfCommits"),s("span",{class:"token punctuation"},"."),a("length "),s("span",{class:"token operator"},">"),a(),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"const"),a(" commit "),s("span",{class:"token operator"},"="),a(" resultingArrayOfCommits"),s("span",{class:"token punctuation"},"["),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},"."),a("commit"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token keyword"},"const"),a(" date "),s("span",{class:"token operator"},"="),a(" commit"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"date"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token punctuation"},"}"),a("\n "),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},";"),a("\n\n"),s("span",{class:"token keyword"},"const"),a(),s("span",{class:"token function-variable function"},"getGitLastUpdatedTimeStamp"),a(),s("span",{class:"token operator"},"="),a(),s("span",{class:"token punctuation"},"("),s("span",{class:"token parameter"},"filePath"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token operator"},"=>"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"let"),a(" lastUpdated "),s("span",{class:"token operator"},"="),a(),s("span",{class:"token number"},"0"),s("span",{class:"token punctuation"},";"),a("\n\n "),s("span",{class:"token keyword"},"try"),a(),s("span",{class:"token punctuation"},"{"),a("\n lastUpdated "),s("span",{class:"token operator"},"="),a("\n "),s("span",{class:"token function"},"parseInt"),s("span",{class:"token punctuation"},"("),a("\n spawn\n "),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"sync"),s("span",{class:"token punctuation"},"("),s("span",{class:"token string"},"'git'"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token punctuation"},"["),s("span",{class:"token string"},"'log'"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token string"},"'-1'"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token string"},"'--format=%at'"),s("span",{class:"token punctuation"},","),a(" path"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"basename"),s("span",{class:"token punctuation"},"("),a("filePath"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},","),a(),s("span",{class:"token punctuation"},"{"),a("\n cwd"),s("span",{class:"token operator"},":"),a(" path"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"dirname"),s("span",{class:"token punctuation"},"("),a("filePath"),s("span",{class:"token punctuation"},")"),s("span",{class:"token punctuation"},","),a("\n "),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},"."),a("stdout"),s("span",{class:"token punctuation"},"."),s("span",{class:"token function"},"toString"),s("span",{class:"token punctuation"},"("),s("span",{class:"token string"},"'utf-8'"),s("span",{class:"token punctuation"},")"),a("\n "),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token operator"},"*"),a(),s("span",{class:"token number"},"1000"),s("span",{class:"token punctuation"},";"),a("\n "),s("span",{class:"token punctuation"},"}"),a(),s("span",{class:"token keyword"},"catch"),a(),s("span",{class:"token punctuation"},"("),a("e"),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token comment"},"/* do not handle for now */"),a("\n "),s("span",{class:"token punctuation"},"}"),a("\n\n "),s("span",{class:"token keyword"},"return"),a(" lastUpdated"),s("span",{class:"token punctuation"},";"),a("\n"),s("span",{class:"token punctuation"},"}"),s("span",{class:"token punctuation"},";"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br"),s("span",{class:"line-number"},"19"),s("br"),s("span",{class:"line-number"},"20"),s("br"),s("span",{class:"line-number"},"21"),s("br"),s("span",{class:"line-number"},"22"),s("br"),s("span",{class:"line-number"},"23"),s("br"),s("span",{class:"line-number"},"24"),s("br"),s("span",{class:"line-number"},"25"),s("br"),s("span",{class:"line-number"},"26"),s("br"),s("span",{class:"line-number"},"27"),s("br"),s("span",{class:"line-number"},"28"),s("br"),s("span",{class:"line-number"},"29"),s("br"),s("span",{class:"line-number"},"30"),s("br"),s("span",{class:"line-number"},"31"),s("br"),s("span",{class:"line-number"},"32"),s("br"),s("span",{class:"line-number"},"33"),s("br"),s("span",{class:"line-number"},"34"),s("br"),s("span",{class:"line-number"},"35"),s("br"),s("span",{class:"line-number"},"36"),s("br"),s("span",{class:"line-number"},"37"),s("br"),s("span",{class:"line-number"},"38"),s("br"),s("span",{class:"line-number"},"39"),s("br"),s("span",{class:"line-number"},"40"),s("br"),s("span",{class:"line-number"},"41"),s("br")])],-1),As=s("h3",{id:"merge-inside"},[s("a",{class:"header-anchor",href:"#merge-inside","aria-hidden":"true"},"#"),a(" Merge Inside")],-1),Gs=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" merge "),s("span",{class:"token operator"},"<"),a("giver-branch"),s("span",{class:"token operator"},">"),a("/"),s("span",{class:"token operator"},"<"),a("giver-commit"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Is=s("ul",null,[s("li",null,[a("write giver commit hash to "),s("code",null,".git/MERGE_HEAD")]),s("li",null,"find base commit (the most recent common ancestor commit)"),s("li",null,"diff and apply according to base commit, giver commit, receiver commit"),s("li",null,[a("do what "),s("code",null,"git checkout"),a(" do")]),s("li",null,[a("remove "),s("code",null,".git/MERGE_HEAD")])],-1),Es=s("h3",{id:"fetch-inside"},[s("a",{class:"header-anchor",href:"#fetch-inside","aria-hidden":"true"},"#"),a(" Fetch Inside")],-1),Ts=s("ul",null,[s("li",null,"get hash of remote commit and its root tree object"),s("li",null,"copy all diff objects in tree graph into .git/objects"),s("li",null,[a("update "),s("code",null,".git/refs/remotes/origin/"),a(", set "),s("code",null,".git/FETCH_HEAD"),a(" to it")])],-1),Ns=s("h3",{id:"clone-inside"},[s("a",{class:"header-anchor",href:"#clone-inside","aria-hidden":"true"},"#"),a(" Clone Inside")],-1),Os=s("p",null,[s("code",null,"git init"),a(" + "),s("code",null,"git remote add origin "),a(" + "),s("code",null,"git pull origin")],-1),Rs=s("h3",{id:"push-inside"},[s("a",{class:"header-anchor",href:"#push-inside","aria-hidden":"true"},"#"),a(" Push Inside")],-1),Ss=s("ul",null,[s("li",null,"apply commit to remote repo"),s("li",null,[a("update remote repo "),s("code",null,".git/refs/heads/"),a(" to new commit")]),s("li",null,[a("update local repo "),s("code",null,".git/refs/remotes/origin/"),a(" to new commit")])],-1),Hs=s("h3",{id:"head-branch-inside"},[s("a",{class:"header-anchor",href:"#head-branch-inside","aria-hidden":"true"},"#"),a(" HEAD Branch Inside")],-1),Ls=s("ul",null,[s("li",null,"HEAD -> refs/heads/master -> commit object"),s("li",null,"branches are just refs, refs are just files (contain commit hash id)")],-1),js=s("h3",{id:"git-objects"},[s("a",{class:"header-anchor",href:"#git-objects","aria-hidden":"true"},"#"),a(" Git Objects")],-1),Ds=s("p",null,[s("code",null,".git/objects"),a(" is immutable, "),s("code",null,".git/refs"),a(" is mutable")],-1),Ps=s("p",null,[s("code",null,"blob"),a("持有文件的内容,"),s("code",null,"树对象"),a("是一个包含"),s("code",null,"blob"),a("对象和"),s("code",null,"子树对象"),a("的目录列表. "),s("code",null,"提交对象"),a("是工作目录的一个快照, 包含了一些像时间或提交信息这样的元数据. "),s("code",null,"分支"),a("是"),s("code",null,"提交对象"),a("的命名引用. "),s("code",null,"工作目录"),a("是一个目录, 有着相应的仓库, "),s("code",null,"暂存区"),a("(索引)为下一个"),s("code",null,"提交对象"),a("持有对应的"),s("code",null,"树对象"),a(", 而仓库就是一个"),s("code",null,"提交对象"),a("的集合.")],-1),Bs=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" hash-object 创建blob对象\n"),s("span",{class:"token function"},"git"),a(" cat-file -t\n"),s("span",{class:"token function"},"git"),a(" cat-file -p\n"),s("span",{class:"token function"},"git"),a(" update-index --add --cache-info 将文件添加至暂存区\n"),s("span",{class:"token function"},"git"),a(" write-tree 创建tree对象\n"),s("span",{class:"token function"},"git"),a(" commit-tree 创建commit对象\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),_s=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# -w for write into codebase,"),a("\n"),s("span",{class:"token comment"},"# --stdin for reading from stdin not file"),a("\n"),s("span",{class:"token builtin class-name"},"echo"),a(),s("span",{class:"token string"},"'test content'"),a(),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"git"),a(" hash-object -w --stdin\n"),s("span",{class:"token function"},"git"),a(" cat-file -p "),s("span",{class:"token operator"},"<"),a("object-hash-number"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br")])],-1),Ms=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token shebang important"},"#!/bin/bash"),a("\n\n"),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token function-name function"},"separator"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token keyword"},"for"),a(),s("span",{class:"token for-or-select variable"},"i"),a(),s("span",{class:"token keyword"},"in"),a(),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"1"),s("span",{class:"token punctuation"},".."),s("span",{class:"token number"},"20"),s("span",{class:"token punctuation"},"}"),a("\n "),s("span",{class:"token keyword"},"do"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},'"-"'),a("\n "),s("span",{class:"token keyword"},"done"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token variable"},"$1"),a("\n "),s("span",{class:"token keyword"},"for"),a(),s("span",{class:"token for-or-select variable"},"i"),a(),s("span",{class:"token keyword"},"in"),a(),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"1"),s("span",{class:"token punctuation"},".."),s("span",{class:"token number"},"20"),s("span",{class:"token punctuation"},"}"),a("\n "),s("span",{class:"token keyword"},"do"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},'"-"'),a("\n "),s("span",{class:"token keyword"},"done"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},[a('"'),s("span",{class:"token entity",title:"\\n"},"\\n"),a('"')]),a("\n"),s("span",{class:"token punctuation"},"}"),a("\n\n"),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token function-name function"},"git_object_type"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},'"type => "'),a("\n "),s("span",{class:"token function"},"git"),a(" cat-file -t "),s("span",{class:"token variable"},"$1"),a("\n "),s("span",{class:"token keyword"},"for"),a(),s("span",{class:"token for-or-select variable"},"i"),a(),s("span",{class:"token keyword"},"in"),a(),s("span",{class:"token punctuation"},"{"),s("span",{class:"token number"},"1"),s("span",{class:"token punctuation"},".."),s("span",{class:"token number"},"40"),s("span",{class:"token punctuation"},"}"),a("\n "),s("span",{class:"token keyword"},"do"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},'"-"'),a("\n "),s("span",{class:"token keyword"},"done"),a("\n "),s("span",{class:"token builtin class-name"},"printf"),a(),s("span",{class:"token string"},[a('"'),s("span",{class:"token entity",title:"\\n"},"\\n"),a('"')]),a("\n"),s("span",{class:"token punctuation"},"}"),a("\n\n"),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token function-name function"},"git_object_content"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token function"},"git"),a(" cat-file -p "),s("span",{class:"token variable"},"$1"),a("\n"),s("span",{class:"token punctuation"},"}"),a("\n\n"),s("span",{class:"token keyword"},"function"),a(),s("span",{class:"token function-name function"},"print_git_objects"),s("span",{class:"token punctuation"},"("),s("span",{class:"token punctuation"},")"),a(),s("span",{class:"token punctuation"},"{"),a("\n "),s("span",{class:"token assign-left variable"},"files"),s("span",{class:"token operator"},"="),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"git"),a(" rev-list --parents --objects HEAD "),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"awk"),a(),s("span",{class:"token string"},"'{print $1}'"),s("span",{class:"token variable"},")")]),a("\n "),s("span",{class:"token assign-left variable"},"index"),s("span",{class:"token operator"},"="),s("span",{class:"token number"},"0"),a("\n\n "),s("span",{class:"token keyword"},"for"),a(),s("span",{class:"token for-or-select variable"},"file"),a(),s("span",{class:"token keyword"},"in"),a(),s("span",{class:"token variable"},"$files"),a("\n "),s("span",{class:"token keyword"},"do"),a("\n "),s("span",{class:"token assign-left variable"},"len"),s("span",{class:"token operator"},"="),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"expr"),a(" length "),s("span",{class:"token string"},[a('"'),s("span",{class:"token variable"},"$file"),a('"')]),s("span",{class:"token variable"},")")]),a("\n\n "),s("span",{class:"token keyword"},"if"),a(),s("span",{class:"token punctuation"},"["),a(),s("span",{class:"token variable"},"$len"),a(" -gt "),s("span",{class:"token number"},"30"),a(),s("span",{class:"token punctuation"},"]"),a("\n "),s("span",{class:"token keyword"},"then"),a("\n "),s("span",{class:"token assign-left variable"},"index"),s("span",{class:"token operator"},"="),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"expr"),a(" $index + "),s("span",{class:"token number"},"1"),s("span",{class:"token variable"},")")]),a("\n separator "),s("span",{class:"token variable"},"$index"),a("\n "),s("span",{class:"token builtin class-name"},"echo"),a(),s("span",{class:"token variable"},"$file"),a("\n git_object_type "),s("span",{class:"token variable"},"$file"),a("\n git_object_content "),s("span",{class:"token variable"},"$file"),a("\n "),s("span",{class:"token keyword"},"fi"),a("\n "),s("span",{class:"token keyword"},"done"),a("\n"),s("span",{class:"token punctuation"},"}"),a("\n\nprint_git_objects\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br"),s("span",{class:"line-number"},"19"),s("br"),s("span",{class:"line-number"},"20"),s("br"),s("span",{class:"line-number"},"21"),s("br"),s("span",{class:"line-number"},"22"),s("br"),s("span",{class:"line-number"},"23"),s("br"),s("span",{class:"line-number"},"24"),s("br"),s("span",{class:"line-number"},"25"),s("br"),s("span",{class:"line-number"},"26"),s("br"),s("span",{class:"line-number"},"27"),s("br"),s("span",{class:"line-number"},"28"),s("br"),s("span",{class:"line-number"},"29"),s("br"),s("span",{class:"line-number"},"30"),s("br"),s("span",{class:"line-number"},"31"),s("br"),s("span",{class:"line-number"},"32"),s("br"),s("span",{class:"line-number"},"33"),s("br"),s("span",{class:"line-number"},"34"),s("br"),s("span",{class:"line-number"},"35"),s("br"),s("span",{class:"line-number"},"36"),s("br"),s("span",{class:"line-number"},"37"),s("br"),s("span",{class:"line-number"},"38"),s("br"),s("span",{class:"line-number"},"39"),s("br"),s("span",{class:"line-number"},"40"),s("br"),s("span",{class:"line-number"},"41"),s("br"),s("span",{class:"line-number"},"42"),s("br"),s("span",{class:"line-number"},"43"),s("br"),s("span",{class:"line-number"},"44"),s("br"),s("span",{class:"line-number"},"45"),s("br"),s("span",{class:"line-number"},"46"),s("br"),s("span",{class:"line-number"},"47"),s("br"),s("span",{class:"line-number"},"48"),s("br"),s("span",{class:"line-number"},"49"),s("br")])],-1),Fs=s("h2",{id:"github"},[s("a",{class:"header-anchor",href:"#github","aria-hidden":"true"},"#"),a(" GitHub")],-1),Us=s("h3",{id:"gpg-usage"},[s("a",{class:"header-anchor",href:"#gpg-usage","aria-hidden":"true"},"#"),a(" GPG Usage")],-1),$s=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# Generate GPG key"),a("\ngpg --full-generate-key\n"),s("span",{class:"token comment"},"# List GPG keys"),a("\ngpg --list-keys\n\n"),s("span",{class:"token comment"},"# Generate GPG public key string"),a("\ngpg --armor --export "),s("span",{class:"token operator"},"<"),a("pub-keyID"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token comment"},"# Copy output to GitHub GPG textarea"),a("\n\n"),s("span",{class:"token comment"},"# Git global configuration for GPG signature commits"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global user.signingkey "),s("span",{class:"token operator"},"<"),a("pub-keyID"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"git"),a(" config --global commit.gpgsign "),s("span",{class:"token boolean"},"true"),a("\n\n"),s("span",{class:"token comment"},"# Single signature commit"),a("\n"),s("span",{class:"token function"},"git"),a(" commit -S -m "),s("span",{class:"token string"},'"..."'),a("\n\n"),s("span",{class:"token comment"},"# Import GitHub signature"),a("\n"),s("span",{class:"token function"},"curl"),a(" https://github.com/web-flow.gpg "),s("span",{class:"token operator"},"|"),a(" gpg --import\ngpg --sign-key "),s("span",{class:"token operator"},"<"),a("GitHub-keyID"),s("span",{class:"token operator"},">"),a("\n\n"),s("span",{class:"token comment"},"# Log git signature"),a("\n"),s("span",{class:"token function"},"git"),a(" log --show-signature\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br"),s("span",{class:"line-number"},"19"),s("br"),s("span",{class:"line-number"},"20"),s("br"),s("span",{class:"line-number"},"21"),s("br"),s("span",{class:"line-number"},"22"),s("br")])],-1),qs=s("h3",{id:"license"},[s("a",{class:"header-anchor",href:"#license","aria-hidden":"true"},"#"),a(" LICENSE")],-1),zs=s("h4",{id:"popular-license"},[s("a",{class:"header-anchor",href:"#popular-license","aria-hidden":"true"},"#"),a(" Popular LICENSE")],-1),Ws=s("div",{class:"language-mermaid line-numbers-mode"},[s("pre",null,[s("code",null,"graph TD\nLicense --\x3e A{Open Source}\nA --\x3e|Yes| B{Same License}\nA --\x3e|No| D{List Copyright on Changed}\nB --\x3e|Yes| GPL\nB --\x3e|No| C{Change Docs}\nC --\x3e|Yes| Mozilla\nC --\x3e|No| LGPL\nD --\x3e|Yes| Apache\nD --\x3e|No| E{Enterprise}\nE --\x3e|Yes| MIT\nE --\x3e|No| BSD\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br")])],-1),Ys=s("p",null,[s("img",{src:"/awesome-notes/assets/6_free_software_licenses.344bcf8c.png",alt:"Free Software License"})],-1),Vs=s("h4",{id:"unique-license"},[s("a",{class:"header-anchor",href:"#unique-license","aria-hidden":"true"},"#"),a(" Unique LICENSE")],-1),Ks=s("ul",null,[s("li",null,"CC BY-NC-SA 3.0 License")],-1),Js=s("div",{class:"language-html line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("a")]),a(),s("span",{class:"token attr-name"},"rel"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("license"),s("span",{class:"token punctuation"},'"')]),a(),s("span",{class:"token attr-name"},"href"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("http://creativecommons.org/licenses/by-nc-sa/3.0/"),s("span",{class:"token punctuation"},'"')]),a("\n "),s("span",{class:"token punctuation"},">")]),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("img")]),a("\n "),s("span",{class:"token attr-name"},"alt"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("Creative\nCommons License"),s("span",{class:"token punctuation"},'"')]),a("\n "),s("span",{class:"token special-attr"},[s("span",{class:"token attr-name"},"style"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),s("span",{class:"token value css language-css"},[s("span",{class:"token property"},"border-width"),s("span",{class:"token punctuation"},":"),a("0")]),s("span",{class:"token punctuation"},'"')])]),a("\n "),s("span",{class:"token attr-name"},"src"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("https://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png"),s("span",{class:"token punctuation"},'"')]),a(),s("span",{class:"token punctuation"},"/>")]),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"")]),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("br")]),a(),s("span",{class:"token punctuation"},"/>")]),a("This work is licensed under a\n"),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"<"),a("a")]),a(),s("span",{class:"token attr-name"},"rel"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("license"),s("span",{class:"token punctuation"},'"')]),a(),s("span",{class:"token attr-name"},"href"),s("span",{class:"token attr-value"},[s("span",{class:"token punctuation attr-equals"},"="),s("span",{class:"token punctuation"},'"'),a("http://creativecommons.org/licenses/by-nc-sa/3.0/"),s("span",{class:"token punctuation"},'"')]),a("\n "),s("span",{class:"token punctuation"},">")]),a("Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License"),s("span",{class:"token tag"},[s("span",{class:"token tag"},[s("span",{class:"token punctuation"},"")]),a(".\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br")])],-1),Xs=s("div",{class:"language-markdown line-numbers-mode"},[s("pre",null,[s("code",null,[a("\\*\\*\n"),s("span",{class:"token bold"},[s("span",{class:"token punctuation"},"**"),s("span",{class:"token content"}," May you do good and not evil.\n"),s("span",{class:"token punctuation"},"**")]),a(" May you find forgiveness for yourself and forgive others.\n"),s("span",{class:"token bold"},[s("span",{class:"token punctuation"},"**"),s("span",{class:"token content"}," May you share freely, never taking more than you give.\n"),s("span",{class:"token punctuation"},"**")]),a("\n\\*\\*\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),Qs=s("div",{class:"language-markdown line-numbers-mode"},[s("pre",null,[s("code",null,"DBAD : DON'T BE A DICK PUBLIC LICENSE:\n\nDo whatever you like with the original work, just don't be a dick.\n\nBeing a dick includes - but is not limited to - the following instances:\n\n1a. Outright copyright infringement - Don't just copy this and change the name.\n1b. Selling the unmodified original with no work done what-so-ever,\nthat's REALLY being a dick.\n1c. Modifying the original work to contain hidden harmful content.\nThat would make you a PROPER dick.\n\nIf you become rich through modifications, related work services, or supporting\nthe original work, share the love. Only a dick would make loads off this work\nand not buy the original works creator(s) a pint.Code is provided with no\nwarranty. Using somebody else's code and bitching when it goes wrong makes\nyou a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix\nback.\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br")])],-1),Zs=s("div",{class:"language-markdown line-numbers-mode"},[s("pre",null,[s("code",null,"Homework Public License(HPL)\n\nCopyright (c) 2016 Yilong Liu\n\nThis is for your reference only,not for your cheating - Just don't be a dick.\n\nBeing a dick includes - but is not limited to - the following instances:\n\n1a. Outright copyright infringement - Don't just copy this and change the name.\n1b. Reserve a copy of this project and tell your teacher\nthat it is your own homework - Plagiarism is shame.\n\nIf you become rich through modifications, related work services,\nor supporting the original work, share the love. Only a dick would make loads\noff this work and not buy the original works creator(s) a pint.Code is\nprovided with no warranty. Using somebody else's code and bitching when it\ngoes wrong makes you a DONKEY dick. Fix the problem yourself. A non-dick\nwould submit the fix back.\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br")])],-1),na=s("div",{class:"language-markdown line-numbers-mode"},[s("pre",null,[s("code",null,[a('The Star And Thank Author License (SATA)\n\nCopyright (c) 2016 sabertazimi(sabertazimi@gmail.com)\n\nProject Url: https://github.com/sabertazimi/Awesome-Notes\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nAnd wait, the most important, you shall star/+1/like the project(s) in project url\nsection above first, and then thank the author(s) in Copyright section.\n\nHere are some suggested ways:\n\n'),s("span",{class:"token list punctuation"},"-"),a(" Email the authors a thank-you letter, and make friends with him/her/them.\n"),s("span",{class:"token list punctuation"},"-"),a(" Report bugs or issues.\n"),s("span",{class:"token list punctuation"},"-"),a(" Tell friends what a wonderful project this is.\n"),s("span",{class:"token list punctuation"},"-"),a(' And, sure, you can just express thanks in your mind without telling the world.\n\nContributors of this project by forking have the option to add his/her name and\nforked project url at copyright and project url sections, but shall not delete\nor modify anything else in these two sections.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n')])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br"),s("span",{class:"line-number"},"14"),s("br"),s("span",{class:"line-number"},"15"),s("br"),s("span",{class:"line-number"},"16"),s("br"),s("span",{class:"line-number"},"17"),s("br"),s("span",{class:"line-number"},"18"),s("br"),s("span",{class:"line-number"},"19"),s("br"),s("span",{class:"line-number"},"20"),s("br"),s("span",{class:"line-number"},"21"),s("br"),s("span",{class:"line-number"},"22"),s("br"),s("span",{class:"line-number"},"23"),s("br"),s("span",{class:"line-number"},"24"),s("br"),s("span",{class:"line-number"},"25"),s("br"),s("span",{class:"line-number"},"26"),s("br"),s("span",{class:"line-number"},"27"),s("br"),s("span",{class:"line-number"},"28"),s("br"),s("span",{class:"line-number"},"29"),s("br"),s("span",{class:"line-number"},"30"),s("br"),s("span",{class:"line-number"},"31"),s("br"),s("span",{class:"line-number"},"32"),s("br"),s("span",{class:"line-number"},"33"),s("br"),s("span",{class:"line-number"},"34"),s("br"),s("span",{class:"line-number"},"35"),s("br"),s("span",{class:"line-number"},"36"),s("br"),s("span",{class:"line-number"},"37"),s("br")])],-1),ea=s("h3",{id:"teamwork"},[s("a",{class:"header-anchor",href:"#teamwork","aria-hidden":"true"},"#"),a(" Teamwork")],-1),sa=s("p",null,"如果在组织的托管空间创建版本库,一定要要为版本库指派一个拥有 Push 权限的团队,以免以“Fork + Pull”模式工作时,Pull Request 没有人响应。",-1),aa=s("h4",{id:"pull-request-work-flow"},[s("a",{class:"header-anchor",href:"#pull-request-work-flow","aria-hidden":"true"},"#"),a(" Pull Request Work Flow")],-1),la=s("ol",null,[s("li",null,"Fork it."),s("li",null,[a("Create your feature branch ("),s("code",null,"git checkout -b my-new-feature"),a(").")]),s("li",null,"Ensure tests are passing."),s("li",null,[a("Commit changes ("),s("code",null,"git commit -am 'Add some feature'"),a(").")]),s("li",null,[a("Push to the branch ("),s("code",null,"git push origin my-new-feature"),a(").")]),s("li",null,"Create new Pull Request.")],-1),ta=s("h3",{id:"github-cli-tool"},[s("a",{class:"header-anchor",href:"#github-cli-tool","aria-hidden":"true"},"#"),a(" GitHub CLI Tool")],-1),ia=s("h4",{id:"cli-installation"},[s("a",{class:"header-anchor",href:"#cli-installation","aria-hidden":"true"},"#"),a(" CLI Installation")],-1),ra=s("p",null,[a("Install "),s("code",null,"gh"),a(" by "),s("code",null,"apt"),a(", according of "),s("a",{href:"https://github.com/cli/cli/blob/trunk/docs/install_linux.md",target:"_blank",rel:"noopener noreferrer"},"official introduction"),a(".")],-1),oa=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,"gh auth login\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),ca=s("h4",{id:"gh-issue-usage"},[s("a",{class:"header-anchor",href:"#gh-issue-usage","aria-hidden":"true"},"#"),a(" GH Issue Usage")],-1),ua=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,"gh issue create\ngh issue close\ngh issue status\ngh issue list\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br")])],-1),pa=s("h4",{id:"gh-pr-usage"},[s("a",{class:"header-anchor",href:"#gh-pr-usage","aria-hidden":"true"},"#"),a(" GH PR Usage")],-1),ba=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[a("gh "),s("span",{class:"token function"},"pr"),a(" checkout\ngh "),s("span",{class:"token function"},"pr"),a(" create\ngh "),s("span",{class:"token function"},"pr"),a(" close\ngh "),s("span",{class:"token function"},"pr"),a(" merge\ngh "),s("span",{class:"token function"},"pr"),a(" status\ngh "),s("span",{class:"token function"},"pr"),a(" list\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),da=s("h4",{id:"gh-repo-usage"},[s("a",{class:"header-anchor",href:"#gh-repo-usage","aria-hidden":"true"},"#"),a(" GH Repo Usage")],-1),ma=s("p",null,"Clone repo:",-1),ha=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[a("gh repo clone cli/cli\n\n"),s("span",{class:"token comment"},"# fastest way to clone authorized user repos"),a("\ngh "),s("span",{class:"token builtin class-name"},"alias"),a(),s("span",{class:"token builtin class-name"},"set"),a(" rc "),s("span",{class:"token string"},"'repo clone'"),a("\ngh rc dragon\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br")])],-1),ga=s("p",null,"Create repo:",-1),ka=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token comment"},"# create a repository under your account using the current directory name"),a("\n$ "),s("span",{class:"token function"},"git"),a(" init my-project\n$ "),s("span",{class:"token builtin class-name"},"cd"),a(" my-project\n$ gh repo create\n\n"),s("span",{class:"token comment"},"# create a repository with a specific name"),a("\n$ gh repo create my-project\n\n"),s("span",{class:"token comment"},"# create a repository in an organization"),a("\n$ gh repo create cli/my-project\n\n"),s("span",{class:"token comment"},"# disable issues and wiki"),a("\n$ gh repo create --enable-issues"),s("span",{class:"token operator"},"="),a("false --enable-wiki"),s("span",{class:"token operator"},"="),a("false\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br"),s("span",{class:"line-number"},"9"),s("br"),s("span",{class:"line-number"},"10"),s("br"),s("span",{class:"line-number"},"11"),s("br"),s("span",{class:"line-number"},"12"),s("br"),s("span",{class:"line-number"},"13"),s("br")])],-1),fa=s("p",null,"Push repo:",-1),va=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" init\n\n"),s("span",{class:"token builtin class-name"},"echo"),a(),s("span",{class:"token string"},'"# RepoName"'),a(),s("span",{class:"token operator"},">>"),a(" README.md\n"),s("span",{class:"token function"},"git"),a(),s("span",{class:"token function"},"add"),a(" README.md\n"),s("span",{class:"token function"},"git"),a(" commit -m "),s("span",{class:"token string"},'"Initial commit"'),a("\n\n"),s("span",{class:"token function"},"git"),a(" remote "),s("span",{class:"token function"},"add"),a(" origin git@github.com:username/RepoName.git\n"),s("span",{class:"token function"},"git"),a(" push -u origin master\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br"),s("span",{class:"line-number"},"7"),s("br"),s("span",{class:"line-number"},"8"),s("br")])],-1),wa=s("p",null,"List repo:",-1),ya=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,"gh repo list sabertazimi\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),xa=s("h3",{id:"wiki"},[s("a",{class:"header-anchor",href:"#wiki","aria-hidden":"true"},"#"),a(" Wiki")],-1),Ca=s("h4",{id:"wiki-git-access"},[s("a",{class:"header-anchor",href:"#wiki-git-access","aria-hidden":"true"},"#"),a(" Wiki Git Access")],-1),Aa=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" clone git@github.com:user/repo.wiki.git\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ga=s("h3",{id:"shorten-github-url"},[s("a",{class:"header-anchor",href:"#shorten-github-url","aria-hidden":"true"},"#"),a(" Shorten GitHub URL")],-1),Ia=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"curl"),a(" -i http://git.io -F "),s("span",{class:"token string"},'"url=https://github.com/technoweenie"'),a(" -F "),s("span",{class:"token string"},'"code=t"'),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ea=s("h3",{id:"github-flavored-markdown"},[s("a",{class:"header-anchor",href:"#github-flavored-markdown","aria-hidden":"true"},"#"),a(" GitHub Flavored Markdown")],-1),Ta=s("h4",{id:"link"},[s("a",{class:"header-anchor",href:"#link","aria-hidden":"true"},"#"),a(" Link")],-1),Na=s("h5",{id:"tooltip-of-link"},[s("a",{class:"header-anchor",href:"#tooltip-of-link","aria-hidden":"true"},"#"),a(" Tooltip of Link")],-1),Oa=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,"This is a [link to a web page](https://url.com 'This title will appear as a tooltip').\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Ra=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,"![Alt text](https://imageurl.com 'This is a title')\n")]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Sa=s("h5",{id:"label-of-link"},[s("a",{class:"header-anchor",href:"#label-of-link","aria-hidden":"true"},"#"),a(" Label of Link")],-1),Ha=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,[a("This is a "),s("span",{class:"token url"},[a("["),s("span",{class:"token content"},"link to a web page"),a("]["),s("span",{class:"token variable"},"mylabel"),a("]")]),a(".\n\nThen at the end of the document …\n\n"),s("span",{class:"token url-reference url"},[s("span",{class:"token punctuation"},"["),s("span",{class:"token variable"},"mylabel"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},":"),a(" https://url.com "),s("span",{class:"token string"},"'Optional title'")]),a("\n"),s("span",{class:"token url-reference url"},[s("span",{class:"token punctuation"},"["),s("span",{class:"token variable"},"mylabel"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},":"),a(" https://url.com "),s("span",{class:"token string"},"'Optional title'")]),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),La=s("div",{class:"language-md line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token url"},[s("span",{class:"token operator"},"!"),a("["),s("span",{class:"token content"},"Alt text"),a("]["),s("span",{class:"token variable"},"mylabel"),a("]")]),a("\n\n"),s("span",{class:"token url-reference url"},[s("span",{class:"token punctuation"},"["),s("span",{class:"token variable"},"mylabel"),s("span",{class:"token punctuation"},"]"),s("span",{class:"token punctuation"},":"),a(" https://imageurl.com "),s("span",{class:"token string"},"'This is a title'")]),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br")])],-1),ja=s("h3",{id:"github-pages"},[s("a",{class:"header-anchor",href:"#github-pages","aria-hidden":"true"},"#"),a(" GitHub Pages")],-1),Da=s("p",null,[a("In "),s("code",null,"https://github.com///settings/pages"),a(", setup "),s("code",null,"source"),a(" of pages and "),s("code",null,"Enforce HTTPS"),a(".")],-1),Pa=s("h2",{id:"git-tools"},[s("a",{class:"header-anchor",href:"#git-tools","aria-hidden":"true"},"#"),a(" Git Tools")],-1),Ba=s("h3",{id:"diff-and-patch"},[s("a",{class:"header-anchor",href:"#diff-and-patch","aria-hidden":"true"},"#"),a(" Diff and Patch")],-1),_a=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"diff"),a(" -u "),s("span",{class:"token operator"},"<"),a("src"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("dist"),s("span",{class:"token operator"},">"),a("\n"),s("span",{class:"token function"},"diff"),a(" -Nur "),s("span",{class:"token operator"},"<"),a("src_dir"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("dist_dir"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Ma=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[a("patch -p"),s("span",{class:"token punctuation"},"["),a("num"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token operator"},"<"),a(" patchFile\npatch -dry -run -p"),s("span",{class:"token punctuation"},"["),a("num"),s("span",{class:"token punctuation"},"]"),a(),s("span",{class:"token operator"},"<"),a(" patchFile\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Fa=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"diff"),a(" -Nur program_1.0 program_2.0 "),s("span",{class:"token operator"},">"),a(" program_2.0.patch\npatch -p1 "),s("span",{class:"token operator"},"<"),s("span",{class:"token punctuation"},".."),a("/program_2.0.patch\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Ua=s("h3",{id:"semantic-git-commit-message"},[s("a",{class:"header-anchor",href:"#semantic-git-commit-message","aria-hidden":"true"},"#"),a(" Semantic Git Commit Message")],-1),$a=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/commitizen/cz-cli",target:"_blank",rel:"noopener noreferrer"},"cz-cli")]),s("li",null,[s("a",{href:"https://github.com/commitizen/cz-conventional-changelog",target:"_blank",rel:"noopener noreferrer"},"cz-conventional-changelog")]),s("li",null,[s("a",{href:"https://github.com/conventional-changelog/commitlint",target:"_blank",rel:"noopener noreferrer"},"Commit Linter")])],-1),qa=s("h3",{id:"changelog-generator"},[s("a",{class:"header-anchor",href:"#changelog-generator","aria-hidden":"true"},"#"),a(" Changelog Generator")],-1),za=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/conventional-changelog/standard-version",target:"_blank",rel:"noopener noreferrer"},"standard-version")])],-1),Wa=s("h3",{id:"purge-tool"},[s("a",{class:"header-anchor",href:"#purge-tool","aria-hidden":"true"},"#"),a(" Purge Tool")],-1),Ya=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" rev-list --objects --all\n"),s("span",{class:"token punctuation"},"\\"),a(),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"grep"),a(),s("span",{class:"token string"},[a('"'),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"git"),a(" verify-pack -v .git/objects/pack/*.idx\n"),s("span",{class:"token punctuation"},"\\"),a(),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"sort"),a(" -k "),s("span",{class:"token number"},"3"),a(" -n "),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"tail"),a(" -5 "),s("span",{class:"token operator"},"|"),a(),s("span",{class:"token function"},"awk"),a(),s("span",{class:"token string"},"'{print$1}'"),s("span",{class:"token variable"},")")]),a('"')]),a("\n"),s("span",{class:"token function"},"git"),a(" filter-branch -f --prune-empty --index-filter\n"),s("span",{class:"token punctuation"},"\\"),a(),s("span",{class:"token string"},"'git rm -rf --cached --ignore-unmatch your-file-name'"),a("\n"),s("span",{class:"token punctuation"},"\\"),a(" --tag-name-filter "),s("span",{class:"token function"},"cat"),a(" -- --all\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br"),s("span",{class:"line-number"},"3"),s("br"),s("span",{class:"line-number"},"4"),s("br"),s("span",{class:"line-number"},"5"),s("br"),s("span",{class:"line-number"},"6"),s("br")])],-1),Va=s("h2",{id:"commands-list"},[s("a",{class:"header-anchor",href:"#commands-list","aria-hidden":"true"},"#"),a(" Commands List")],-1),Ka=s("h3",{id:"basic-commands"},[s("a",{class:"header-anchor",href:"#basic-commands","aria-hidden":"true"},"#"),a(" Basic Commands")],-1),Ja=s("h4",{id:"git-config"},[s("a",{class:"header-anchor",href:"#git-config","aria-hidden":"true"},"#"),a(" git config")],-1),Xa=s("h4",{id:"git-help"},[s("a",{class:"header-anchor",href:"#git-help","aria-hidden":"true"},"#"),a(" git help")],-1),Qa=s("h4",{id:"git-init"},[s("a",{class:"header-anchor",href:"#git-init","aria-hidden":"true"},"#"),a(" git init")],-1),Za=s("h4",{id:"git-clone"},[s("a",{class:"header-anchor",href:"#git-clone","aria-hidden":"true"},"#"),a(" git clone")],-1),nl=s("p",null,"clone specific branch",-1),el=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" clone -b branch_name repo_url\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),sl=s("h4",{id:"git-add"},[s("a",{class:"header-anchor",href:"#git-add","aria-hidden":"true"},"#"),a(" git add")],-1),al=s("h4",{id:"git-status"},[s("a",{class:"header-anchor",href:"#git-status","aria-hidden":"true"},"#"),a(" git status")],-1),ll=s("h4",{id:"git-diff"},[s("a",{class:"header-anchor",href:"#git-diff","aria-hidden":"true"},"#"),a(" git diff")],-1),tl=s("h4",{id:"git-difftool"},[s("a",{class:"header-anchor",href:"#git-difftool","aria-hidden":"true"},"#"),a(" git difftool")],-1),il=s("p",null,"外置 diff 工具",-1),rl=s("h4",{id:"git-commit"},[s("a",{class:"header-anchor",href:"#git-commit","aria-hidden":"true"},"#"),a(" git commit")],-1),ol=s("h4",{id:"git-reset"},[s("a",{class:"header-anchor",href:"#git-reset","aria-hidden":"true"},"#"),a(" git reset")],-1),cl=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" reset "),s("span",{class:"token variable"},[s("span",{class:"token variable"},"$("),s("span",{class:"token function"},"git"),a(" merge-base master "),s("span",{class:"token punctuation"},"$("),a("git rev-parse --abbrev-ref HEAD"),s("span",{class:"token punctuation"},")"),s("span",{class:"token variable"},")")]),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),ul=s("ul",null,[s("li",null,[s("code",null,"git rev-parse --abbrev-rev HEAD"),a(" will return the name of the branch currently on")]),s("li",null,[s("code",null,"git merge-base master $(name of your branch)"),a(" will find the best common ancestor between master and current branch")]),s("li",null,[s("code",null,"git reset $(hash of the branch creation)"),a(" will undo all the commits, merges, rebase (preserving changes to the code)")])],-1),pl=s("h4",{id:"git-rm"},[s("a",{class:"header-anchor",href:"#git-rm","aria-hidden":"true"},"#"),a(" git rm")],-1),bl=s("h4",{id:"git-mv"},[s("a",{class:"header-anchor",href:"#git-mv","aria-hidden":"true"},"#"),a(" git mv")],-1),dl=s("h4",{id:"git-clean"},[s("a",{class:"header-anchor",href:"#git-clean","aria-hidden":"true"},"#"),a(" git clean")],-1),ml=s("p",null,"从工作区中移除不想要的文件。可以是编译的临时文件或者合并冲突的文件。",-1),hl=s("h4",{id:"git-branch"},[s("a",{class:"header-anchor",href:"#git-branch","aria-hidden":"true"},"#"),a(" git branch")],-1),gl=s("h4",{id:"git-checkout"},[s("a",{class:"header-anchor",href:"#git-checkout","aria-hidden":"true"},"#"),a(" git checkout")],-1),kl=s("h4",{id:"git-merge"},[s("a",{class:"header-anchor",href:"#git-merge","aria-hidden":"true"},"#"),a(" git merge")],-1),fl=s("h4",{id:"git-mergetool"},[s("a",{class:"header-anchor",href:"#git-mergetool","aria-hidden":"true"},"#"),a(" git mergetool")],-1),vl=s("p",null,"外置 merge 工具",-1),wl=s("h4",{id:"git-log"},[s("a",{class:"header-anchor",href:"#git-log","aria-hidden":"true"},"#"),a(" git log")],-1),yl=s("h4",{id:"git-stash"},[s("a",{class:"header-anchor",href:"#git-stash","aria-hidden":"true"},"#"),a(" git stash")],-1),xl=s("p",null,"临时地保存一些还没有提交的工作,以便在分支上不需要提交未完成工作就可以清理工作目录。",-1),Cl=s("h4",{id:"git-tag"},[s("a",{class:"header-anchor",href:"#git-tag","aria-hidden":"true"},"#"),a(" git tag")],-1),Al=s("h4",{id:"git-fetch"},[s("a",{class:"header-anchor",href:"#git-fetch","aria-hidden":"true"},"#"),a(" git fetch")],-1),Gl=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" fetch "),s("span",{class:"token operator"},"<"),a("repo_name"),s("span",{class:"token operator"},">"),a(),s("span",{class:"token operator"},"<"),a("branch_name"),s("span",{class:"token operator"},">"),a("\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Il=s("h4",{id:"git-pull"},[s("a",{class:"header-anchor",href:"#git-pull","aria-hidden":"true"},"#"),a(" git pull")],-1),El=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" pull --rebase\n"),s("span",{class:"token function"},"git"),a(" pull --allow-unrelated-histories\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Tl=s("h4",{id:"git-push"},[s("a",{class:"header-anchor",href:"#git-push","aria-hidden":"true"},"#"),a(" git push")],-1),Nl=s("h4",{id:"git-remote"},[s("a",{class:"header-anchor",href:"#git-remote","aria-hidden":"true"},"#"),a(" git remote")],-1),Ol=s("h4",{id:"git-archive"},[s("a",{class:"header-anchor",href:"#git-archive","aria-hidden":"true"},"#"),a(" git archive")],-1),Rl=s("p",null,"创建项目一个指定快照的归档文件",-1),Sl=s("h4",{id:"git-submodule"},[s("a",{class:"header-anchor",href:"#git-submodule","aria-hidden":"true"},"#"),a(" git submodule")],-1),Hl=s("p",null,"管理一个仓库的其他外部仓库。 它可以被用在库或者其他类型的共 享资源上.submodule 命令有几个子命令, 如( add 、 update 、 sync 等等)用来管理这些 资源.",-1),Ll=s("ul",null,[s("li",null,"add submodule")],-1),jl=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" submodule "),s("span",{class:"token function"},"add"),a(" git://github.com/rack/rack.git ./lib/rack\n"),s("span",{class:"token function"},"cat"),a(" .gitmodules\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Dl=s("ul",null,[s("li",null,"get submodule")],-1),Pl=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" submodule init\n"),s("span",{class:"token function"},"git"),a(" submodule update\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Bl=s("ul",null,[s("li",null,"sync submodule")],-1),_l=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" pull origin/master --rebase\n"),s("span",{class:"token function"},"git"),a(" submodule update\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br"),s("span",{class:"line-number"},"2"),s("br")])],-1),Ml=s("div",{class:"language-bash line-numbers-mode"},[s("pre",null,[s("code",null,[s("span",{class:"token function"},"git"),a(" submodule update --init --force --remote\n")])]),s("div",{class:"line-numbers-wrapper"},[s("span",{class:"line-number"},"1"),s("br")])],-1),Fl=s("h3",{id:"检查与比较"},[s("a",{class:"header-anchor",href:"#检查与比较","aria-hidden":"true"},"#"),a(" 检查与比较")],-1),Ul=s("h4",{id:"git-show"},[s("a",{class:"header-anchor",href:"#git-show","aria-hidden":"true"},"#"),a(" git show")],-1),$l=s("h4",{id:"git-shortlog"},[s("a",{class:"header-anchor",href:"#git-shortlog","aria-hidden":"true"},"#"),a(" git shortlog")],-1),ql=s("p",null,"创建一个漂亮的 changelog 文件",-1),zl=s("h4",{id:"git-describe"},[s("a",{class:"header-anchor",href:"#git-describe","aria-hidden":"true"},"#"),a(" git describe")],-1),Wl=s("p",null,"接受任何可以解析成一个提交的东西,然后生成一个人类可读的字符串且不可变。 这是一种获得一个提交的描述的方式,它跟一个提交的 SHA-1 值一样是无歧义,但是更具可读性。",-1),Yl=s("h3",{id:"调试"},[s("a",{class:"header-anchor",href:"#调试","aria-hidden":"true"},"#"),a(" 调试")],-1),Vl=s("h4",{id:"git-bisect"},[s("a",{class:"header-anchor",href:"#git-bisect","aria-hidden":"true"},"#"),a(" git bisect")],-1),Kl=s("p",null,"通过自动进行一个二分查找来找到哪一个特定的提交是导致 bug 或者问题的第一个提交。",-1),Jl=s("h4",{id:"git-blame"},[s("a",{class:"header-anchor",href:"#git-blame","aria-hidden":"true"},"#"),a(" git blame")],-1),Xl=s("h4",{id:"git-grep"},[s("a",{class:"header-anchor",href:"#git-grep","aria-hidden":"true"},"#"),a(" git grep")],-1),Ql=s("p",null,"查找任何字符串或者正则表达式",-1),Zl=s("h3",{id:"补丁"},[s("a",{class:"header-anchor",href:"#补丁","aria-hidden":"true"},"#"),a(" 补丁")],-1),nt=s("h4",{id:"git-cherry-pick"},[s("a",{class:"header-anchor",href:"#git-cherry-pick","aria-hidden":"true"},"#"),a(" git cherry-pick")],-1),et=s("p",null,"获得在单个提交中引入的变更,然后尝试将作为一个新的提交引入到你当前分支上",-1),st=s("h4",{id:"git-rebase"},[s("a",{class:"header-anchor",href:"#git-rebase","aria-hidden":"true"},"#"),a(" git rebase")],-1),at=s("h4",{id:"git-revert"},[s("a",{class:"header-anchor",href:"#git-revert","aria-hidden":"true"},"#"),a(" git revert")],-1),lt=s("h3",{id:"邮件"},[s("a",{class:"header-anchor",href:"#邮件","aria-hidden":"true"},"#"),a(" 邮件")],-1),tt=s("h4",{id:"git-apply"},[s("a",{class:"header-anchor",href:"#git-apply","aria-hidden":"true"},"#"),a(" git apply")],-1),it=s("p",null,"应用一个通过 git diff 或者甚至使用 GNU diff 命令创建的补丁",-1),rt=s("h4",{id:"git-am"},[s("a",{class:"header-anchor",href:"#git-am","aria-hidden":"true"},"#"),a(" git am")],-1),ot=s("p",null,"应用来自邮箱的补丁",-1),ct=s("h4",{id:"git-format-patch"},[s("a",{class:"header-anchor",href:"#git-format-patch","aria-hidden":"true"},"#"),a(" git format-patch")],-1),ut=s("p",null,"mailbox 的格式来生成一系列的补丁以便你可以发送到一个邮件列表中",-1),pt=s("h4",{id:"git-imap-send"},[s("a",{class:"header-anchor",href:"#git-imap-send","aria-hidden":"true"},"#"),a(" git imap-send")],-1),bt=s("p",null,"将一个由 git format-patch 生成的邮箱上传至 IMAP 草稿文件夹",-1),dt=s("h4",{id:"git-send-email"},[s("a",{class:"header-anchor",href:"#git-send-email","aria-hidden":"true"},"#"),a(" git send-email")],-1),mt=s("p",null,"通过邮件发送那些使用 git format-patch 生成的补丁",-1),ht=s("h4",{id:"git-request-pull"},[s("a",{class:"header-anchor",href:"#git-request-pull","aria-hidden":"true"},"#"),a(" git request-pull")],-1),gt=s("h3",{id:"外部系统"},[s("a",{class:"header-anchor",href:"#外部系统","aria-hidden":"true"},"#"),a(" 外部系统")],-1),kt=s("h4",{id:"git-svn"},[s("a",{class:"header-anchor",href:"#git-svn","aria-hidden":"true"},"#"),a(" git svn")],-1),ft=s("h4",{id:"git-fast-import"},[s("a",{class:"header-anchor",href:"#git-fast-import","aria-hidden":"true"},"#"),a(" git fast-import")],-1),vt=s("p",null,"对于其他版本控制系统或者从其他任何的格式导入,你可以使用 git fast-import 快速地将其他格式映射到 Git 可以轻松记录的格式",-1),wt=s("h3",{id:"管理"},[s("a",{class:"header-anchor",href:"#管理","aria-hidden":"true"},"#"),a(" 管理")],-1),yt=s("h4",{id:"git-gc"},[s("a",{class:"header-anchor",href:"#git-gc","aria-hidden":"true"},"#"),a(" git gc")],-1),xt=s("p",null,"在你的仓库中执行 ``garbage collection'' ,删除数据库中不需要的文件和将其他文件打包成一种更有效的格式",-1),Ct=s("h4",{id:"git-fsck"},[s("a",{class:"header-anchor",href:"#git-fsck","aria-hidden":"true"},"#"),a(" git fsck")],-1),At=s("p",null,"检查内部数据库的问题或者不一致性",-1),Gt=s("h4",{id:"git-reflog"},[s("a",{class:"header-anchor",href:"#git-reflog","aria-hidden":"true"},"#"),a(" git reflog")],-1),It=s("p",null,"分析你所有分支的头指针的日志来查找出你在重写历史上可能丢失的提交",-1),Et=s("h4",{id:"git-filter-branch"},[s("a",{class:"header-anchor",href:"#git-filter-branch","aria-hidden":"true"},"#"),a(" git filter-branch")],-1),Tt=s("p",null,"根据某些规则来重写大量的提交记录,例如从任何地方删除文件,或者通过过滤一个仓库中的一个单独的子目录以提取出一个项目",-1),Nt=s("h4",{id:"git-note"},[s("a",{class:"header-anchor",href:"#git-note","aria-hidden":"true"},"#"),a(" git-note")],-1),Ot=s("p",null,"为特定 commit 添加 note,一个 commit 只能有一个 note",-1);t.render=function(s,a,l,t,Rt,St){return n(),e("div",null,[i,r,o,c,u,p,b,d,m,h,g,k,f,v,w,y,x,C,A,G,I,E,T,N,O,R,S,H,L,j,D,P,B,_,M,F,U,$,q,z,W,Y,V,K,J,X,Q,Z,nn,en,sn,an,ln,tn,rn,on,cn,un,pn,bn,dn,mn,hn,gn,kn,fn,vn,wn,yn,xn,Cn,An,Gn,In,En,Tn,Nn,On,Rn,Sn,Hn,Ln,jn,Dn,Pn,Bn,_n,Mn,Fn,Un,$n,qn,zn,Wn,Yn,Vn,Kn,Jn,Xn,Qn,Zn,ne,ee,se,ae,le,te,ie,re,oe,ce,ue,pe,be,de,me,he,ge,ke,fe,ve,we,ye,xe,Ce,Ae,Ge,Ie,Ee,Te,Ne,Oe,Re,Se,He,Le,je,De,Pe,Be,_e,Me,Fe,Ue,$e,qe,ze,We,Ye,Ve,Ke,Je,Xe,Qe,Ze,ns,es,ss,as,ls,ts,is,rs,os,cs,us,ps,bs,ds,ms,hs,gs,ks,fs,vs,ws,ys,xs,Cs,As,Gs,Is,Es,Ts,Ns,Os,Rs,Ss,Hs,Ls,js,Ds,Ps,Bs,_s,Ms,Fs,Us,$s,qs,zs,Ws,Ys,Vs,Ks,Js,Xs,Qs,Zs,na,ea,sa,aa,la,ta,ia,ra,oa,ca,ua,pa,ba,da,ma,ha,ga,ka,fa,va,wa,ya,xa,Ca,Aa,Ga,Ia,Ea,Ta,Na,Oa,Ra,Sa,Ha,La,ja,Da,Pa,Ba,_a,Ma,Fa,Ua,$a,qa,za,Wa,Ya,Va,Ka,Ja,Xa,Qa,Za,nl,el,sl,al,ll,tl,il,rl,ol,cl,ul,pl,bl,dl,ml,hl,gl,kl,fl,vl,wl,yl,xl,Cl,Al,Gl,Il,El,Tl,Nl,Ol,Rl,Sl,Hl,Ll,jl,Dl,Pl,Bl,_l,Ml,Fl,Ul,$l,ql,zl,Wl,Yl,Vl,Kl,Jl,Xl,Ql,Zl,nt,et,st,at,lt,tt,it,rt,ot,ct,ut,pt,bt,dt,mt,ht,gt,kt,ft,vt,wt,yt,xt,Ct,At,Gt,It,Et,Tt,Nt,Ot])};export default t;export{l as __pageData}; diff --git a/assets/programming_tools_vim_vimBasicNotes.md.bfbb803e.js b/assets/programming_tools_vim_vimBasicNotes.md.bfbb803e.js new file mode 100644 index 00000000000..95ee24b2127 --- /dev/null +++ b/assets/programming_tools_vim_vimBasicNotes.md.bfbb803e.js @@ -0,0 +1 @@ +import{o as t,c as e,a as l}from"./app.6dd2a1b7.js";const a='{"title":"Vim Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Normal 模式命令","slug":"normal-模式命令"},{"level":3,"title":"重复命令","slug":"重复命令"},{"level":3,"title":"撤销命令","slug":"撤销命令"},{"level":3,"title":"复合命令","slug":"复合命令"},{"level":3,"title":"编辑命令","slug":"编辑命令"},{"level":2,"title":"Insert 模式命令","slug":"insert-模式命令"},{"level":3,"title":"模式转换","slug":"模式转换"},{"level":3,"title":"I 插入","slug":"i-插入"},{"level":3,"title":"I 删除","slug":"i-删除"},{"level":3,"title":"I 粘贴","slug":"i-粘贴"},{"level":2,"title":"可视模式 (Visual Mode)","slug":"可视模式-visual-mode"},{"level":2,"title":"底行模式","slug":"底行模式"},{"level":3,"title":"补全","slug":"补全"},{"level":3,"title":"Range","slug":"range"},{"level":3,"title":"撤销/重复","slug":"撤销-重复"},{"level":3,"title":"Shell","slug":"shell"},{"level":3,"title":"Range 替换","slug":"range-替换"},{"level":3,"title":"global","slug":"global"},{"level":2,"title":"文件与缓冲区","slug":"文件与缓冲区"},{"level":3,"title":"File Search","slug":"file-search"},{"level":3,"title":"缓冲区","slug":"缓冲区"},{"level":3,"title":"保存文件","slug":"保存文件"},{"level":3,"title":"窗口","slug":"窗口"},{"level":3,"title":"标签","slug":"标签"},{"level":3,"title":"文件管理器","slug":"文件管理器"},{"level":2,"title":"宏","slug":"宏"},{"level":2,"title":"Tools","slug":"tools"},{"level":3,"title":"Vundle","slug":"vundle"},{"level":3,"title":"Color Schemes","slug":"color-schemes"},{"level":3,"title":"YouCompleteMe","slug":"youcompleteme"},{"level":3,"title":"ctags","slug":"ctags"},{"level":3,"title":"make","slug":"make"},{"level":3,"title":"内置补全","slug":"内置补全"},{"level":3,"title":"git","slug":"git"},{"level":2,"title":"Documentation","slug":"documentation"},{"level":2,"title":"Error","slug":"error"},{"level":3,"title":"Swap Backup File","slug":"swap-backup-file"},{"level":3,"title":"Error Encoding","slug":"error-encoding"}],"relativePath":"programming/tools/vim/vimBasicNotes.md","lastUpdated":1627196177000}',s={},n=l('

Vim Basic Notes

Normal 模式命令

重复命令

.

微型宏: 重复上一次(插入命令 至 <Esc>)录制动作

;

重复 t/T/f/F

@

命令作用
@{register}重复寄存器中的宏命令
@:重复任意 Ex 命令
@@重复@命令

:&

重复:substitute


撤销命令

目的操作重复回退
做出一个修改{edit}.u
在行内查找下一指定字符f{char}/t{char};,
在行内查找上一指定字符F{char}/T{char};,
在文档中查找下一处匹配项/pattern<CR>nN
在文档中查找上一处匹配项?pattern<CR>nN
执行替换😒/target/replacement&u
执行一系列修改qx{changes}q@xu

复合命令

插入

复合命令等效的长命令
Cc$
scl
S^c
I^i
A$a
oA<CR>
Oko

编辑命令

Operator + {motion} 精神

操作符命令 + 动作命令(范围)

:h operator :h :map-operator

  • 操作符命令: 基本编辑命令
命令作用
c修改
d删除
y复制到寄存器
g~反转大小写
gu转换为小写
gU转换为大写
>增加缩进
<减小缩进
=自动缩进
!使用外部程序过滤{motion}所跨越的行
  • 动作命令(a(all)/i(in)/t(til))

e.g $/2l/aw/ap 或 /patterns

  • 模式串: /patterns
  • 0/$: 行首/尾
  • 文本对象: i/a
  • 分隔符文本对象(separator text objects)
分隔符文本对象选择区域
a) 或 ab一对圆括号(parentheses)
i) 或 ib圆括号(parentheses) 内部
a} 或 aB一对花括号{braces}
i} 或 iB花括号{braces} 内部
a]一对方括号[brackets]
i]方括号[brackets] 内部
a>一对尖括号 <angle brackets>
i>尖括号 <angle brackets> 内部
a'一对单引号'single quotes'
i'单引号'single quotes' 内部
a"一对双引号"double quotes"
i"双引号"double quotes" 内部
a`一对反引号`backTicks`
i`反引号`backTicks` 内部
at一对 XML 标签<xml>tags</xml>
itXML 标签内部
  • 范围文本对象(range text objects)
范围文本对象选择范围
iw当前单词
aw当前单词及一个空格
iW当前字串
aW当前字串及一个空格
is当前句子
as当前句子及一个空格
ip当前段落
ap当前段落及一个空行

插入编辑命令

复合命令等效的长命令
Cc$
scl
S^c
I^i
A$a
oA<CR>
Oko

register

{register}{operator}{motion} / <C-r>{register}

寄存器符号作用使用范例
""无名寄存器e.g yiw / diw
"a ~ "z有名寄存器e.g "ayiw
"0复制专用寄存器e.g yiw + diw + "0p
"_黑洞寄存器/有去无回寄存器e.g "_diw
"+系统剪贴板寄存器e.g "+p / InsertMode <C-r>+
"*上次高亮文本寄存器e.g "+p / InsertMode <C-r>*
"=表达式寄存器e.g "+p / InsertMode <C-r>=
"%当前文件名
"#轮换文件名
".上次插入的文本
":上次执行的 Ex 命令
"/上次查找的模式

:reg {register} 显示寄存器内容

删除

{number}{register}d/s/x{motion}

复制

{number}{register}y{motion}

粘贴

{number}{register}p{motion}

选择

*

选中包含光标处字符的单词

跳转

基本域
命令光标动作
%跳转至下一个配对的范围符号
j向下移动一个实际行
gj向下移动一个屏幕行
k向上移动一个实际行
gk向上移动一个屏幕行
w正向移动到下一单词的开头
b反向移动到当前单词/上一单词的开头
e正向移动到当前单词/下一单词的结尾
ge反向移动到上一单词的结尾
0移动到实际行的行首
g0移动到屏幕行的行首
$移动到实际行的行尾
g$移动到屏幕行的行尾
^移动到实际行的第一个非空白字符
g^移动到屏幕行的第一个非空白字符
_first char of line
g_last char of line
Hjump to top of screen
Mjump to middle of screen
Ljump to bottom of screen
ztcurrent line to top of screen
zzcurrent line to middle of screen
zbcurrent line to bottom of screen
<C-u>/<C-d>翻半页
<C-b>/<C-f>翻半页
gdjump to definition
gijump to last insert position
#jump to previous same word
*jump to next same word

:set path?

查看'path'值

命令作用
[count]G跳转到指定的行号
/pattern<CR>/?pattern<CR>/n/N跳转到下一个/上一个模式出现之处
%跳转到匹配的括号所在之处
(/)跳转到上一句/下一句的开头
{/}跳转到上一段/下一段的开头
gf跳转到光标下的文件名
gg文首
G文尾
<N>ggjump to line
<N>%jump to percent of file
<C-]>跳转到光标下关键字的定义之处
`{mark}跳转到一个位置标记
标记

m{alpha} 标记当前光标处,标记名为 alpha

位置标记跳转到
`{alpha}自定义标记处
``当前文件中上次跳转动作之前的位置
`.上次修改的地方
`^上次插入的地方
`[上次修改或复制的起始位置
`]上次修改或复制的结束位置
`<上次高亮选区的起始位置
`>上次高亮选区的结束位置
拼写检查
命令作用
]s跳到下一处拼写错误
[s跳到上一处拼写错误
z=为当前单词提供更正建议
zg把当前单词添加到拼写文件中
zw把当前单词从拼写文件中删除
zug撤销针对当前单词的 zg 或 zw 命令

查找

字符
命令光标动作
w正向移动到下一单词的开头
b反向移动到当前单词/上一单词的开头
e正向移动到当前单词/下一单词的结尾
ge反向移动到上一单词的结尾
;重复/f/F/t/T
,撤销/f/F/t/T

<C-a>/<C-x>: 查找数字,递增/减数字

/pattern ?pattern
原字符
原字符作用范例
\\c忽略大小写
\\C大小写敏感
\\v开启 very magic 模式(可省略转义符)
< 与 >\\v 模式下的单词定界符
%()分组符/\\v%(And|D)rew
\\V开启原义模式(可省略转义符):
\\w匹配单词类字符(alpha,number,"_")
\\W匹配非单词字符
\\x匹配十六进制数[0-9a-fA-F]
\\_s匹配空白符/换行符
\\zs界定一个匹配的开始
\\ze界定一个匹配的结束
Flags

置于 patterns 末尾

Flags作用
/g全局
/e光标至匹配词末尾
/c确认或拒绝每一处修改
/n会抑制正常的替换行为,只报告本次 substitute 命令匹配的个数
/e屏蔽错误提示
/&重复上次 flags

替换

:h gU

  • gU{motion} 小写转大写 e.gUaw
  • r{char} 替换光标所在字符
  • gr{char}
  • R/gR 进入替换模式

缩进

>{motion}

e.g >G: 缩进全文

={motion}

gg=G 自动缩进全文

注释

\\{motion}

vim-commentary 插件

\\\\ 注释行 \\ap 注释段落

历史记录

u: 回退命令 <C-r>: 前进命令

Insert 模式命令

模式转换

<C-o> - 进入临时 normal 模式

I 插入

特殊字符

:h digraphs-default :h digraph-table :digraphs

命令作用
<C-v>{123}以十进制字符编码插入字符
<C-v>u{1234}以十六进制字符编码插入字符
<C-v>{nonDigit}按原义插入非数字字符
<C-k>{char1}{char2}插入以二合字母{char1}{char2}表示的字符

I 删除

命令作用
<C-h>删除前一个字符(同退格键)
<C-w>删除前一个单词
<C-u>删至行首

I 粘贴

  • <C-r>{register} 粘贴
  • <C-r><C-p>{register} 智能粘贴
寄存器作用
0普通寄存器
=算术寄存器: 在下方输入表达式后,自动粘贴运算结果

可视模式 (Visual Mode)

命令作用
v激活面向字符的可视模式
V激活面向行的可视模式
<C-v>激活面向列块的可视模式
gv重选上次的高亮选区
shift-vselect current line
gqsplit selected line into multiple lines
  • o 切换高亮选区的活动端

底行模式

:h ex-cmd-index

黑魔法列表:

  • :normal 将 Ex 命令与 Normal 指令结合在一起的黑魔法
  • :args 文件列表
  • :argdo {ExCommands} 对存于文件列表的所有文件执行 Ex 命令
  • :global 对 patterns 执行 EX 命令
命令作用
:edit/:write读写文件
:tabnew创建新标签页
:split分割窗口
:prev/:next前/后标签页
:bprev/:bnext前/后缓冲区列表
:copy命令对快速复制一行非常好用
:[range]delete [x]删除范围内的行[到寄存器 x 中]
:[range]yank [x]复制范围的行[到寄存器 x 中]
:[line]put [x]在行后粘贴寄存器 x 中的内容
:[range]copy/t/co{address}行拷贝到{address} 行之下
:[range]move/m {address}行移动到{address} 行之下
:[range]join连接指定范围内的行
:[range]normal {commands}每一行执行命令{commands}
:[range]substitute/{pattern}/{string}/[flags]{pattern}替换为{string}
:[range]global/{pattern}/[cmd]对指定范围内匹配
:shell打开内置 shell
:!{commands}执行外部命令
:args {argList}文件列表
:argdo {ExCommands}对文件列表中的文件执行命令
:grep
:vimgrep
:make
cnoremap <expr> %% getcmdtype( ) == ':' ? expand('%:h').'/' : '%%'\n
1

补全

自动补全

<C-d>

粘贴补全

<C-r><C-w>: 将光标所在单词粘贴至命令行

补全历史记录

q/ 或 q: 弹出历史记录窗口

Range

  • {start},{end}
  • number 表示行号,.表示当前行,$表示最后一行,%表示所有行
  • /start_pattern/,/end_pattern/
符号地址
1文件的第一行
$文件的最后一行
0虚拟行,位于文件第一行上方
.光标所在行
'm包含位置标记 m 的行
'<高亮选区的起始行
'>高亮选区的结束行
%整个文件(:1,$ 的简写形式)

e.g 1 第 1 行 % 所有行 2,$ 第 2 行 - 最后一行 /<html>/,/<\\/html>/ 标签 html 所有行

撤销/重复

  • @: 重复命令
  • <C-o> 撤销命令

Shell

命令作用
:shell启动一个 shell (输入 exit 返回 Vim)
:!{cmd}在 shell 中执行{cmd}
:read !{cmd}在 shell 中执行{cmd} ,并把其标准输出插入到光标下方
:[range]write !{cmd}在 shell 中执行{cmd} ,以[range] 作为其标准输入
:[range]!{filter}使用外部程序{filter} 过滤指定的[range]

Range 替换

Abolish.vim:超级 substitute 命令

:[range]s[substitute]/{pattern}/{string}/[flags]

Range 替换原字符

原字符作用
\\c忽略大小写
\\C大小写敏感
\\v开启 very magic 模式(可省略转义符)
< 与 >\\v 模式下的单词定界符
%( 与 )分组符
\\V开启原义模式(可省略转义符)
\\w匹配单词类字符(alpha,number,"_")
\\W匹配非单词字符
\\x匹配十六进制数[0-9a-fA-F]
_s匹配空白符/换行符
\\zs界定一个匹配的开始
\\ze界定一个匹配的结束

Range 替换 Flags

置于 patterns 末尾

Flags作用
/g全局
/e光标至匹配词末尾
/c确认或拒绝每一处修改
/n会抑制正常的替换行为,只报告本次 substitute 命令匹配的个数
/e屏蔽错误提示
/&重复上次 flags
/c 后选项作用
y替换此处匹配
n忽略此处匹配
q退出替换过程
l“last” —— 替换此处匹配后退出
a“all” —— 替换此处与之后所有的匹配
<C-e>向上滚动屏幕
<C-y>向下滚动屏幕

替换域中的特殊字符

符号 作用 |:---------------|:--------------------------------------------------| |\\r|插入一个换行符| |\\t|插入一个制表符| |\\|插入一个反斜杠| |\\1|插入第 1 个子匹配| |\\2|插入第 2 个子匹配(以此类推,最多到\\9)| |\\0|插入匹配模式的所有内容| |&|插入匹配模式的所有内容| |~|使用上一次调用:substitute 时的{string}| |={Vim script}|执行{Vim Script} 表达式;并将返回的结果作为替换{string}|

global

  • :[range] global[!] /{pattern}/[ExCommands]
  • :g/{start}/ .,{finish} [cmd]

':g/re/d' 删除所有的匹配行 ':v/re/d' 只保留匹配行 追加存储 TODO 项至 a 寄存器 :g/TODO/yank A > :reg a

文件与缓冲区

built-in fuzzy file search :find or :tabnew

set nocompatible\nset number\n\nsyntax on\nfiletype plugin indent on\n\n" :find filepath\n" :tabnew filepath\n" :tabp :tabn\n" :b partOfFilepath\n" :ls\nset path+=**\nset wildmenu\nset wildmode=list:longest,full\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

:ls to list opened file, :b to jump to specific file

缓冲区

命令作用
:b{bufferNumber/bufferName}跳转至指定缓冲区
:bd{bufferNumber/bufferName}删除指定缓冲区

保存文件

命令作用
:w[rite]把缓冲区内容写入磁盘
:e[dit]!把磁盘文件内容读入缓冲区(即回滚所做修改)
:qa[ll]!关闭所有窗口,摒弃修改而无需警告
:wa[ll]!把所有改变的缓冲区写入磁盘

窗口

  • 创建窗口
命令作用
<C-w>s水平切分当前窗口,新窗口仍显示当前缓冲区
<C-w>v垂直切分当前窗口,新窗口仍显示当前缓冲区
:sp[lit] {file}水平切分当前窗口,并在新窗口中载入{file}
:vsp[lit] {file}垂直切分当前窗口,并在新窗口中载入{file}
  • 切换窗口
命令作用
<C-w>w/<C-w><C-w>在窗口间循环切换
<C-w>h切换到左边的窗口
<C-w>j切换到下边的窗口
<C-w>k切换到上边的窗口
<C-w>l切换到右边的窗口
  • 自定义窗口

:h window-moving

命令作用
<C-w>=使所有窗口等宽、等高
<C-w>_最大化活动窗口的高度
<C-w>|最大化活动窗口的宽度
[N]<C-w>_把活动窗口的高度设为[N]行
[N]<C-w>|把活动窗口的宽度设为[N]列
:windo lcd {path}设置当前标签页的所有窗口的工作目录
  • 关闭窗口
Ex 命令Normal 命令作用
:clo[se]<C-w>c关闭活动窗口
:on[ly]<C-w>o只保留活动窗口,关闭其他所有窗口

标签

  • 新建/关闭标签页
命令作用
:tabe[dit] {filename}在新标签页中打开{filename}
<C-w>T把当前窗口移到一个新标签页
:tabc[lose]关闭当前标签页及其中的所有窗口
:tabo[nly]只保留活动标签页,关闭所有其他标签页
  • 切换标签页
Ex 命令Normal 命令作用
:tabn[ext] {N}{N}gt切换到编号为{N} 的标签页
:tabn[ext]gt切换到下一标签页
:tabp[revious]gT切换到上一标签页

-重排标签页

:tabmove [N] - N 为 0 时,当前标签页移至首;N 省略时,至尾

文件管理器

Ex 命令作用
:edit ./:e.打开文件管理器,并显示当前工作目录
:Explore/:E打开文件管理器,并显示活动缓冲区所在的目录

命令作用
q{register}开始录制宏,并将其保存到指定寄存器,再次按 q 结束录制
{number}@{register}命令执行指定寄存器的内容
@@重复@命令

register 为小写时,覆盖录制宏;register 为大写时,追加录制宏

Tools

Vundle

git clone https://github.com/VundleVim/Vundle.vim ~/.vim/bundle/Vundle.vim\n
1
:PluginInstall\n
1

Color Schemes

  • built-in fuzzy file search :find
  • need config
  • :AirlineTheme {theme}
  • :colorscheme {theme}
set nocompatible\nset number\n\nsyntax on\nfiletype plugin indent on\n\nset shiftwidth=4\nset softtabstop=4\n\nset spell spelllang=en_us\nscriptencoding utf-8\n\nset mouse=a\nset mousehide\n\nset ignorecase\nset smartcase\n\n" :find filepath\n" :tabnew filepath\n" :tabp :tabn\n" :b partOfFilepath\n" :ls\nset path+=**\nset wildmenu\nset wildmode=list:longest,full\n\n" NERDtree like setup for netrw\nlet g:netrw_banner = 0\nlet g:netrw_liststyle = 3\nlet g:netrw_browse_split = 4\nlet g:netrw_altv = 1\nlet g:netrw_winsize = 25\n" let g:netrw_list_hide=netrw_gitignore#Hide()\n" let g:netrw_list_hide.=',\\(^\\|\\s\\s\\)\\zs\\.\\S\\+'\naugroup ProjectDrawer\nautocmd!\nautocmd VimEnter * :Vexplore\naugroup END\n\n" ctags setup\ncommand! MakeTags !ctags -R .\n\n" colorschemes config\ncolorscheme Monokai\n\n" airline config\n" set laststatus=2\nlet g:airline#extensions#tabline#enabled = 1\nlet g:airline#extensions#tabline#left_sep = ' '\nlet g:airline#extensions#tabline#left_alt_sep = '|'\n\n" CtrlP config\nlet g:ctrlp_map = '<c-p>'\nlet g:ctrlp_cmd = 'CtrlP'\nlet g:ctrlp_working_path_mode = 'ra'\nlet g:ctrlp_user_command = 'find %s -type f'\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

YouCompleteMe

need compile and config

sudo apt-get install build-essential cmake python-dev python3-dev\ncd ~/.vim/bundle/YouCompleteMe\n./install.py --clang-completer\n
1
2
3
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py'\n
1

ctags

" ^] jump\n" g^] for ambiguous tags\n" ^t jump back\ncommand! MakeTags !ctags -R .\n
1
2
3
4
命令作用
<C-]>跳转到匹配当前光标所在关键字的第一处标签
g<C-]>跳转至匹配当前光标所在的关键字
:tag {keyword}跳转到匹配{keyword}的第一处标签
:tjump {keyword}提示用户从匹配{keyword}的多处标签中指定一处进行跳转
:pop 或 <C-t>反向遍历标签历史
:tag正向遍历标签历史
:tnext跳转到下一处匹配的标签
:tprev跳转到上一处匹配的标签
:tfirst跳转到第一处匹配的标签
:tlast跳转到最后一处匹配的标签
:tselect提示用户从标签匹配列表中选择一项进行跳转

make

QuickFix 命令作用
:cnext跳转到下一项
:cprev跳转到上一项
:cfirst跳转到第一项
:clast跳转到最后一项
:cnfile跳转到下一个文件中的第一项
:cpfile跳转到上一个文件中的最后一项
:cc N跳转到第 n 项
:copen打开 QuickFix 窗口
:cclose关闭 QuickFix 窗口

内置补全

In insert mode

命令补全类型
<C-n>普通关键字
<C-x><C-n>当前缓冲区关键字
<C-x><C-i>包含文件关键字
<C-x><C-]>tag 关键字
<C-x><C-k>字典查找
<C-x><C-l>整行补全
<C-x><C-f>文件名补全
<C-x><C-o>全能(Omni)补全

可供弹出式菜单使用的命令

按键操作作用
<C-n>使用来自补全列表的下一个匹配项(next 匹配项)
<C-p>使用来自补全列表的上一个匹配项(previous 匹配项)
<Down>选择来自补全列表的下一个匹配项
<Up>选择来自补全列表的上一个匹配项
<C-y>确认使用当前选中的匹配项(yes)
<C-e>还原最早输入的文本(从自动补全中 exit)
<C-h>(与<BS>)从当前匹配项中删除一个字符
<C-l>从当前匹配项中增加一个字符
{char}中止自动补全并插入字符{char}

git

  • Gblame + key o
  • Glog + [q/]q/[Q/]Q
  • Gdiff
  • Gcommit/Gstatus
  • Gpull/Gpush

Documentation

:help:

  • :help ^n: <C-n> docs in normal mode
  • :help i_^n: <C-n> docs in insert mode
  • :help c_^n: <C-n> docs in commandLine mode
  • :helpgrep command: search docs includes command

Error

Swap Backup File

can't open swap or backup file:

  • create :set directory? directory
  • chown of directory to ${whoami}

Error Encoding

set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bom,cp936,latin1\nset fileformats=unix,dos,mac\nset enc=utf8\nset fencs=utf8,gbk,gb2312,gb18030\nset termencoding=utf-8\n
1
2
3
4
5
',196);s.render=function(l,a,s,d,r,i){return t(),e("div",null,[n])};export default s;export{a as __pageData}; diff --git a/assets/programming_tools_vim_vimBasicNotes.md.bfbb803e.lean.js b/assets/programming_tools_vim_vimBasicNotes.md.bfbb803e.lean.js new file mode 100644 index 00000000000..ddebce3204b --- /dev/null +++ b/assets/programming_tools_vim_vimBasicNotes.md.bfbb803e.lean.js @@ -0,0 +1 @@ +import{o as t,c as e,a as l}from"./app.6dd2a1b7.js";const a='{"title":"Vim Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Normal 模式命令","slug":"normal-模式命令"},{"level":3,"title":"重复命令","slug":"重复命令"},{"level":3,"title":"撤销命令","slug":"撤销命令"},{"level":3,"title":"复合命令","slug":"复合命令"},{"level":3,"title":"编辑命令","slug":"编辑命令"},{"level":2,"title":"Insert 模式命令","slug":"insert-模式命令"},{"level":3,"title":"模式转换","slug":"模式转换"},{"level":3,"title":"I 插入","slug":"i-插入"},{"level":3,"title":"I 删除","slug":"i-删除"},{"level":3,"title":"I 粘贴","slug":"i-粘贴"},{"level":2,"title":"可视模式 (Visual Mode)","slug":"可视模式-visual-mode"},{"level":2,"title":"底行模式","slug":"底行模式"},{"level":3,"title":"补全","slug":"补全"},{"level":3,"title":"Range","slug":"range"},{"level":3,"title":"撤销/重复","slug":"撤销-重复"},{"level":3,"title":"Shell","slug":"shell"},{"level":3,"title":"Range 替换","slug":"range-替换"},{"level":3,"title":"global","slug":"global"},{"level":2,"title":"文件与缓冲区","slug":"文件与缓冲区"},{"level":3,"title":"File Search","slug":"file-search"},{"level":3,"title":"缓冲区","slug":"缓冲区"},{"level":3,"title":"保存文件","slug":"保存文件"},{"level":3,"title":"窗口","slug":"窗口"},{"level":3,"title":"标签","slug":"标签"},{"level":3,"title":"文件管理器","slug":"文件管理器"},{"level":2,"title":"宏","slug":"宏"},{"level":2,"title":"Tools","slug":"tools"},{"level":3,"title":"Vundle","slug":"vundle"},{"level":3,"title":"Color Schemes","slug":"color-schemes"},{"level":3,"title":"YouCompleteMe","slug":"youcompleteme"},{"level":3,"title":"ctags","slug":"ctags"},{"level":3,"title":"make","slug":"make"},{"level":3,"title":"内置补全","slug":"内置补全"},{"level":3,"title":"git","slug":"git"},{"level":2,"title":"Documentation","slug":"documentation"},{"level":2,"title":"Error","slug":"error"},{"level":3,"title":"Swap Backup File","slug":"swap-backup-file"},{"level":3,"title":"Error Encoding","slug":"error-encoding"}],"relativePath":"programming/tools/vim/vimBasicNotes.md","lastUpdated":1627196177000}',s={},n=l('',196);s.render=function(l,a,s,d,r,i){return t(),e("div",null,[n])};export default s;export{a as __pageData}; diff --git a/assets/stack_frame.daa8403a.png b/assets/stack_frame.daa8403a.png new file mode 100644 index 00000000000..21bdc7e8bd1 Binary files /dev/null and b/assets/stack_frame.daa8403a.png differ diff --git a/assets/style.c5400c29.css b/assets/style.c5400c29.css new file mode 100644 index 00000000000..7bdabf19e5a --- /dev/null +++ b/assets/style.c5400c29.css @@ -0,0 +1 @@ +:root{--c-white:#ffffff;--c-white-dark:#f8f8f8;--c-black:#000000;--c-divider-light:rgba(60, 60, 67, 0.12);--c-divider-dark:rgba(84, 84, 88, 0.48);--c-text-light-1:#2c3e50;--c-text-light-2:#476582;--c-text-light-3:#90a4b7;--c-brand:#3eaf7c;--c-brand-light:#4abf8a;--font-family-base:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen,Ubuntu,Cantarell,'Fira Sans','Droid Sans','Helvetica Neue',sans-serif;--font-family-mono:source-code-pro,Menlo,Monaco,Consolas,'Courier New',monospace;--z-index-navbar:10;--z-index-sidebar:6;--shadow-1:0 1px 2px rgba(0, 0, 0, 0.04),0 1px 2px rgba(0, 0, 0, 0.06);--shadow-2:0 3px 12px rgba(0, 0, 0, 0.07),0 1px 4px rgba(0, 0, 0, 0.07);--shadow-3:0 12px 32px rgba(0, 0, 0, 0.1),0 2px 6px rgba(0, 0, 0, 0.08);--shadow-4:0 14px 44px rgba(0, 0, 0, 0.12),0 3px 9px rgba(0, 0, 0, 0.12);--shadow-5:0 18px 56px rgba(0, 0, 0, 0.16),0 4px 12px rgba(0, 0, 0, 0.16);--header-height:3.6rem}:root{--c-divider:var(--c-divider-light);--c-text:var(--c-text-light-1);--c-text-light:var(--c-text-light-2);--c-text-lighter:var(--c-text-light-3);--c-bg:var(--c-white);--c-bg-accent:var(--c-white-dark);--code-line-height:24px;--code-font-family:var(--font-family-mono);--code-font-size:14px;--code-inline-bg-color:rgba(27, 31, 35, 0.05);--code-bg-color:#282c34}*,::after,::before{box-sizing:border-box}html{line-height:1.4;font-size:16px;-webkit-text-size-adjust:100%}body{margin:0;width:100%;min-width:320px;min-height:100vh;line-height:1.4;font-family:var(--font-family-base);font-size:16px;font-weight:400;color:var(--c-text);background-color:var(--c-bg);direction:ltr;font-synthesis:none;text-rendering:optimizeLegibility;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}main{display:block}h1,h2,h3,h4,h5,h6{margin:0;line-height:1.25}b,h1,h2,h3,h4,h5,h6,strong{font-weight:600}h1:focus .header-anchor,h1:hover .header-anchor,h2:focus .header-anchor,h2:hover .header-anchor,h3:focus .header-anchor,h3:hover .header-anchor,h4:focus .header-anchor,h4:hover .header-anchor,h5:focus .header-anchor,h5:hover .header-anchor,h6:focus .header-anchor,h6:hover .header-anchor{opacity:1}h1{margin-top:1.5rem;font-size:1.9rem}@media screen and (min-width:420px){h1{font-size:2.2rem}}h2{margin-top:2.25rem;margin-bottom:1.25rem;border-bottom:1px solid var(--c-divider);padding-bottom:.3rem;line-height:1.25;font-size:1.65rem}h2+h3{margin-top:1.5rem}h3{margin-top:2rem;font-size:1.35rem}h4{font-size:1.15rem}ol,p,ul{margin:1rem 0;line-height:1.7}[role=button],a,area,button,input,label,select,summary,textarea{touch-action:manipulation}a{text-decoration:none;color:var(--c-brand)}a:hover{text-decoration:underline}a.header-anchor{float:left;margin-top:.125em;margin-left:-.87em;padding-right:.23em;font-size:.85em;opacity:0}a.header-anchor:focus,a.header-anchor:hover{text-decoration:none}figure{margin:0}img{max-width:100%}ol,ul{padding-left:1.25em}li>ol,li>ul{margin:0}table{display:block;border-collapse:collapse;margin:1rem 0;overflow-x:auto}tr{border-top:1px solid #dfe2e5}tr:nth-child(2n){background-color:#f6f8fa}td,th{border:1px solid #dfe2e5;padding:.6em 1em}blockquote{margin:1rem 0;border-left:.2rem solid #dfe2e5;padding:.25rem 0 .25rem 1rem;font-size:1rem;color:#999}blockquote>p{margin:0}form{margin:0}.theme.sidebar-open .sidebar-mask{display:block}.theme.no-navbar>h1,.theme.no-navbar>h2,.theme.no-navbar>h3,.theme.no-navbar>h4,.theme.no-navbar>h5,.theme.no-navbar>h6{margin-top:1.5rem;padding-top:0}.theme.no-navbar aside{top:0}@media screen and (min-width:720px){.theme.no-sidebar aside{display:none}.theme.no-sidebar main{margin-left:0}}.sidebar-mask{position:fixed;z-index:2;display:none;width:100vw;height:100vh}code{margin:0;border-radius:3px;padding:.25rem .5rem;font-family:var(--code-font-family);font-size:.85em;color:var(--c-text-light);background-color:var(--code-inline-bg-color)}code .token.deleted{color:#ec5975}code .token.inserted{color:var(--c-brand)}div[class*=language-]{position:relative;margin:1rem -1.5rem;background-color:var(--code-bg-color);overflow-x:auto}li>div[class*=language-]{border-radius:6px 0 0 6px;margin:1rem -1.5rem 1rem -1.25rem}@media (min-width:420px){div[class*=language-]{margin:1rem 0;border-radius:6px}li>div[class*=language-]{margin:1rem 0 1rem 0;border-radius:6px}}[class*=language-] code,[class*=language-] pre{text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}[class*=language-] pre{position:relative;z-index:1;margin:0;padding:1.25rem 1.5rem;background:0 0;overflow-x:auto}[class*=language-] code{padding:0;line-height:var(--code-line-height);font-size:var(--code-font-size);color:#eee}.highlight-lines{position:absolute;top:0;bottom:0;left:0;padding:1.25rem 0;width:100%;line-height:var(--code-line-height);font-family:var(--code-font-family);font-size:var(--code-font-size);user-select:none;overflow:hidden}.highlight-lines .highlighted{background-color:rgba(0,0,0,.66)}div[class*=language-].line-numbers-mode{padding-left:3.5rem}.line-numbers-wrapper{position:absolute;top:0;bottom:0;left:0;z-index:3;border-right:1px solid rgba(0,0,0,.5);padding:1.25rem 0;width:3.5rem;text-align:center;line-height:var(--code-line-height);font-family:var(--code-font-family);font-size:var(--code-font-size);color:#888}div[class*=language-]:before{position:absolute;top:.6em;right:1em;z-index:2;font-size:.8rem;color:#888}div[class~=language-html]:before,div[class~=language-markup]:before{content:'html'}div[class~=language-markdown]:before,div[class~=language-md]:before{content:'md'}div[class~=language-css]:before{content:'css'}div[class~=language-sass]:before{content:'sass'}div[class~=language-scss]:before{content:'scss'}div[class~=language-less]:before{content:'less'}div[class~=language-stylus]:before{content:'styl'}div[class~=language-javascript]:before,div[class~=language-js]:before{content:'js'}div[class~=language-ts]:before,div[class~=language-typescript]:before{content:'ts'}div[class~=language-json]:before{content:'json'}div[class~=language-rb]:before,div[class~=language-ruby]:before{content:'rb'}div[class~=language-py]:before,div[class~=language-python]:before{content:'py'}div[class~=language-bash]:before,div[class~=language-sh]:before{content:'sh'}div[class~=language-php]:before{content:'php'}div[class~=language-go]:before{content:'go'}div[class~=language-rust]:before{content:'rust'}div[class~=language-java]:before{content:'java'}div[class~=language-c]:before{content:'c'}div[class~=language-yaml]:before{content:'yaml'}div[class~=language-dockerfile]:before{content:'dockerfile'}div[class~=language-vue]:before{content:'vue'}.token.block-comment,.token.cdata,.token.comment,.token.doctype,.token.prolog{color:#999}.token.punctuation{color:#ccc}.token.attr-name,.token.deleted,.token.namespace,.token.tag{color:#e2777a}.token.function-name{color:#6196cc}.token.boolean,.token.function,.token.number{color:#f08d49}.token.class-name,.token.constant,.token.property,.token.symbol{color:#f8c555}.token.atrule,.token.builtin,.token.important,.token.keyword,.token.selector{color:#cc99cd}.token.attr-value,.token.char,.token.regex,.token.string,.token.variable{color:#7ec699}.token.entity,.token.operator,.token.url{color:#67cdcc}.token.bold,.token.important{font-weight:700}.token.italic{font-style:italic}.token.entity{cursor:help}.token.inserted{color:green}.custom-block.danger,.custom-block.tip,.custom-block.warning{margin:1rem 0;border-left:.5rem solid;padding:.1rem 1.5rem;overflow-x:auto}.custom-block.tip{background-color:#f3f5f7;border-color:var(--c-brand)}.custom-block.warning{border-color:#e7c000;color:#6b5900;background-color:rgba(255,229,100,.3)}.custom-block.warning .custom-block-title{color:#b29400}.custom-block.warning a{color:var(--c-text)}.custom-block.danger{border-color:#c00;color:#4d0000;background-color:#ffe6e6}.custom-block.danger .custom-block-title{color:#900}.custom-block.danger a{color:var(--c-text)}.custom-block.details{position:relative;display:block;border-radius:2px;margin:1.6em 0;padding:1.6em;background-color:#eee}.custom-block.details h4{margin-top:0}.custom-block.details figure:last-child,.custom-block.details p:last-child{margin-bottom:0;padding-bottom:0}.custom-block.details summary{outline:0;cursor:pointer}.custom-block-title{margin-bottom:-.4rem;font-weight:600}.sidebar-links{margin:0;padding:0;list-style:none}.sidebar-link-item{display:block;margin:0;border-left:.25rem solid transparent;color:var(--c-text)}a.sidebar-link-item:hover{text-decoration:none;color:var(--c-brand)}a.sidebar-link-item.active{color:var(--c-brand)}.sidebar>.sidebar-links{padding:.75rem 0 5rem}@media (min-width:720px){.sidebar>.sidebar-links{padding:1.5rem 0}}.sidebar>.sidebar-links>.sidebar-link+.sidebar-link{padding-top:.5rem}@media (min-width:720px){.sidebar>.sidebar-links>.sidebar-link+.sidebar-link{padding-top:1.25rem}}.sidebar>.sidebar-links>.sidebar-link>.sidebar-link-item{padding:.35rem 1.5rem .35rem 1.25rem;font-size:1.1rem;font-weight:700}.sidebar>.sidebar-links>.sidebar-link>a.sidebar-link-item.active{border-left-color:var(--c-brand);font-weight:600}.sidebar>.sidebar-links>.sidebar-link>.sidebar-links>.sidebar-link>.sidebar-link-item{display:block;padding:.35rem 1.5rem .35rem 2rem;line-height:1.4;font-size:1rem;font-weight:400}.sidebar>.sidebar-links>.sidebar-link>.sidebar-links>.sidebar-link>a.sidebar-link-item.active{border-left-color:var(--c-brand);font-weight:600}.sidebar>.sidebar-links>.sidebar-link>.sidebar-links>.sidebar-link>.sidebar-links>.sidebar-link>.sidebar-link-item{display:block;padding:.3rem 1.5rem .3rem 3rem;line-height:1.4;font-size:.9rem;font-weight:400}.sidebar>.sidebar-links>.sidebar-link>.sidebar-links>.sidebar-link>.sidebar-links>.sidebar-link>.sidebar-links>.sidebar-link>.sidebar-link-item{display:block;padding:.3rem 1.5rem .3rem 4rem;line-height:1.4;font-size:.9rem;font-weight:400}.debug[data-v-edc1134c]{box-sizing:border-box;position:fixed;right:8px;bottom:8px;z-index:9999;border-radius:4px;width:74px;height:32px;color:#eee;overflow:hidden;cursor:pointer;background-color:rgba(0,0,0,.85);transition:all .15s ease}.debug[data-v-edc1134c]:hover{background-color:rgba(0,0,0,.75)}.debug.open[data-v-edc1134c]{right:0;bottom:0;width:100%;height:100%;margin-top:0;border-radius:0;padding:0 0;overflow:scroll}@media (min-width:512px){.debug.open[data-v-edc1134c]{width:512px}}.debug.open[data-v-edc1134c]:hover{background-color:rgba(0,0,0,.85)}.title[data-v-edc1134c]{margin:0;padding:6px 16px 6px;line-height:20px;font-size:13px}.block[data-v-edc1134c]{margin:2px 0 0;border-top:1px solid rgba(255,255,255,.16);padding:8px 16px;font-family:Hack,monospace;font-size:13px}.block+.block[data-v-edc1134c]{margin-top:8px}.nav-bar-title[data-v-f07481ba]{font-size:1.3rem;font-weight:600;color:var(--c-text)}.nav-bar-title[data-v-f07481ba]:hover{text-decoration:none}.logo[data-v-f07481ba]{margin-right:.75rem;height:1.3rem;vertical-align:bottom}.icon.outbound{position:relative;top:-1px;display:inline-block;vertical-align:middle;color:var(--c-text-lighter)}.item[data-v-2c9af77b]{display:block;padding:0 1.5rem;line-height:36px;font-size:1rem;font-weight:600;color:var(--c-text);white-space:nowrap}.item.active[data-v-2c9af77b],.item[data-v-2c9af77b]:hover{text-decoration:none;color:var(--c-brand)}.item.external[data-v-2c9af77b]:hover{border-bottom-color:transparent;color:var(--c-text)}@media (min-width:720px){.item[data-v-2c9af77b]{border-bottom:2px solid transparent;padding:0;line-height:24px;font-size:.9rem;font-weight:500}.item.active[data-v-2c9af77b],.item[data-v-2c9af77b]:hover{border-bottom-color:var(--c-brand);color:var(--c-text)}}.item[data-v-22d28cf1]{display:block;padding:0 1.5rem 0 2.5rem;line-height:32px;font-size:.9rem;font-weight:500;color:var(--c-text);white-space:nowrap}@media (min-width:720px){.item[data-v-22d28cf1]{padding:0 24px 0 12px;line-height:32px;font-size:.85rem;font-weight:500;color:var(--c-text);white-space:nowrap}.item.active .arrow[data-v-22d28cf1]{opacity:1}}.item.active[data-v-22d28cf1],.item[data-v-22d28cf1]:hover{text-decoration:none;color:var(--c-brand)}.item.external[data-v-22d28cf1]:hover{border-bottom-color:transparent;color:var(--c-text)}@media (min-width:720px){.arrow[data-v-22d28cf1]{display:inline-block;margin-right:8px;border-top:6px solid #ccc;border-right:4px solid transparent;border-bottom:0;border-left:4px solid transparent;vertical-align:middle;opacity:0;transform:translateY(-2px) rotate(-90deg)}}.nav-dropdown-link[data-v-2a5d8d7e]{position:relative;height:36px;overflow:hidden;cursor:pointer}@media (min-width:720px){.nav-dropdown-link[data-v-2a5d8d7e]{height:auto;overflow:visible}.nav-dropdown-link:hover .dialog[data-v-2a5d8d7e]{display:block}}.nav-dropdown-link.open[data-v-2a5d8d7e]{height:auto}.button[data-v-2a5d8d7e]{display:block;border:0;padding:0 1.5rem;width:100%;text-align:left;line-height:36px;font-family:var(--font-family-base);font-size:1rem;font-weight:600;color:var(--c-text);white-space:nowrap;background-color:transparent;cursor:pointer}.button[data-v-2a5d8d7e]:focus{outline:0}@media (min-width:720px){.button[data-v-2a5d8d7e]{border-bottom:2px solid transparent;padding:0;line-height:24px;font-size:.9rem;font-weight:500}}.button-arrow[data-v-2a5d8d7e]{display:inline-block;margin-top:-1px;margin-left:8px;border-top:6px solid #ccc;border-right:4px solid transparent;border-bottom:0;border-left:4px solid transparent;vertical-align:middle}.button-arrow.right[data-v-2a5d8d7e]{transform:rotate(-90deg)}@media (min-width:720px){.button-arrow.right[data-v-2a5d8d7e]{transform:rotate(0)}}.dialog[data-v-2a5d8d7e]{margin:0;padding:0;list-style:none}@media (min-width:720px){.dialog[data-v-2a5d8d7e]{display:none;position:absolute;top:26px;right:-8px;border-radius:6px;padding:12px 0;min-width:128px;background-color:var(--c-bg);box-shadow:var(--shadow-3)}}.nav-links[data-v-74485887]{padding:.75rem 0;border-bottom:1px solid var(--c-divider)}@media (min-width:720px){.nav-links[data-v-74485887]{display:flex;padding:6px 0 0;align-items:center;border-bottom:0}.item+.item[data-v-74485887]{padding-left:24px}}.sidebar-button{position:absolute;top:.6rem;left:1rem;display:none;padding:.6rem;cursor:pointer}.sidebar-button .icon{display:block;width:1.25rem;height:1.25rem}@media screen and (max-width:719px){.sidebar-button{display:block}}.nav-bar[data-v-dd1cb1ee]{position:fixed;top:0;right:0;left:0;z-index:var(--z-index-navbar);display:flex;justify-content:space-between;align-items:center;border-bottom:1px solid var(--c-divider);padding:.7rem 1.5rem .7rem 4rem;height:var(--header-height);background-color:var(--c-bg)}@media (min-width:720px){.nav-bar[data-v-dd1cb1ee]{padding:.7rem 1.5rem}}.flex-grow[data-v-dd1cb1ee]{flex-grow:1}.nav[data-v-dd1cb1ee]{display:none}@media (min-width:720px){.nav[data-v-dd1cb1ee]{display:block}}.sidebar[data-v-74188dea]{position:fixed;top:var(--header-height);bottom:0;left:0;z-index:var(--z-index-sidebar);border-right:1px solid var(--c-divider);width:16.4rem;background-color:var(--c-bg);overflow-y:auto;transform:translateX(-100%);transition:transform .25s ease}@media (min-width:720px){.sidebar[data-v-74188dea]{transform:translateX(0)}}@media (min-width:960px){.sidebar[data-v-74188dea]{width:20rem}}.sidebar.open[data-v-74188dea]{transform:translateX(0)}.nav[data-v-74188dea]{display:block}@media (min-width:720px){.nav[data-v-74188dea]{display:none}}.link[data-v-93821cda]{display:inline-block;font-size:1rem;font-weight:500;color:var(--c-text-light)}.link[data-v-93821cda]:hover{text-decoration:none;color:var(--c-brand)}.icon[data-v-93821cda]{margin-left:4px}.last-updated[data-v-694e09d6]{display:inline-block;margin:0;line-height:1.4;font-size:.9rem;color:var(--c-text-light)}@media (min-width:960px){.last-updated[data-v-694e09d6]{font-size:1rem}}.prefix[data-v-694e09d6]{display:inline-block;font-weight:500}.datetime[data-v-694e09d6]{display:inline-block;margin-left:6px;font-weight:400}.page-footer[data-v-3538c8db]{padding-top:1rem;padding-bottom:1rem;overflow:auto}@media (min-width:960px){.page-footer[data-v-3538c8db]{display:flex;justify-content:space-between;align-items:center}}.updated[data-v-3538c8db]{padding-top:4px}@media (min-width:960px){.updated[data-v-3538c8db]{padding-top:0}}.next-and-prev-link[data-v-5ae8aee1]{padding-top:1rem}.container[data-v-5ae8aee1]{display:flex;justify-content:space-between;border-top:1px solid var(--c-divider);padding-top:1rem}.next[data-v-5ae8aee1],.prev[data-v-5ae8aee1]{display:flex;flex-shrink:0;width:50%}.prev[data-v-5ae8aee1]{justify-content:flex-start;padding-right:12px}.next[data-v-5ae8aee1]{justify-content:flex-end;padding-left:12px}.link[data-v-5ae8aee1]{display:inline-flex;align-items:center;max-width:100%;font-size:1rem;font-weight:500}.text[data-v-5ae8aee1]{display:block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.icon[data-v-5ae8aee1]{display:block;flex-shrink:0;width:16px;height:16px;fill:var(--c-text);transform:translateY(1px)}.icon-prev[data-v-5ae8aee1]{margin-right:8px}.icon-next[data-v-5ae8aee1]{margin-left:8px}.page[data-v-d004a6fc]{padding-top:var(--header-height)}@media (min-width:720px){.page[data-v-d004a6fc]{margin-left:16.4rem}}@media (min-width:960px){.page[data-v-d004a6fc]{margin-left:20rem}}.container[data-v-d004a6fc]{margin:0 auto;padding:0 1.5rem 4rem;max-width:48rem}.content[data-v-d004a6fc]{padding-bottom:1.5rem}@media (max-width:420px){.content[data-v-d004a6fc]{clear:both}}#ads-container{margin:0 auto}@media (min-width:420px){#ads-container{position:relative;right:0;float:right;margin:-8px -8px 24px 24px;width:146px}}@media (max-width:420px){#ads-container{height:105px;margin:1.75rem 0}}@media (min-width:1400px){#ads-container{position:fixed;right:8px;bottom:8px}}.home-hero[data-v-1154499e]{margin:2.5rem 0 2.75rem;padding:0 1.5rem;text-align:center}@media (min-width:420px){.home-hero[data-v-1154499e]{margin:3.5rem 0}}@media (min-width:720px){.home-hero[data-v-1154499e]{margin:4rem 0 4.25rem}}.figure[data-v-1154499e]{padding:0 1.5rem}.image[data-v-1154499e]{display:block;margin:0 auto;width:auto;max-width:100%;max-height:280px}.title[data-v-1154499e]{margin-top:1.5rem;font-size:2rem}@media (min-width:420px){.title[data-v-1154499e]{font-size:3rem}}@media (min-width:720px){.title[data-v-1154499e]{margin-top:2rem}}.description[data-v-1154499e]{margin:0;margin-top:.25rem;line-height:1.3;font-size:1.2rem;color:var(--c-text-light)}@media (min-width:420px){.description[data-v-1154499e]{line-height:1.2;font-size:1.6rem}}.action[data-v-1154499e]{margin-top:1.5rem;display:inline-block}.action.alt[data-v-1154499e]{margin-left:1.5rem}@media (min-width:420px){.action[data-v-1154499e]{margin-top:2rem;display:inline-block}}.action[data-v-1154499e] .item{display:inline-block;border-radius:6px;padding:0 20px;line-height:44px;font-size:1rem;font-weight:500;color:var(--c-bg);background-color:var(--c-brand);border:2px solid var(--c-brand);transition:background-color .1s ease}.action.alt[data-v-1154499e] .item{background-color:var(--c-bg);color:var(--c-brand)}.action[data-v-1154499e] .item:hover{text-decoration:none;color:var(--c-bg);background-color:var(--c-brand-light)}@media (min-width:420px){.action[data-v-1154499e] .item{padding:0 24px;line-height:52px;font-size:1.2rem;font-weight:500}}.home-features[data-v-58405b12]{margin:0 auto;padding:2.5rem 0 2.75rem;max-width:960px}.home-hero+.home-features[data-v-58405b12]{padding-top:0}@media (min-width:420px){.home-features[data-v-58405b12]{padding:3.25rem 0 3.5rem}.home-hero+.home-features[data-v-58405b12]{padding-top:0}}@media (min-width:720px){.home-features[data-v-58405b12]{padding-right:1.5rem;padding-left:1.5rem}}.wrapper[data-v-58405b12]{padding:0 1.5rem}.home-hero+.home-features .wrapper[data-v-58405b12]{border-top:1px solid var(--c-divider);padding-top:2.5rem}@media (min-width:420px){.home-hero+.home-features .wrapper[data-v-58405b12]{padding-top:3.25rem}}@media (min-width:720px){.wrapper[data-v-58405b12]{padding-right:0;padding-left:0}}.container[data-v-58405b12]{margin:0 auto;max-width:392px}@media (min-width:720px){.container[data-v-58405b12]{max-width:960px}}.features[data-v-58405b12]{display:flex;flex-wrap:wrap;margin:-20px -24px}.feature[data-v-58405b12]{flex-shrink:0;padding:20px 24px;width:100%}@media (min-width:720px){.feature[data-v-58405b12]{width:calc(100% / 3)}}.title[data-v-58405b12]{margin:0;border-bottom:0;line-height:1.4;font-size:1.25rem;font-weight:500}@media (min-width:420px){.title[data-v-58405b12]{font-size:1.4rem}}.details[data-v-58405b12]{margin:0;line-height:1.6;font-size:1rem;color:var(--c-text-light)}.title+.details[data-v-58405b12]{padding-top:.25rem}.footer[data-v-d7c9d38c]{margin:0 auto;max-width:960px}@media (min-width:720px){.footer[data-v-d7c9d38c]{padding:0 1.5rem}}.container[data-v-d7c9d38c]{padding:2rem 1.5rem 2.25rem}.home-content+.footer .container[data-v-d7c9d38c],.home-features+.footer .container[data-v-d7c9d38c],.home-hero+.footer .container[data-v-d7c9d38c]{border-top:1px solid var(--c-divider)}@media (min-width:420px){.container[data-v-d7c9d38c]{padding:3rem 1.5rem 3.25rem}}.text[data-v-d7c9d38c]{margin:0;text-align:center;line-height:1.4;font-size:.9rem;color:var(--c-text-light)}.home[data-v-46076ded]{padding-top:var(--header-height)}.home-content[data-v-46076ded]{max-width:960px;margin:0 auto;padding:0 1.5rem}@media (max-width:720px){.home-content[data-v-46076ded]{max-width:392px;padding:0}} \ No newline at end of file diff --git a/assets/web_angular_angularBasicNotes.md.85fd00dc.js b/assets/web_angular_angularBasicNotes.md.85fd00dc.js new file mode 100644 index 00000000000..4e1287cf817 --- /dev/null +++ b/assets/web_angular_angularBasicNotes.md.85fd00dc.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const e='{"title":"Angular Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic","slug":"basic"},{"level":3,"title":"CLI","slug":"cli"},{"level":2,"title":"Module","slug":"module"},{"level":2,"title":"Component","slug":"component"},{"level":3,"title":"Props","slug":"props"},{"level":3,"title":"Event","slug":"event"},{"level":3,"title":"Attributes","slug":"attributes"},{"level":3,"title":"Reference","slug":"reference"},{"level":3,"title":"Pipe","slug":"pipe"},{"level":2,"title":"Service","slug":"service"},{"level":3,"title":"Injection Provider","slug":"injection-provider"},{"level":2,"title":"RxJS","slug":"rxjs"},{"level":3,"title":"Basis","slug":"basis"},{"level":3,"title":"RxJS Pipe Helper","slug":"rxjs-pipe-helper"},{"level":3,"title":"Operator","slug":"operator"},{"level":2,"title":"Router","slug":"router"},{"level":2,"title":"Form","slug":"form"},{"level":2,"title":"Event Binding","slug":"event-binding"},{"level":2,"title":"Directives","slug":"directives"},{"level":3,"title":"Structural Directives","slug":"structural-directives"},{"level":3,"title":"Attribute Directives","slug":"attribute-directives"},{"level":2,"title":"Styles","slug":"styles"},{"level":3,"title":"CSS Class Binding","slug":"css-class-binding"}],"relativePath":"web/angular/angularBasicNotes.md","lastUpdated":1627196035000}',p={},t=a('

Angular Basic Notes

Basic

CLI

npm install -g @angular/cli\n
1
ng new my-app\nng add <package-name>\nng serve --open\nng lint\nng test\nng e2e\n
1
2
3
4
5
6

production build: suffix / is required

ng build --prod true --outputPath ./build --baseHref https://sabertazimi.github.io/hust-web/angular/learn/\n
1
ng generate --help\n
1

Module

Angular 需要知道如何把应用程序的各个部分组合到一起, 以及该应用需要哪些其它文件和库, 这些信息被称为元数据(metadata. 有些元数据位于@Component装饰器中, 你会把它加到组件类上. 另一些关键性的元数据位于@NgModule装饰器中.

NgModule 为其中的组件提供了一个编译上下文环境. 根模块总会有一个根组件, 并在引导期间创建它. 任何模块都能包含任意数量的其它组件, 这些组件可以通过路由器加载, 也可以通过模板创建. 那些属于这个 NgModule 的组件会共享同一个编译上下文环境.

Component

Props

import { Input } from '@angular/core';\n\n... {\n  @Input() hero: Hero;\n}\n
1
2
3
4
5

private props

import { HeroService } from '../hero.service';\n\nconstructor(private heroService: HeroService) { }\n
1
2
3

Event

parent

import { Component, OnInit } from '@angular/core';\n\n@Component({\n  selector: 'app-root',\n  template: `<app-child (valueChange)="displayCounter($event)"></app-child>`,\n})\nexport class AppComponent implements OnInit {\n  ngOnInit() {}\n\n  displayCounter(count) {\n    console.log(count);\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13

child

import { Component, Input, EventEmitter, Output } from '@angular/core';\n\n@Component({\n  selector: 'app-child',\n  template: `<button class="btn btn-primary" (click)="handleClick()">\n    Click me\n  </button>`,\n})\nexport class AppChildComponent {\n  @Output() valueChange: EventEmitter<number> = new EventEmitter();\n  counter = 0;\n\n  handleClick() {\n    this.counter += 1;\n    this.valueChange.emit(this.counter);\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Attributes

Angular 只会绑定到组件的公共属性

import { MessageService } from '../message.service';\n\n... {\n  constructor(public messageService: MessageService) {}\n}\n
1
2
3
4
5
<div *ngIf="messageService.messages.length">\n  <h2>Messages</h2>\n  <button class="clear" (click)="messageService.clear()">Clear</button>\n  <div *ngFor="let message of messageService.messages">{{message}}</div>\n</div>\n
1
2
3
4
5

Reference

# refer to DOM

<div>\n  <label\n    >Hero name:\n    <input #heroName />\n  </label>\n  <button (click)="add(heroName.value); heroName.value=''">add</button>\n</div>\n
1
2
3
4
5
6
7

Pipe

Pure Pipe

Angular 只有在它检测到输入值发生了纯变更时才会执行纯管道. 纯变更是指对原始类型值 (String、Number、Boolean、Symbol) 的更改, 或者对对象引用 (Date、Array、Function、Object) 的更改. Higher performance.

Impure Pipe

Angular 会在每个组件的变更检测周期中执行非纯管道. 非纯管道可能会被调用很多次, 和每个按键或每次鼠标移动一样频繁.

Async Pipe

<!-- heroes$ is a Observable -->\n<li *ngFor="let hero of heroes$ | async">{{hero.name}}</li>\n
1
2

Service

Injection Provider

@Injectable({\n  providedIn: 'root',\n})\n
1
2
3

RxJS

  • RxJS 管理所有输入的 input -> consumer/redux action 的调度过程
  • 条件变更之后的自动重新计算 (Reactive)
  • 同步与异步的统一
  • 获取和订阅的统一
  • 现在与未来的统一
  • 可组合的数据变更过程
  • 数据与视图的精确绑定
  • UI 变化很复杂时,用 component 归一化处理
  • state 变化很复杂时,用 action/state 归一化处理
  • data-input 很复杂时,用 RxJS/observable 归一化处理
  • reactive.how

Basis

可观察对象可以发送多个任意类型的值 —— 字面量、消息、事件. 无论这些值是同步发送的还是异步发送的, 接收这些值的 API 都是一样的. 由于准备(setup)和清场(teardown)的逻辑都是由可观察对象自己处理的, 因此应用代码只管订阅并消费这些值就可以了, 做完之后, 取消订阅. 无论这个流是击键流、HTTP 响应流还是定时器, 对这些值进行监听和停止监听的接口都是一样的.

Observer(观察者)是 Observable(可观察对象)推送数据的消费者. 在 RxJS 中, Observer 是一个由回调函数组成的对象, 键名分别为 next, error, complete, 以此接受 Observable 推送的不同类型的通知 (data input).

Subscription 是一个代表可以终止资源的对象, 表示一个 Observable 的执行过程. Subscription 有一个重要的方法: unsubscribe. 这个方法不需要传入参数, 调用后便会终止相应的资源. Observable 当有数据产生时才会推送给订阅者, 所以它可能会无限次向订阅者推送数据. 因此在 Angular 里面创建组件的时候务必要取消订阅操作, 以避免内存泄漏.

Subject 既是可观察对象的数据源, 本身也是 Observable. 可以像订阅任何 Observable 一样订阅 Subject. 还可以通过调用它的 next(value) 方法往 Observable 中推送一些值.

// Create simple observable that emits three values\nconst myObservable = of(1, 2, 3);\n\n// Create observer object\nconst myObserver = {\n  next: (x) => console.log('Observer got a next value: ' + x),\n  error: (err) => console.error('Observer got an error: ' + err),\n  complete: () => console.log('Observer got a complete notification'),\n};\n\n// Execute with the observer object\nconst subscription = myObservable.subscribe(myObserver);\n// Logs:\n// Observer got a next value: 1\n// Observer got a next value: 2\n// Observer got a next value: 3\n// Observer got a complete notification\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
... {\n  search(term: string): void {\n    this.searchTerms.next(term);\n  }\n\n  ngOnInit(): void {\n    this.heroes$ = this.searchTerms.pipe(\n      // wait 300ms after each keystroke before considering the term\n      debounceTime(300),\n\n      // ignore new term if same as previous term\n      distinctUntilChanged(),\n\n      // switch to new search observable each time the term changes\n      // return another Observable\n      switchMap((term: string) => this.heroService.searchHeroes(term)),\n    );\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

RxJS Pipe Helper

RxJS 提供了 pipe 辅助函数, 它存在于 Observable 上, 它缓解了操作符不在原型上所带来的问题.

import { take, map } from 'rxjs/operators';\nimport { of } from 'rxjs/observable/of';\n\nmap.call(take.call(of(1, 2, 3), 2), (val) => val + 2);\n\n// to\nimport { take, map } from 'rxjs/operators';\nimport { of } from 'rxjs/observable/of';\n\nof(1, 2, 3).pipe(\n  take(2),\n  map((val) => val + 2)\n);\n
1
2
3
4
5
6
7
8
9
10
11
12
13

Operator

Creation Operator

  • 单值: of, empty, never
  • 多值: from
  • 定时: interval, timer
  • 从事件创建: fromEvent/fromEventPattern
  • 从 Promise 创建: fromPromise
  • 自定义创建: create

Transformation Operator

  • map
  • mapTo
  • mergeMap/flatMap
  • pluck
  • reduce
  • scan
  • groupBy
  • switch
  • switchMap: 在每次发出时, 会取消前一个内部 Observable (所提供函数的 return value) 的订阅, 然后订阅一个新的 observable. 即当有新的输入时便不再关心之前请求的响应结果.

借助switchMap操作符, 每个有效的击键事件都会触发一次HttpClient.get()方法调用. 即使在每个请求之间都有至少 300ms 的间隔, 仍然可能会同时存在多个尚未返回的 HTTP 请求. switchMap()会记住原始的请求顺序, 只会返回最近一次 HTTP 方法调用的结果, 以前的那些请求都会被取消和舍弃.

Filter Operator

  • audit
  • auditTime
  • filter
  • skip
  • first
  • last
  • take
  • takeWhile
  • takeUntil
  • throttle
  • throttleTime
  • debounce
  • debounceTime
  • distinctUntilChanged: 只有当当前值与之前最后一个值 !== 时才将其发出
  • bufferTime
  • subscribeOn
  • ObserveOn

Combination Operator

  • concat: 保持原来的序列顺序连接两个数据流
  • merge: 合并序列
  • race: 预设条件为其中一个数据流完成
  • forkJoin: 预设条件为所有数据流都完成
  • zip: 取各来源数据流最后一个值合并为对象
  • combineLatest: 取各来源数据流最后一个值合并为数组

Multi-Cast Operator

  • MultiCast
  • Publish
  • Share

Error Handling Operator

  • throw
  • catch/catchError
  • retry
  • retryWhen
  • finally

Utils Operator

  • do/tap
  • delay
  • delayWhen
  • timeout
  • toPromise

Router

<router-outlet> 会告诉路由器要在哪里显示路由的视图.

import { NgModule } from '@angular/core';\nimport { Routes, RouterModule } from '@angular/router';\nimport { HeroesComponent } from './heroes/heroes.component';\n\nconst routes: Routes = [\n  {\n    path: 'heroes',\n    component: HeroesComponent,\n  },\n];\n\n@NgModule({\n  imports: [RouterModule.forRoot(routes)],\n  exports: [RouterModule],\n})\nexport class AppRoutingModule {}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Form

  • [(ngModel)]
  • (ngSubmit)

Event Binding

  • () template syntax for event binding
<li (click)="onSelect($event.target.name)"></li>\n<li (click)="onSelect(hero.name)"></li>\n
1
2

Directives

Structural Directives

For Directive

<li *ngFor="let hero of heroes">{{hero.name}}</li>\n
1

If Directive

<div *ngIf="selectedHero">Selected</div>\n
1

Attribute Directives

Class Directive

same to [ngStyle]

<div [ngClass]="currentClasses">\n  This div is initially saveable, unchanged, and special\n</div>\n
1
2
3
currentClasses: {};\n\nsetCurrentClasses() {\n  // CSS classes: added/removed per current state of component properties\n  this.currentClasses = {\n    'saveable': this.canSave,\n    'modified': !this.isUnchanged,\n    'special':  this.isSpecial\n  };\n}\n
1
2
3
4
5
6
7
8
9
10

Styles

CSS Class Binding

<!-- toggle the "special" class on/off with a property -->\n<div [class.special]="isSpecial">The class binding is special</div>\n\n<!-- binding to `class.special` trumps the class attribute -->\n<div class="special" [class.special]="!isSpecial">\n  This one is not so special\n</div>\n
1
2
3
4
5
6
7
',91);p.render=function(a,e,p,o,l,c){return n(),s("div",null,[t])};export default p;export{e as __pageData}; diff --git a/assets/web_angular_angularBasicNotes.md.85fd00dc.lean.js b/assets/web_angular_angularBasicNotes.md.85fd00dc.lean.js new file mode 100644 index 00000000000..8f90e58cac1 --- /dev/null +++ b/assets/web_angular_angularBasicNotes.md.85fd00dc.lean.js @@ -0,0 +1 @@ +import{o as n,c as s,a}from"./app.6dd2a1b7.js";const e='{"title":"Angular Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Basic","slug":"basic"},{"level":3,"title":"CLI","slug":"cli"},{"level":2,"title":"Module","slug":"module"},{"level":2,"title":"Component","slug":"component"},{"level":3,"title":"Props","slug":"props"},{"level":3,"title":"Event","slug":"event"},{"level":3,"title":"Attributes","slug":"attributes"},{"level":3,"title":"Reference","slug":"reference"},{"level":3,"title":"Pipe","slug":"pipe"},{"level":2,"title":"Service","slug":"service"},{"level":3,"title":"Injection Provider","slug":"injection-provider"},{"level":2,"title":"RxJS","slug":"rxjs"},{"level":3,"title":"Basis","slug":"basis"},{"level":3,"title":"RxJS Pipe Helper","slug":"rxjs-pipe-helper"},{"level":3,"title":"Operator","slug":"operator"},{"level":2,"title":"Router","slug":"router"},{"level":2,"title":"Form","slug":"form"},{"level":2,"title":"Event Binding","slug":"event-binding"},{"level":2,"title":"Directives","slug":"directives"},{"level":3,"title":"Structural Directives","slug":"structural-directives"},{"level":3,"title":"Attribute Directives","slug":"attribute-directives"},{"level":2,"title":"Styles","slug":"styles"},{"level":3,"title":"CSS Class Binding","slug":"css-class-binding"}],"relativePath":"web/angular/angularBasicNotes.md","lastUpdated":1627196035000}',p={},t=a('',91);p.render=function(a,e,p,o,l,c){return n(),s("div",null,[t])};export default p;export{e as __pageData}; diff --git a/assets/web_css_bootstrapBasicNotes.md.222d8e91.js b/assets/web_css_bootstrapBasicNotes.md.222d8e91.js new file mode 100644 index 00000000000..37e1b908f55 --- /dev/null +++ b/assets/web_css_bootstrapBasicNotes.md.222d8e91.js @@ -0,0 +1 @@ +import{o as n,c as a,a as s}from"./app.6dd2a1b7.js";const t='{"title":"Bootstrap Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Bootstrap Basis","slug":"bootstrap-basis"},{"level":2,"title":"Grid System","slug":"grid-system"},{"level":3,"title":"Basic","slug":"basic"},{"level":3,"title":"row","slug":"row"},{"level":2,"title":"Nav","slug":"nav"},{"level":3,"title":"NavBar","slug":"navbar"},{"level":3,"title":"BreadCrumb","slug":"breadcrumb"},{"level":3,"title":"Scroll Spy","slug":"scroll-spy"},{"level":2,"title":"Tabs and Pills","slug":"tabs-and-pills"},{"level":3,"title":"Tab Pane","slug":"tab-pane"},{"level":3,"title":"Tab Pane JS API","slug":"tab-pane-js-api"},{"level":3,"title":"Tab Pane JS Events","slug":"tab-pane-js-events"},{"level":2,"title":"Collapse","slug":"collapse"},{"level":3,"title":"Accordion","slug":"accordion"},{"level":2,"title":"Text","slug":"text"},{"level":2,"title":"Form","slug":"form"},{"level":3,"title":"Form Basic","slug":"form-basic"},{"level":3,"title":"CheckBox and Select","slug":"checkbox-and-select"},{"level":3,"title":"Form Alerts and Prompts","slug":"form-alerts-and-prompts"},{"level":2,"title":"Table","slug":"table"},{"level":3,"title":"Table Basic Class","slug":"table-basic-class"},{"level":2,"title":"Card","slug":"card"},{"level":3,"title":"Panel","slug":"panel"},{"level":3,"title":"Well","slug":"well"},{"level":2,"title":"Images","slug":"images"},{"level":3,"title":"Img Class","slug":"img-class"},{"level":3,"title":"Thumbnail","slug":"thumbnail"},{"level":3,"title":"Media","slug":"media"},{"level":3,"title":"Embed","slug":"embed"},{"level":2,"title":"Alerts and Prompts","slug":"alerts-and-prompts"},{"level":3,"title":"Labels","slug":"labels"},{"level":3,"title":"Badges","slug":"badges"},{"level":3,"title":"Alerts","slug":"alerts"},{"level":3,"title":"Progress Bars","slug":"progress-bars"},{"level":3,"title":"Tooltip","slug":"tooltip"},{"level":3,"title":"Popover","slug":"popover"},{"level":3,"title":"Modal","slug":"modal"},{"level":2,"title":"Button","slug":"button"},{"level":2,"title":"Jumbotron","slug":"jumbotron"},{"level":2,"title":"Icon-Fonts","slug":"icon-fonts"},{"level":2,"title":"Carousel","slug":"carousel"},{"level":2,"title":"Common Class","slug":"common-class"},{"level":3,"title":"color","slug":"color"},{"level":3,"title":"size","slug":"size"},{"level":3,"title":"state","slug":"state"},{"level":3,"title":"Bootstrap Animation","slug":"bootstrap-animation"},{"level":2,"title":"Bootstrap 4","slug":"bootstrap-4"},{"level":3,"title":"Bootstrap Card","slug":"bootstrap-card"},{"level":2,"title":"Custom Bootstrap Theme","slug":"custom-bootstrap-theme"},{"level":3,"title":"Webpack Setup","slug":"webpack-setup"},{"level":3,"title":"Bootstrap Reboot Tips","slug":"bootstrap-reboot-tips"},{"level":3,"title":"Useful Custom Functions","slug":"useful-custom-functions"},{"level":3,"title":"Custom Global Options","slug":"custom-global-options"},{"level":3,"title":"Custom Colors","slug":"custom-colors"},{"level":3,"title":"Custom Spacing","slug":"custom-spacing"},{"level":3,"title":"Custom Layout","slug":"custom-layout"},{"level":3,"title":"Custom Borders","slug":"custom-borders"},{"level":3,"title":"Custom Navbar and Navigation","slug":"custom-navbar-and-navigation"},{"level":3,"title":"Custom Dropdown","slug":"custom-dropdown"},{"level":3,"title":"Custom List Group","slug":"custom-list-group"},{"level":3,"title":"Custom Card","slug":"custom-card"},{"level":3,"title":"Custom Breadcrumb","slug":"custom-breadcrumb"},{"level":3,"title":"Custom Form","slug":"custom-form"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"web/css/bootstrapBasicNotes.md","lastUpdated":1628014655000}',p={},e=s('

Bootstrap Basic Notes

Bootstrap Basis

<UI API>\n  <container/content>\n  <header>\n  </header>\n  <body>\n  </body>\n  <footer>\n  </footer>\n</container/content>\n</UI API>\n
1
2
3
4
5
6
7
8
9
10

Grid System

Basic

  • 设置宽度: col-xs/sm/md/lg-num
  • 设置偏移量: col-xs/sm/md/lg-pull/push/offset-num

row

  • row
  • row-header/row-content/row-footer

Class

  • navbar navbar-default
  • navbar-inverse(color) navbar-fixed-top
  • navbar-form
  • nav-tabs nav-pills
  • navbar-left/right

Structure

navbar > container > nav/navbar-header/navbar-nav/navbar-form > li/a

<nav class="navbar navbar-default">\n  <div class="container-fluid">\n    <!-- Brand and toggle get grouped for better mobile display -->\n    <div class="navbar-header">\n      <button\n        type="button"\n        class="navbar-toggle collapsed"\n        data-toggle="collapse"\n        data-target="#bs-example-navbar-collapse-1"\n        aria-expanded="false"\n      >\n        <span class="sr-only">Toggle navigation</span>\n        <span class="icon-bar"></span>\n        <span class="icon-bar"></span>\n        <span class="icon-bar"></span>\n      </button>\n      <a class="navbar-brand" href="#">Brand</a>\n    </div>\n\n    <!-- Collect the nav links, forms, and other content for toggling -->\n    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">\n      <ul class="nav navbar-nav">\n        <li class="active">\n          <a href="#">Link <span class="sr-only">(current)</span></a>\n        </li>\n        <li><a href="#">Link</a></li>\n        <li class="dropdown">\n          <a\n            href="#"\n            class="dropdown-toggle"\n            data-toggle="dropdown"\n            role="button"\n            aria-haspopup="true"\n            aria-expanded="false"\n          >\n            Dropdown<span class="caret"></span>\n          </a>\n          <ul class="dropdown-menu">\n            <li><a href="#">Action</a></li>\n            <li><a href="#">Another action</a></li>\n            <li><a href="#">Something else here</a></li>\n            <li role="separator" class="divider"></li>\n            <li><a href="#">Separated link</a></li>\n            <li role="separator" class="divider"></li>\n            <li><a href="#">One more separated link</a></li>\n          </ul>\n        </li>\n      </ul>\n      <form class="navbar-form navbar-left" role="search">\n        <div class="form-group">\n          <input type="text" class="form-control" placeholder="Search" />\n        </div>\n        <button type="submit" class="btn btn-default">Submit</button>\n      </form>\n      <ul class="nav navbar-nav navbar-right">\n        <li><a href="#">Link</a></li>\n        <li class="dropdown">\n          <a\n            href="#"\n            class="dropdown-toggle"\n            data-toggle="dropdown"\n            role="button"\n            aria-haspopup="true"\n            aria-expanded="false"\n          >\n            Dropdown <span class="caret"></span>\n          </a>\n          <ul class="dropdown-menu">\n            <li><a href="#">Action</a></li>\n            <li><a href="#">Another action</a></li>\n            <li><a href="#">Something else here</a></li>\n            <li role="separator" class="divider"></li>\n            <li><a href="#">Separated link</a></li>\n          </ul>\n        </li>\n      </ul>\n    </div>\n    <!-- /.navbar-collapse -->\n  </div>\n  <!-- /.container-fluid -->\n</nav>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
body {\n  padding: 50px 0px 0px 0px;\n  z-index: 0;\n}\n\n.navbar-inverse {\n  background: #303f9f;\n}\n\n.navbar-inverse .navbar-nav > .active > a,\n.navbar-inverse .navbar-nav > .active > a:hover,\n.navbar-inverse .navbar-nav > .active > a:focus {\n  color: #fff;\n  background: #1a237e;\n}\n\n.navbar-inverse .navbar-nav > .open > a,\n.navbar-inverse .navbar-nav > .open > a:hover,\n.navbar-inverse .navbar-nav > .open > a:focus {\n  color: #fff;\n  background: #1a237e;\n}\n\n.navbar-inverse .navbar-nav .open .dropdown-menu > li > a,\n.navbar-inverse .navbar-nav .open .dropdown-menu {\n  background-color: #303f9f;\n  color: #eeeeee;\n}\n\n.navbar-inverse .navbar-nav .open .dropdown-menu > li > a:hover {\n  color: #000000;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

路径导航栏 - Home -> Library -> Data

<ol class="breadcrumb">\n  <li><a href="#">Home</a></li>\n  <li><a href="#">Library</a></li>\n  <li class="active">Data</li>\n</ol>\n
1
2
3
4
5

Scroll Spy

body {\n  position: relative;\n}\n\n.affix {\n  top: 100px;\n}\n
1
2
3
4
5
6
7
<body data-spy="scroll" data-target="#navbar-example" data-offset="200">\n  <nav class="hidden-xs col-sm-2" id="navbar-example">\n    <ul\n      class="nav nav-tabs"\n      role="tablist"\n      data-spy="affix"\n      data-offset-top="400"\n    >\n      <li><a href="#history">Our History</a></li>\n      <li><a href="#corporate">Corporate</a></li>\n      <li><a href="#facts">Facts</a></li>\n    </ul>\n  </nav>\n\n  <!-- 在某个地方 -->\n  <div id="history">...</div>\n  <div id="corporate">...</div>\n  <div id="facts">...</div>\n</body>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Tabs and Pills

  • nav-tabs/nav-pills
  • nav-justified/nav-stacked
  • link - active/disabled
<ul class="nav nav-tabs/nav-pills nav-justified/nav-stacked">\n  <li role="presentation" class="active"><a href="#">Home</a></li>\n  <li role="presentation" class="disabled"><a href="#">Messages</a></li>\n  <li role="presentation" class="dropdown">\n    <a\n      class="dropdown-toggle"\n      data-toggle="dropdown"\n      href="#"\n      role="button"\n      aria-haspopup="true"\n      aria-expanded="false"\n    >\n      Dropdown <span class="caret"></span>\n    </a>\n    <ul class="dropdown-menu">\n      ...\n    </ul>\n  </li>\n</ul>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Tab Pane

<div>\n  <!-- Nav tabs -->\n  <ul class="nav nav-tabs" role="tablist">\n    <li role="presentation" class="active">\n      <a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a>\n    </li>\n    <li role="presentation">\n      <a href="#profile" aria-controls="profile" role="tab" data-toggle="tab"\n        >Profile</a\n      >\n    </li>\n    <li role="presentation">\n      <a href="#messages" aria-controls="messages" role="tab" data-toggle="tab"\n        >Messages</a\n      >\n    </li>\n    <li role="presentation">\n      <a href="#settings" aria-controls="settings" role="tab" data-toggle="tab"\n        >Settings</a\n      >\n    </li>\n  </ul>\n\n  <!-- Tab panes -->\n  <div class="tab-content">\n    <div role="tabpanel" class="tab-pane fade in active" id="home">...</div>\n    <div role="tabpanel" class="tab-pane fade" id="profile">...</div>\n    <div role="tabpanel" class="tab-pane fade" id="messages">...</div>\n    <div role="tabpanel" class="tab-pane fade" id="settings">...</div>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Tab Pane JS API

$('#myTabs a').click(function (e) {\n  e.preventDefault();\n  $(this).tab('show');\n});\n\n$('#myTabs a[href="#profile"]').tab('show'); // Select tab by name\n$('#myTabs a:first').tab('show'); // Select first tab\n$('#myTabs a:last').tab('show'); // Select last tab\n$('#myTabs li:eq(2) a').tab('show'); // Select third tab (0-indexed)\n
1
2
3
4
5
6
7
8
9

Tab Pane JS Events

  • hide.bs.tab (on the current active tab)
  • show.bs.tab (on the to-be-shown tab)
  • hidden.bs.tab (on the previous active tab, the same one as for the hide.bs.tab event)
  • shown.bs.tab (on the newly-active just-shown tab, the same one as for the show.bs.tab event)
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {\n  e.target; // newly activated tab\n  e.relatedTarget; // previous active tab\n});\n
1
2
3
4

Collapse

href/data-target -> id

<a\n  class="btn btn-primary"\n  role="button"\n  data-toggle="collapse"\n  href="#collapseExample"\n  aria-expanded="false"\n  aria-controls="collapseExample"\n>\n  Link with href\n</a>\n\n<button\n  class="btn btn-primary"\n  type="button"\n  data-toggle="collapse"\n  data-target="#collapseExample"\n  aria-expanded="false"\n  aria-controls="collapseExample"\n>\n  Button with data-target\n</button>\n\n<div class="collapse in" id="collapseExample">\n  <div class="well">...</div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Accordion

panel-body/list-group

<div\n  class="panel-group"\n  id="accordion"\n  role="tablist"\n  aria-multiselectable="true"\n>\n  <div class="panel panel-default">\n    <div class="panel-heading" role="tab" id="headingOne">\n      <h4 class="panel-title">\n        <a\n          role="button"\n          data-toggle="collapse"\n          data-parent="#accordion"\n          href="#collapseOne"\n          aria-expanded="true"\n          aria-controls="collapseOne"\n        >\n          Collapsible Group Item #1\n        </a>\n      </h4>\n    </div>\n    <div\n      id="collapseOne"\n      class="panel-collapse collapse in"\n      role="tabpanel"\n      aria-labelledby="headingOne"\n    >\n      <div class="panel-body">\n        Anim cliche, high life terry richardson ad squid.\n      </div>\n    </div>\n  </div>\n  <div class="panel panel-default">\n    <div class="panel-heading" role="tab" id="headingTwo">\n      <h4 class="panel-title">\n        <a\n          class="collapsed"\n          role="button"\n          data-toggle="collapse"\n          data-parent="#accordion"\n          href="#collapseTwo"\n          aria-expanded="false"\n          aria-controls="collapseTwo"\n        >\n          Collapsible Group Item #2\n        </a>\n      </h4>\n    </div>\n    <div\n      id="collapseTwo"\n      class="panel-collapse collapse"\n      role="tabpanel"\n      aria-labelledby="headingTwo"\n    >\n      <div class="panel-body">\n        Anim cliche, high life terry richardson ad squid.\n      </div>\n    </div>\n  </div>\n  <div class="panel panel-default">\n    <div class="panel-heading" role="tab" id="headingThree">\n      <h4 class="panel-title">\n        <a\n          class="collapsed"\n          role="button"\n          data-toggle="collapse"\n          data-parent="#accordion"\n          href="#collapseThree"\n          aria-expanded="false"\n          aria-controls="collapseThree"\n        >\n          Collapsible Group Item #3\n        </a>\n      </h4>\n    </div>\n    <div\n      id="collapseThree"\n      class="panel-collapse collapse"\n      role="tabpanel"\n      aria-labelledby="headingThree"\n    >\n      <div class="panel-body">\n        Anim cliche, high life terry richardson ad squid.\n      </div>\n    </div>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

Text

  • text-center/left/right
  • text-primary/info/success/danger

Form

<!-- type,format,value -->\n<input id=”” name=””\ntype=”text/email/password/radiobutton/checkbox(checked)/submit/number/range/color/date/url”\nvalue=”(textfield/buttonText)” placeholder=”(默认占位文字)”\n\n<!-- validate input data -->\n\nrequired pattern=”[0-9a-zA-z]{5/13-16}”\n<!-- []:可用字符 {}:字符数目 -->\nmin=”(number)” max=”(number)” step=”(步长:指定输入数字必须倍数关系)”\n<!-- validate input data-->\n/>\n
1
2
3
4
5
6
7
8
9
10
11
12

Form Basic

  • <form class="form-horizontal" role="form">
  • <form class="form-inline" role="form">
<form class="form-horizontal" role="form">\n  <!-- input -->\n  <div class="form-group">\n    <label for="firstName" class="col-sm-2 control-label">First Name</label>\n    <div class="col-sm-10">\n      <input\n        type="text"\n        class="form-control"\n        id="firstName"\n        name="firstName"\n        placeholder="Enter First Name"\n      />\n    </div>\n  </div>\n  <!-- input -->\n  <div class="form-group">\n    <label for="lastName" class="col-sm-2 control-label">Last Name</label>\n    <div class="col-sm-10">\n      <input\n        type="text"\n        class="form-control"\n        id="lastName"\n        name="lastName"\n        placeholder="Enter Last Name"\n      />\n    </div>\n  </div>\n  <!-- input-addon -->\n  <div class="form-group">\n    <label for="telNum" class="col-xs-12 col-sm-2 control-label">\n      Contact Tel\n    </label>\n    <div class="col-xs-5 col-sm-4 col-md-3">\n      <div class="input-group">\n        <div class="input-group-addon">(</div>\n        <input\n          type="tel"\n          class="form-control"\n          id="areaCode"\n          name="areaCode"\n          placeholder="Area code"\n        />\n        <div class="input-group-addon">)</div>\n      </div>\n    </div>\n    <div class="col-xs-7 col-sm-6 col-md-7">\n      <input\n        type="tel"\n        class="form-control"\n        id="telNum"\n        name="telNum"\n        placeholder="Tel. number"\n      />\n    </div>\n  </div>\n  <!-- input -->\n  <div class="form-group">\n    <label for="emailID" class="col-sm-2 control-label">Email</label>\n    <div class="col-sm-10">\n      <input\n        type="email"\n        class="form-control"\n        id="emailID"\n        name="emailID"\n        placeholder="Email"\n      />\n    </div>\n  </div>\n  <!-- check box and select -->\n  <div class="form-group">\n    <div class="checkbox col-sm-5 col-sm-offset-2">\n      <label class="checkbox-inline">\n        <input type="checkbox" name="approve" value="" />\n        <strong>May we contact you?</strong>\n      </label>\n    </div>\n    <div class="col-sm-3 col-sm-offset-1">\n      <select class="form-control">\n        <option>Tel.</option>\n        <option>Email</option>\n      </select>\n    </div>\n  </div>\n  <!-- textarea -->\n  <div class="form-group">\n    <label for="feedback" class="col-sm-2 control-label">Your Feedback</label>\n    <div class="col-sm-10">\n      <textarea\n        class="form-control"\n        id="feedback"\n        name="feedback"\n        rows="12"\n      ></textarea>\n    </div>\n  </div>\n  <!-- submit button -->\n  <div class="form-group">\n    <div class="col-sm-offset-2 col-sm-10">\n      <button type="submit" class="btn btn-primary">Send Feedback</button>\n    </div>\n  </div>\n</form>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<form>\n  <div class="form-group">\n    <label for="exampleInputEmail1">Email address</label>\n    <input\n      type="email"\n      class="form-control"\n      id="exampleInputEmail1"\n      placeholder="Email"\n    />\n  </div>\n  <div class="form-group">\n    <label for="exampleInputPassword1">Password</label>\n    <input\n      type="password"\n      class="form-control"\n      id="exampleInputPassword1"\n      placeholder="Password"\n    />\n  </div>\n  <div class="form-group">\n    <label for="exampleInputFile">File input</label>\n    <input type="file" id="exampleInputFile" />\n    <p class="help-block">Example block-level help text here.</p>\n  </div>\n  <div class="checkbox">\n    <label> <input type="checkbox" /> Check me out </label>\n  </div>\n  <button type="submit" class="btn btn-default">Submit</button>\n</form>\n\n<div class="row">\n  <div class="col-lg-6">\n    <div class="input-group">\n      <div class="input-group-btn">\n        <button\n          type="button"\n          class="btn btn-default dropdown-toggle"\n          data-toggle="dropdown"\n          aria-haspopup="true"\n          aria-expanded="false"\n        >\n          Action <span class="caret"></span>\n        </button>\n        <ul class="dropdown-menu">\n          <li><a href="#">Action</a></li>\n          <li><a href="#">Another action</a></li>\n          <li><a href="#">Something else here</a></li>\n          <li role="separator" class="divider"></li>\n          <li><a href="#">Separated link</a></li>\n        </ul>\n      </div>\n      <!-- /btn-group -->\n      <input type="text" class="form-control" aria-label="..." />\n    </div>\n    <!-- /input-group -->\n  </div>\n  <!-- /.col-lg-6 -->\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

CheckBox and Select

<div class="form-group">\n  <div class="checkbox col-sm-5 col-sm-offset-2">\n    <label class="checkbox-inline">\n      <input type="checkbox" name="approve" value="" />\n      <strong>May we contact you?</strong>\n    </label>\n  </div>\n  <div class="col-sm-3 col-sm-offset-1">\n    <select class="form-control">\n      <option>Tel.</option>\n      <option>Email</option>\n    </select>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Form Alerts and Prompts

  • has-color
  • has-feedback
  • form-control-feedback(icon)
  • control-label/form-control/help-block
<div class="form-group has-success has-feedback">\n  <label class="control-label" for="inputSuccess2">Input with success</label>\n  <input\n    type="text"\n    class="form-control"\n    id="inputSuccess2"\n    aria-describedby="inputSuccess2Status"\n  />\n  <span\n    class="glyphicon glyphicon-ok form-control-feedback"\n    aria-hidden="true"\n  ></span>\n  <span id="inputSuccess2Status" class="sr-only">(success)</span>\n</div>\n<div class="form-group has-warning has-feedback">\n  <label class="control-label" for="inputWarning2">Input with warning</label>\n  <input\n    type="text"\n    class="form-control"\n    id="inputWarning2"\n    aria-describedby="inputWarning2Status"\n  />\n  <span\n    class="glyphicon glyphicon-warning-sign form-control-feedback"\n    aria-hidden="true"\n  ></span>\n  <span id="inputWarning2Status" class="sr-only">(warning)</span>\n</div>\n<div class="form-group has-error has-feedback">\n  <label class="control-label" for="inputError2">Input with error</label>\n  <input\n    type="text"\n    class="form-control"\n    id="inputError2"\n    aria-describedby="inputError2Status"\n  />\n  <span\n    class="glyphicon glyphicon-remove form-control-feedback"\n    aria-hidden="true"\n  ></span>\n  <span id="inputError2Status" class="sr-only">(error)</span>\n</div>\n<div class="form-group has-success has-feedback">\n  <label class="control-label" for="inputGroupSuccess1"\n    >Input group with success</label\n  >\n  <div class="input-group">\n    <span class="input-group-addon">@</span>\n    <input\n      type="text"\n      class="form-control"\n      id="inputGroupSuccess1"\n      aria-describedby="inputGroupSuccess1Status"\n    />\n  </div>\n  <span\n    class="glyphicon glyphicon-ok form-control-feedback"\n    aria-hidden="true"\n  ></span>\n  <span id="inputGroupSuccess1Status" class="sr-only">(success)</span>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

Table

Table Basic Class

  • table-striped
  • table-bordered
  • table-hover
  • table-condensed
  • table-responsive
<div class="table-responsive">\n  <table class="table table-striped">\n    <tr>\n      <td>&nbsp;</td>\n      <th>2013</th>\n      <th>2014</th>\n      <th>2015</th>\n    </tr>\n    <tr>\n      <th>Employees</th>\n      <td>15</td>\n      <td>30</td>\n      <td>40</td>\n    </tr>\n    <tr>\n      <th>Guests Served</th>\n      <td>15000</td>\n      <td>45000</td>\n      <td>100,000</td>\n    </tr>\n    <tr>\n      <th>Special Events</th>\n      <td>3</td>\n      <td>20</td>\n      <td>45</td>\n    </tr>\n    <tr>\n      <th>Annual Turnover</th>\n      <td>$251,325</td>\n      <td>$1,250,375</td>\n      <td>~$3,000,000</td>\n    </tr>\n  </table>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

Card

Panel

<div class="panel panel-primary">\n  <div class="panel-heading">\n    <h3 class="panel-title">Facts At a Glance</h3>\n  </div>\n  <div class="panel-body">\n    <dl class="dl-horizontal">\n      <dt>Started</dt>\n      <dd>3 Feb. 2013</dd>\n      <dt>Major Stake Holder</dt>\n      <dd>HK Fine Foods Inc.</dd>\n      <dt>Last Year's Turnover</dt>\n      <dd>$1,250,375</dd>\n      <dt>Employees</dt>\n      <dd>40</dd>\n    </dl>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Well

  • well/well-xs/well-sm
<div class="well">\n  <blockquote>\n    <p>paragraph</p>\n    <footer>\n      Yogi Berra,<cite title="Source Title"\n        >The Wit and Wisdom of Yogi Berra, 2014</cite\n      >\n    </footer>\n  </blockquote>\n</div>\n
1
2
3
4
5
6
7
8
9
10

Images

Img Class

  • img-responsive
  • img-rounded
  • img-circle
  • img-thumbnail

Thumbnail

<div class="row">\n  <div class="col-sm-6 col-md-4">\n    <div class="thumbnail">\n      <img class="img-thumbnail" src="..." alt="..." />\n      <div class="caption">\n        <h3>Thumbnail label</h3>\n        <p>...</p>\n        <p>\n          <a href="#" class="btn btn-primary btn-xs" role="button">Button</a>\n          <a href="#" class="btn btn-default btn-xs" role="button">Button</a>\n        </p>\n      </div>\n    </div>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Media

  • media
  • media-list(ul)
  • media-object
  • media-body
  • media-heading
  • media-left/right
  • media-top/middle/bottom
<ul class="media-list">\n  <li class="media">\n    <div class="media-left media-middle">\n      <a href="#">\n        <img class="media-object" src="..." alt="..." />\n      </a>\n    </div>\n    <div class="media-body">\n      <h4 class="media-heading">Media heading</h4>\n      ...\n    </div>\n  </li>\n</ul>\n
1
2
3
4
5
6
7
8
9
10
11
12
13

Embed

  • embed-responsive
  • embed-responsive-item
  • targets: <iframe>, <embed>, <video>, <object>
<!-- 16:9 aspect ratio -->\n<div class="embed-responsive embed-responsive-16by9">\n  <iframe class="embed-responsive-item" src="..."></iframe>\n</div>\n\n<!-- 4:3 aspect ratio -->\n<div class="embed-responsive embed-responsive-4by3">\n  <iframe class="embed-responsive-item" src="..."></iframe>\n</div>\n
1
2
3
4
5
6
7
8
9

Alerts and Prompts

Labels

  • target: span
  • label
  • label-color
  • label-size
<span class="label label-default label-xs">Default</span>\n<span class="label label-primary label-sm">Primary</span>\n<span class="label label-success label-md">Success</span>\n<span class="label label-info label-lg">Info</span>\n<span class="label label-warning">Warning</span>\n<span class="label label-danger">Danger</span>\n
1
2
3
4
5
6

Badges

<button class="btn btn-primary" type="button">\n  Messages <span class="badge">4</span>\n</button>\n
1
2
3

Alerts

  • alert
  • alert-color
  • alert-dismissible
  • alert-link
<div class="alert alert-warning alert-dismissible" role="alert">\n  <button type="button" class="close" data-dismiss="alert" aria-label="Close">\n    <span aria-hidden="true">&times;</span>\n  </button>\n  <strong>Warning!</strong> Better check yourself, you're not looking too good.\n  <a href="#" class="alert-link">...</a>\n</div>\n
1
2
3
4
5
6
7

Progress Bars

  • progress-bar
  • width/aria-valuemin/aria-valuemax
  • progress-bar-color
  • progress-bar-striped
  • stacked: put multi-progress-bar into same .progress
<div class="progress">\n  <div\n    class="progress-bar progress-bar-success"\n    role="progressbar"\n    aria-valuenow="40"\n    aria-valuemin="0"\n    aria-valuemax="100"\n    style="width:\n  40%"\n  >\n    <span class="sr-only">40% Complete (success)</span>\n  </div>\n</div>\n<div class="progress">\n  <div\n    class="progress-bar progress-bar-info"\n    role="progressbar"\n    aria-valuenow="20"\n    aria-valuemin="0"\n    aria-valuemax="100"\n    style="width:\n  20%"\n  >\n    <span class="sr-only">20% Complete</span>\n  </div>\n</div>\n<div class="progress">\n  <div\n    class="progress-bar progress-bar-warning"\n    role="progressbar"\n    aria-valuenow="60"\n    aria-valuemin="0"\n    aria-valuemax="100"\n    style="width:\n  60%"\n  >\n    <span class="sr-only">60% Complete (warning)</span>\n  </div>\n</div>\n<div class="progress">\n  <div\n    class="progress-bar progress-bar-danger"\n    role="progressbar"\n    aria-valuenow="80"\n    aria-valuemin="0"\n    aria-valuemax="100"\n    style="width:\n  80%"\n  >\n    <span class="sr-only">80% Complete (danger)</span>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

Tooltip

  • data-toggle="tooltip"
  • data-placement="left"
  • title="Tooltip on left"
// manually initialization\n$(function () {\n  $('[data-toggle="tooltip"]').tooltip();\n});\n
1
2
3
4
<button\n  type="button"\n  class="btn btn-default"\n  data-toggle="tooltip"\n  data-placement="left"\n  title="Tooltip on left"\n>\n  Tooltip on left\n</button>\n\n<button\n  type="button"\n  class="btn btn-default"\n  data-toggle="tooltip"\n  data-placement="top"\n  title="Tooltip on top"\n>\n  Tooltip on top\n</button>\n\n<button\n  type="button"\n  class="btn btn-default"\n  data-toggle="tooltip"\n  data-placement="bottom"\n  title="Tooltip on bottom"\n>\n  Tooltip on bottom\n</button>\n\n<button\n  type="button"\n  class="btn btn-default"\n  data-toggle="tooltip"\n  data-placement="right"\n  title="Tooltip on right"\n>\n  Tooltip on right\n</button>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

Popover

  • data-container="body"
  • data-toggle="popover"
  • data-trigger="focus"
  • data-placement="left"
  • title="left"
  • data-content="Button content."
// manually initialization\n$(function () {\n  $('[data-toggle="popover"]').popover();\n});\n
1
2
3
4
<button\n  type="button"\n  class="btn btn-default"\n  data-container="body"\n  data-toggle="popover"\n  data-placement="left"\n  title="left"\n  data-content="Button Content."\n>\n  Popover on left\n</button>\n\n<button\n  type="button"\n  class="btn btn-default"\n  data-container="body"\n  data-toggle="popover"\n  data-placement="top"\n  title="top"\n  data-content="Button content."\n>\n  Popover on top\n</button>\n\n<button\n  type="button"\n  class="btn btn-default"\n  data-container="body"\n  data-toggle="popover"\n  data-placement="bottom"\n  title="bottom"\n  data-content="Button content."\n>\n  Popover on bottom\n</button>\n\n<button\n  type="button"\n  class="btn btn-default"\n  data-container="body"\n  data-toggle="popover"\n  data-placement="right"\n  title="right"\n  data-content="Button content."\n>\n  Popover on right\n</button>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
data-dismiss ="modal"\n
1

修饰.modal-dialog、.modal-content

$().modal('');

<!-- Button trigger modal -->\n<button\n  type="button"\n  class="btn btn-primary btn-lg"\n  data-toggle="modal"\n  data-target="#myModal"\n>\n  Launch demo modal\n</button>\n\n<!-- Modal -->\n<div\n  class="modal fade"\n  id="myModal"\n  tabindex="-1"\n  role="dialog"\n  aria-labelledby="myModalLabel"\n>\n  <div class="modal-dialog" role="document">\n    <div class="modal-content">\n      <div class="modal-header">\n        <button\n          type="button"\n          class="close"\n          data-dismiss="modal"\n          aria-label="Close"\n        >\n          <span aria-hidden="true">&times;</span>\n        </button>\n        <h4 class="modal-title" id="myModalLabel">Modal title</h4>\n      </div>\n      <div class="modal-body">...</div>\n      <div class="modal-footer">\n        <button type="button" class="btn btn-default" data-dismiss="modal">\n          Close\n        </button>\n        <button type="button" class="btn btn-primary">Save changes</button>\n      </div>\n    </div>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<button\n  type="button"\n  class="btn btn-primary"\n  data-toggle="modal"\n  data-target="#exampleModal"\n  data-whatever="@mdo"\n>\n  Open modal for @mdo\n</button>\n<button\n  type="button"\n  class="btn btn-primary"\n  data-toggle="modal"\n  data-target="#exampleModal"\n  data-whatever="@fat"\n>\n  Open modal for @fat\n</button>\n<button\n  type="button"\n  class="btn btn-primary"\n  data-toggle="modal"\n  data-target="#exampleModal"\n  data-whatever="@getBootstrap"\n>\n  Open modal for @getBootstrap\n</button>\n\n<div\n  class="modal fade"\n  id="exampleModal"\n  tabindex="-1"\n  role="dialog"\n  aria-labelledby="exampleModalLabel"\n>\n  <div class="modal-dialog" role="document">\n    <div class="modal-content">\n      <div class="modal-header">\n        <button\n          type="button"\n          class="close"\n          data-dismiss="modal"\n          aria-label="Close"\n        >\n          <span aria-hidden="true">&times;</span>\n        </button>\n        <h4 class="modal-title" id="exampleModalLabel">New message</h4>\n      </div>\n      <div class="modal-body">\n        <form>\n          <div class="form-group">\n            <label for="recipient-name" class="control-label">Recipient:</label>\n            <input type="text" class="form-control" id="recipient-name" />\n          </div>\n          <div class="form-group">\n            <label for="message-text" class="control-label">Message:</label>\n            <textarea class="form-control" id="message-text"></textarea>\n          </div>\n        </form>\n      </div>\n      <div class="modal-footer">\n        <button type="button" class="btn btn-default" data-dismiss="modal">\n          Close\n        </button>\n        <button type="button" class="btn btn-primary">Send message</button>\n      </div>\n    </div>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

Button

  • targets: a/input/button
  • base: btn
  • color: btn-primary btn-success btn-warning btn-danger btn-info
  • size: btn-xs/sm/lg
  • display: btn-block/btn-main
  • group: btn-toolbar/btn-group/btn-group-vertical/btn-group-lg
<div class="btn-toolbar">\n  <div class="btn-group">\n    <button class="btn"></button>\n    <button class="btn"></button>\n    <button class="btn"></button>\n  </div>\n</div>\n\n<div class="btn-group btn-group-vertical">\n  <button class="btn"></button>\n  <button class="btn"></button>\n  <button class="btn"></button>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
  • other: btn-social-icon
<button data-toggle="””" data-target-”” data-dismiss="””"></button>\n
1

Jumbotron

<div class="jumbotron">\n  <div class="container">\n    <div class="main">\n      <h1>We are Broadway</h1>\n      <a href="#" class="btn-main"> Get started </a>\n    </div>\n  </div>\n</div>\n
1
2
3
4
5
6
7
8

Icon-Fonts

<span class="glyphicon glyphicon-home" aria-hidden="true"></span>\n<i class="fa fa-phone/fa-fax/fa-envelope"></i>\n
1
2

Slide Show:

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">\n  <!-- Indicators -->\n  <ol class="carousel-indicators">\n    <li\n      data-target="#carousel-example-generic"\n      data-slide-to="0"\n      class="active"\n    ></li>\n    <li data-target="#carousel-example-generic" data-slide-to="1"></li>\n    <li data-target="#carousel-example-generic" data-slide-to="2"></li>\n  </ol>\n\n  <!-- Wrapper for slides -->\n  <div class="carousel-inner" role="listbox">\n    <div class="item active">\n      <img src="..." alt="..." />\n      <div class="carousel-caption">...</div>\n    </div>\n    <div class="item">\n      <img src="..." alt="..." />\n      <div class="carousel-caption">...</div>\n    </div>\n    ...\n  </div>\n\n  <!-- Controls -->\n  <a\n    class="left carousel-control"\n    href="#carousel-example-generic"\n    role="button"\n    data-slide="prev"\n  >\n    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>\n    <span class="sr-only">Previous</span>\n  </a>\n  <a\n    class="right carousel-control"\n    href="#carousel-example-generic"\n    role="button"\n    data-slide="next"\n  >\n    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>\n    <span class="sr-only">Next</span>\n  </a>\n</div>\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
.carousel(options)\n\nInitializes the carousel with an optional options object\nand starts cycling through items.\nCopy\n\n$('.carousel').carousel({\n  interval: 2000\n})\n\n.carousel('cycle')\n\nCycles through the carousel items from left to right.\n.carousel('pause')\n\nStops the carousel from cycling through items.\n.carousel(number)\n\nCycles the carousel to a particular frame (0 based, similar to an array).\n.carousel('prev')\n\nCycles to the previous item.\n.carousel('next')\n\nCycles to the next item.\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Common Class

color

  • default/primary/info/warnings/danger

size

  • xs/sm/md/lg

state

  • active
  • disabled

Bootstrap Animation

  • collapse
  • fade
  • in

Bootstrap 4

Bootstrap Card

  • .well: .card.bg-light
  • .panel: .card
  • .thumbnail: .card + .card-img/.card-img-top

Custom Bootstrap Theme

Webpack Setup

{\n  "scripts": {\n    "start": "npm run dev",\n    "dev": "cross-env NODE_ENV=development \\\n      webpack-dev-server --port 2333 --mode development --open",\n    "build": "cross-env NODE_ENV=production webpack --mode production",\n    "lint": "stylelint ./src/**/*.scss ./src/**/*.css && \\\n      eslint --ext .js --ext .jsx ./src"\n  },\n  "devDependencies": {\n    "@babel/cli": "^7.2.3",\n    "@babel/core": "^7.3.3",\n    "@babel/plugin-proposal-class-properties": "^7.3.3",\n    "@babel/plugin-proposal-object-rest-spread": "^7.3.2",\n    "@babel/preset-env": "^7.3.1",\n    "autoprefixer": "^8.6.2",\n    "babel-eslint": "^10.0.1",\n    "babel-loader": "^8.0.5",\n    "clean-webpack-plugin": "^0.1.19",\n    "cross-env": "^5.2.0",\n    "css-loader": "^0.28.11",\n    "eslint": "^4.19.1",\n    "eslint-config-airbnb": "^17.0.0",\n    "eslint-loader": "^2.1.0",\n    "eslint-plugin-import": "^2.13.0",\n    "eslint-plugin-jsx-a11y": "^6.1.1",\n    "eslint-plugin-react": "^7.10.0",\n    "html-loader": "^0.5.5",\n    "html-webpack-plugin": "^3.2.0",\n    "mini-css-extract-plugin": "^0.4.0",\n    "node-sass": "^4.11.0",\n    "postcss-loader": "^2.1.5",\n    "precss": "^4.0.0",\n    "sass-loader": "^7.0.3",\n    "style-loader": "^0.21.0",\n    "stylelint": "^9.10.1",\n    "stylelint-config-mass": "^1.0.2",\n    "stylelint-webpack-plugin": "^0.10.5",\n    "webpack": "^4.12.0",\n    "webpack-cli": "^3.0.6",\n    "webpack-dev-server": "^3.2.0"\n  },\n  "dependencies": {\n    "bootstrap": "^4.3.1",\n    "jquery": "^3.3.1",\n    "popper.js": "^1.14.7"\n  }\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const path = require('path');\n\nconst HtmlWebpackPlugin = require('html-webpack-plugin');\nconst MiniCssExtractPlugin = require('mini-css-extract-plugin');\nconst CleanWebpackPlugin = require('clean-webpack-plugin');\nconst StyleLintPlugin = require('stylelint-webpack-plugin');\n\nconst packageJson = require('./package.json');\n\nconst devMode = process.env.NODE_ENV !== 'production';\nconst useSass = !!packageJson.devDependencies['node-sass'];\n\nconst styleLoader = [\n  devMode ? 'style-loader' : MiniCssExtractPlugin.loader,\n  {\n    loader: 'css-loader',\n    options: {\n      minimize: !devMode,\n    },\n  },\n  'postcss-loader',\n];\n\nif (useSass) {\n  styleLoader.push('sass-loader');\n}\n\nmodule.exports = {\n  entry: {\n    main: './src/index.js',\n  },\n  output: {\n    filename: '[name].[chunkhash].js',\n    path: path.resolve(__dirname, 'build'),\n  },\n  module: {\n    rules: [\n      {\n        test: /\\.(js|jsx)$/,\n        exclude: /node_modules/,\n        use: ['babel-loader', 'eslint-loader'],\n      },\n      {\n        test: /\\.html$/,\n        use: [\n          {\n            loader: 'html-loader',\n            options: {\n              minimize: !devMode,\n            },\n          },\n        ],\n      },\n      {\n        test: /\\.(css|scss)$/,\n        use: [...styleLoader],\n      },\n    ],\n  },\n  plugins: [\n    new CleanWebpackPlugin('build'),\n    new HtmlWebpackPlugin({\n      hash: true,\n      template: './src/index.html',\n      filename: './index.html',\n    }),\n    new MiniCssExtractPlugin({\n      filename: devMode ? '[name].css' : '[name].[hash].css',\n      chunkFilename: devMode ? '[id].css' : '[id].[hash].css',\n    }),\n    new StyleLintPlugin(),\n  ],\n  resolve: {\n    extensions: ['.js', '.jsx'],\n  },\n  devtool: 'source-map',\n};\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

Bootstrap Reboot Tips

  • @import '~bootstrap/scss/reboot;

Some useful best practices:

  • body font-size: 1rem for scalable component spacing.
  • avoid margin-top as vertical margins collapse (only use margin-bottom for headings h1/.../h6, lists ul/ol/dl/dd, <pre></pre>)
  • block use rems for margin for easier scaling across device sizes
  • using inherit whenever possible for font- property
  • box-sizing: border-box is globally set on every element including *::before and *::after
  • body sets a global font-family, line-height and text-align
  • body sets background-color: #fff for safety
  • legend/fieldset have no borders/padding/margin
  • label are set to display: inline-block to allow margin
  • textarea are modified to only be resizable vertically resize: vertical as horizontal resizing often “breaks” page layout
  • summary are set to cursor: pointer

Useful Custom Functions

  • @import '~bootstrap/scss/functions';
  • @import '~bootstrap/scss/mixins';
@function color($key: 'blue') {\n  @return map-get($colors, $key);\n}\n\n@function theme-color($key: 'primary') {\n  @return map-get($theme-colors, $key);\n}\n\n@function gray($key: '100') {\n  @return map-get($grays, $key);\n}\n\n@function theme-color-level($color-name: 'primary', $level: 0) {\n  $color: theme-color($color-name);\n  $color-base: if($level > 0, #000, #fff);\n  $level: abs($level);\n\n  @return mix($color-base, $color, $level * $theme-color-interval);\n}\n\n/* color contrast: color-yiq(color) */\n.custom-element {\n  background-color: color-yiq(#000);\n  color: color-yiq(theme-color('dark'));\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Custom Global Options

$enable-caret: true !default;\n$enable-rounded: true !default;\n$enable-shadows: false !default;\n$enable-gradients: false !default;\n$enable-transitions: true !default;\n$enable-prefers-reduced-motion-media-query: true !default;\n$enable-grid-classes: true !default;\n$enable-pointer-cursor-for-buttons: true !default;\n$enable-print-styles: true !default;\n$enable-responsive-font-sizes: false !default;\n$enable-validation-icons: true !default;\n$enable-deprecation-messages: true !default;\n
1
2
3
4
5
6
7
8
9
10
11
12

Custom Colors

  • @import '~bootstrap/scss/variables';
$theme-colors: (\n  'primary': #0074d9,\n  'danger': #ff4136 'secondary': #495057,\n  'success': #37b24d,\n  'info': #1c7ed6,\n  'warning': #f59f00,\n  'danger': #f03e3e\n);\n\n$colors: (\n  'blue': $blue,\n  'indigo': $indigo,\n  'purple': $purple,\n  'pink': $pink,\n  'red': $red,\n  'orange': $orange,\n  'yellow': $yellow,\n  'green': $green,\n  'teal': $teal,\n  'cyan': $cyan,\n  'white': $white,\n  'gray': $gray-600,\n  'gray-dark': $gray-800\n) !default;\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Custom Spacing

key variable - $spacer:

  • t - for classes that set margin-top or padding-top
  • b - for classes that set margin-bottom or padding-bottom
  • l - for classes that set margin-left or padding-left
  • r - for classes that set margin-right or padding-right
  • x - for classes that set both xxx-left and xxx-right
  • y - for classes that set both xxx-top and xxx-bottom
  • blank - for classes that set a margin or padding on all 4 sides of the element
  • 0 - for classes that eliminate the margin or padding by setting it to 0
  • 1 - (by default) for classes that set the margin or padding to $spacer * .25
  • 2 - (by default) for classes that set the margin or padding to $spacer * .5
  • 3 - (by default) for classes that set the margin or padding to $spacer
  • 4 - (by default) for classes that set the margin or padding to $spacer * 1.5
  • 5 - (by default) for classes that set the margin or padding to $spacer * 3
  • auto - for classes that set the margin to auto
.mt-0 {\n  margin-top: 0 !important;\n}\n\n.ml-1 {\n  margin-left: ($spacer * 0.25) !important;\n}\n\n.px-2 {\n  padding-right: ($spacer * 0.5) !important;\n  padding-left: ($spacer * 0.5) !important;\n}\n\n.p-3 {\n  padding: $spacer !important;\n}\n\n.mt-n1 {\n  margin-top: -0.25rem !important;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Custom Layout

$grid-columns: 12 !default;\n$grid-gutter-width: 30px !default;\n
1
2

Custom Borders

$border-width: 1px !default;\n$border-color: $gray-300 !default;\n$border-radius: 0.25rem !default;\n$border-radius-lg: 0.3rem !default;\n$border-radius-sm: 0.2rem !default;\n
1
2
3
4
5

Custom Navbar and Navigation

/* $nav-link-padding-x: 1.5rem; */\n$navbar-nav-link-padding-x: 1.5rem;\n$nav-link-padding-y: 1rem;\n\n$navbar-light-color: $violet-4;\n$navbar-light-hover-color: $violet-6;\n$navbar-light-active-color: $violet-9;\n$navbar-light-toggler-border-color: $violet-2;\n\n$navbar-dark-color: $violet-3;\n$navbar-dark-hover-color: $violet-5;\n$navbar-dark-active-color: $violet-1;\n$navbar-dark-toggler-border-color: $violet-1;\n\n$nav-tabs-border-color: $primary;\n$nav-tabs-link-hover-border-color: $violet-5;\n$nav-tabs-link-active-color: $violet-9;\n$nav-tabs-link-active-bg: $violet-3;\n$nav-tabs-link-active-border-color: $violet-1;\n\n$nav-pills-link-active-color: $white;\n$nav-pills-link-active-bg: $primary;\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Custom Dropdown

custom $dropdown- variables

$dropdown-padding-y: 1rem;\n$dropdown-spacer: 0.5rem;\n$dropdown-bg: $white;\n$dropdown-border-color: $primary;\n$dropdown-border-width: $border-width * 3;\n$dropdown-link-color: $primary;\n$dropdown-item-padding-y: 0.5rem;\n$dropdown-item-padding-x: 3rem;\n
1
2
3
4
5
6
7
8

Custom List Group

$list-group-border-color: $primary;\n
1

Custom Card

$card-border-color: $primary;\n$card-color: $primary;\n$card-bg: $violet-0;\n
1
2
3

Custom Breadcrumb

$breadcrumb-bg: $violet-0;\n$breadcrumb-divider-color: $gray-600 !default;\n$breadcrumb-active-color: $violet-3;\n$breadcrumb-divider: quote('>');\n
1
2
3
4

Custom Form

$input-btn-padding-y: 0.75rem;\n$input-btn-padding-x: 1.5rem;\n$input-btn-focus-width: 0; /* remove focus box-shadow */\n$custom-control-indicator-checked-color: $primary;\n
1
2
3
4

Reference

',164);p.render=function(s,t,p,o,l,c){return n(),a("div",null,[e])};export default p;export{t as __pageData}; diff --git a/assets/web_css_bootstrapBasicNotes.md.222d8e91.lean.js b/assets/web_css_bootstrapBasicNotes.md.222d8e91.lean.js new file mode 100644 index 00000000000..a5929d4eda5 --- /dev/null +++ b/assets/web_css_bootstrapBasicNotes.md.222d8e91.lean.js @@ -0,0 +1 @@ +import{o as n,c as a,a as s}from"./app.6dd2a1b7.js";const t='{"title":"Bootstrap Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Bootstrap Basis","slug":"bootstrap-basis"},{"level":2,"title":"Grid System","slug":"grid-system"},{"level":3,"title":"Basic","slug":"basic"},{"level":3,"title":"row","slug":"row"},{"level":2,"title":"Nav","slug":"nav"},{"level":3,"title":"NavBar","slug":"navbar"},{"level":3,"title":"BreadCrumb","slug":"breadcrumb"},{"level":3,"title":"Scroll Spy","slug":"scroll-spy"},{"level":2,"title":"Tabs and Pills","slug":"tabs-and-pills"},{"level":3,"title":"Tab Pane","slug":"tab-pane"},{"level":3,"title":"Tab Pane JS API","slug":"tab-pane-js-api"},{"level":3,"title":"Tab Pane JS Events","slug":"tab-pane-js-events"},{"level":2,"title":"Collapse","slug":"collapse"},{"level":3,"title":"Accordion","slug":"accordion"},{"level":2,"title":"Text","slug":"text"},{"level":2,"title":"Form","slug":"form"},{"level":3,"title":"Form Basic","slug":"form-basic"},{"level":3,"title":"CheckBox and Select","slug":"checkbox-and-select"},{"level":3,"title":"Form Alerts and Prompts","slug":"form-alerts-and-prompts"},{"level":2,"title":"Table","slug":"table"},{"level":3,"title":"Table Basic Class","slug":"table-basic-class"},{"level":2,"title":"Card","slug":"card"},{"level":3,"title":"Panel","slug":"panel"},{"level":3,"title":"Well","slug":"well"},{"level":2,"title":"Images","slug":"images"},{"level":3,"title":"Img Class","slug":"img-class"},{"level":3,"title":"Thumbnail","slug":"thumbnail"},{"level":3,"title":"Media","slug":"media"},{"level":3,"title":"Embed","slug":"embed"},{"level":2,"title":"Alerts and Prompts","slug":"alerts-and-prompts"},{"level":3,"title":"Labels","slug":"labels"},{"level":3,"title":"Badges","slug":"badges"},{"level":3,"title":"Alerts","slug":"alerts"},{"level":3,"title":"Progress Bars","slug":"progress-bars"},{"level":3,"title":"Tooltip","slug":"tooltip"},{"level":3,"title":"Popover","slug":"popover"},{"level":3,"title":"Modal","slug":"modal"},{"level":2,"title":"Button","slug":"button"},{"level":2,"title":"Jumbotron","slug":"jumbotron"},{"level":2,"title":"Icon-Fonts","slug":"icon-fonts"},{"level":2,"title":"Carousel","slug":"carousel"},{"level":2,"title":"Common Class","slug":"common-class"},{"level":3,"title":"color","slug":"color"},{"level":3,"title":"size","slug":"size"},{"level":3,"title":"state","slug":"state"},{"level":3,"title":"Bootstrap Animation","slug":"bootstrap-animation"},{"level":2,"title":"Bootstrap 4","slug":"bootstrap-4"},{"level":3,"title":"Bootstrap Card","slug":"bootstrap-card"},{"level":2,"title":"Custom Bootstrap Theme","slug":"custom-bootstrap-theme"},{"level":3,"title":"Webpack Setup","slug":"webpack-setup"},{"level":3,"title":"Bootstrap Reboot Tips","slug":"bootstrap-reboot-tips"},{"level":3,"title":"Useful Custom Functions","slug":"useful-custom-functions"},{"level":3,"title":"Custom Global Options","slug":"custom-global-options"},{"level":3,"title":"Custom Colors","slug":"custom-colors"},{"level":3,"title":"Custom Spacing","slug":"custom-spacing"},{"level":3,"title":"Custom Layout","slug":"custom-layout"},{"level":3,"title":"Custom Borders","slug":"custom-borders"},{"level":3,"title":"Custom Navbar and Navigation","slug":"custom-navbar-and-navigation"},{"level":3,"title":"Custom Dropdown","slug":"custom-dropdown"},{"level":3,"title":"Custom List Group","slug":"custom-list-group"},{"level":3,"title":"Custom Card","slug":"custom-card"},{"level":3,"title":"Custom Breadcrumb","slug":"custom-breadcrumb"},{"level":3,"title":"Custom Form","slug":"custom-form"},{"level":2,"title":"Reference","slug":"reference"}],"relativePath":"web/css/bootstrapBasicNotes.md","lastUpdated":1628014655000}',p={},e=s('',164);p.render=function(s,t,p,o,l,c){return n(),a("div",null,[e])};export default p;export{t as __pageData}; diff --git a/assets/web_css_cssAdvancedNotes.md.fcd5d609.js b/assets/web_css_cssAdvancedNotes.md.fcd5d609.js new file mode 100644 index 00000000000..15a052272f1 --- /dev/null +++ b/assets/web_css_cssAdvancedNotes.md.fcd5d609.js @@ -0,0 +1 @@ +import{o as n,c as a,a as s}from"./app.6dd2a1b7.js";const e='{"title":"CSS Advanced Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"CSS Framework Key Points","slug":"css-framework-key-points"},{"level":2,"title":"Typography Principles","slug":"typography-principles"},{"level":3,"title":"font-size and spacing","slug":"font-size-and-spacing"},{"level":3,"title":"Table","slug":"table"},{"level":2,"title":"Grid System","slug":"grid-system"},{"level":2,"title":"Form Design Principles","slug":"form-design-principles"},{"level":3,"title":"Buttons Placement Principles","slug":"buttons-placement-principles"},{"level":2,"title":"Graceful Degradation","slug":"graceful-degradation"},{"level":2,"title":"CSS Houdini","slug":"css-houdini"},{"level":2,"title":"CSS Performance","slug":"css-performance"},{"level":3,"title":"Basic Perf Tips","slug":"basic-perf-tips"},{"level":3,"title":"CSS Selectors","slug":"css-selectors"},{"level":3,"title":"CSS Triggers","slug":"css-triggers"},{"level":3,"title":"will-change","slug":"will-change"},{"level":3,"title":"contain","slug":"contain"},{"level":3,"title":"Animation Frame","slug":"animation-frame"},{"level":3,"title":"CSS Loading Tips","slug":"css-loading-tips"},{"level":3,"title":"Animation","slug":"animation"},{"level":2,"title":"CSS Hacks","slug":"css-hacks"},{"level":2,"title":"Design Principles","slug":"design-principles"},{"level":3,"title":"费茨定律","slug":"费茨定律"},{"level":3,"title":"米勒定律","slug":"米勒定律"},{"level":3,"title":"席克定律","slug":"席克定律"},{"level":3,"title":"泰斯勒定律","slug":"泰斯勒定律"}],"relativePath":"web/css/cssAdvancedNotes.md","lastUpdated":1627196035000}',t={},l=s('

CSS Advanced Notes

CSS Framework Key Points

  • Content
  • Centering
  • Font Family
  • Spacing
  • Color and Contrast
  • Balance (Position)
  • Primary and Secondary Color
  • Custom Text (Font)
  • Images and Links

Typography Principles

  • The typeface (font-family)
  • Type (modular) scale
  • Responsiveness of the text (size unit and breakpoints)
  • Spacing and vertical rhythm
  • Colors (theming)

refer to: font-family, font-size, spacing, color

font-size and spacing

  • set a base-size
  • multiples of base-size
  • use rem for most font-size, use em for some spacing (needing responsive design)
$xs: $base / $ratio / $ratio;\n$sm: $base / $ratio;\n$md: $base;\n$lg: $base * $ratio;\n$xl: $base * $ratio * ratio;\n
1
2
3
4
5

font-size

  • rem is better
  • em for responsive layout (etc. layer2 font based-on layer1 font in dropdown menu)
  • make text legible: at least 16px

spacing

make text breathe:

  • margin/padding: at least 15px
  • line-height: 1.4
  • word-spacing
  • letter-spacing
  • 60-100 characters per line

vertical rhythms

keep vertical spaces between elements on a page consistent (and relative) to each other:

  • Set the vertical white space between elements to a multiple of base-size
  • Set the line-height of all text elements to a multiple of base-size

Table

  • remove fills, grid lines, border and bolding
  • left-align text, right-align numbers and align headings with data
  • put white space to work to group and separate

Grid System

  • Must have different traits at different sizes
  • Must be fluid between breakpoints
  • Must have enough control to decide which columns will transform and at which point
  • Classes should ideally still make sense at all breakpoints

Form Design Principles

Buttons Placement Principles

  • Align the primary button to the left edge of the inputs
  • Put the back button above the form
  • Put tangentially related actions above the form
  • Place extra buttons based on what they do
  • In some single field forms put the button next to the input (e.g search button)
  • Put buttons on multi select forms above the form

https://adamsilver.io/articles/where-to-put-buttons-in-forms

Graceful Degradation

write old browser css code, then write modern browser css code

.grid {\n  display: flex;\n}\n\n/* old browser don't support this rule */\n/* old browser will ignore this rule */\n.grid {\n  display: grid;\n}\n
1
2
3
4
5
6
7
8
9

CSS Houdini

CSS Houdini bring many API:

  • css properties and values api: custom properties (--var, var(--var)), custom function (--darken(--var))
  • css layout api (layoutWorklet): layout below (display: flex/grid), layout above (css-sizing: min-content/max-content/fit-content)
  • css paint api (paintWorklet): background, background-color, background-image
  • css composite api (compositeWorklet)

to let developers enhance css without any browser updates

CSS Performance

Basic Perf Tips

  • use audits panel to diagnose
  • use CSS shorthand and color shortcuts
  • eliminate unneeded zeros and units
  • remove unused CSS by coverage panel of Devtools
  • link is parallel, @import isn't parallel

CSS Selectors

减少选择器的复杂性,与构造样式本身的其他工作相比, 选择器复杂性可以占用计算元素样式所需时间的 50%以上

CSS Triggers

avoid to frequently change css property or call JS DOM API triggering layout stage (reflow)

will-change

告知浏览器该元素会有哪些变化的方法,这样浏览器可以在元素属性真正发生变化之前提前做好对应的优化准备工作

 {\n  will-change: auto;\n  will-change: scroll-position;\n  will-change: contents;\n  will-change: transform; /* Example of <custom-ident> */\n  will-change: opacity; /* Example of <custom-ident> */\n  will-change: left, top; /* Example of two <animate-feature> */\n\n  will-change: unset;\n  will-change: initial;\n  will-change: inherit;\n}\n
1
2
3
4
5
6
7
8
9
10
11
12

contain

CSS Containment

contain 属性允许开发者声明当前元素和它的内容尽可能的独立于 DOM 树的其他部分。 这使得浏览器在重新计算布局、样式、绘图或它们的组合的时候,只会影响到有限的 DOM 区域,而不是整个页面

/* 无布局包含*/\ncontain: none;\n\n/* 布局包含 layout、style、paint 和 size*/\ncontain: strict;\n\n/* 布局包含layout、style 和 paint */\ncontain: content;\n\n/* 布局包含 size */\ncontain: size;\n\n/* 布局包含 layout */\ncontain: layout;\n\n/* 布局包含 style */\ncontain: style;\n\n/* 布局包含 paint */\ncontain: paint;\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • size: 声明这个元素的尺寸会变化,不需要去检查它依赖关系中的尺寸变化
  • style: 声明那些同时会影响这个元素和其子孙元素的属性,都在这个元素的包含范围内
  • layout: 声明没有外部元素可以影响它内部的布局,反之亦然
  • paint: 声明这个元素的子孙节点不会在它边缘外显示。如果一个元素在视窗外或因其他原因导致不可见,则同样保证它的子孙节点不会被显示

Animation Frame

window.requestAnimationFrame:

  • reflow: javascript -> style -> layout -> paint -> composite
  • repaint: paint -> composite

告诉浏览器希望执行动画并请求浏览器在下一次重绘之前调用指定的函数来更新动画。该方法使用一个回调函数作为参数,这个回调函数会在浏览器重绘之前调用

若想要在下次重绘时产生另一个动画画面,callback 必须调用 requestAnimationFrame

const start = null;\nconst element = document.getElementById('SomeElementYouWantToAnimate');\nelement.style.position = 'absolute';\n\nfunction step(timestamp) {\n  if (!start) {\n    start = timestamp;\n  }\n\n  const progress = timestamp - start;\n  element.style.left = Math.min(progress / 10, 200) + 'px';\n\n  if (progress < 2000) {\n    window.requestAnimationFrame(step);\n  }\n}\n\nwindow.requestAnimationFrame(step);\n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

CSS Loading Tips

  • Lazyload any CSS not needed for Start Render:
    • This could be Critical CSS;
    • or splitting your CSS into Media Queries.
  • Avoid @import:
    • In your HTML;
    • but in CSS especially;
    • and beware of oddities with the PreLoad Scanner.
  • Be wary of synchronous CSS and JavaScript order:
    • JavaScript defined after CSS won’t run until CSSOM is completed;
    • so if your JavaScript doesn’t depend on your CSS;
      • load it before your CSS;
    • but if it does depend on your CSS:
      • load it after your CSS.
  • Load CSS as the DOM needs it:
    • This unblocks Start Render and allows progressive rendering.
<link rel="preload" href="/path/to/split.css" as="style" />\n<link\n  rel="stylesheet"\n  href="/path/to/split.css"\n  media="print"\n  onload="this.media='all'"\n/>\n
1
2
3
4
5
6
7

Animation

Best Practice

  • High Performance Tips
  • all animation: keyframe animation or transitions is best
  • js-based animation: requestAnimationFrame is better than setTimeout/setInterval
  • position animation:transform: translate(npx, npx) is better than top/right/bottom/left
  • scale animation: transform: scale(n) better than width/height
  • rotation animation: transform: rotate(deg) is better
  • opacity/visibility animation: opacity: 0...1 is better

DevTools for Animation

  • DevTools for Animation Performance
  • slower CPU simulation in performance panel
  • enable paint instrumentation in performance panel
  • FPS meter in rendering panel
  • paint flashing in rendering panel
  • layers panel

Animation Internal

  • width/height/margin/left/top in Layout stage
  • box-shadow/border-radius/background/outline/color in Paint stage
  • cursor/z-index/transform/opacity in Composite Layers stage
  • top/left has very large time to paint each frame

CSS Hacks

Design Principles

费茨定律

人机交互和人体工程学中人类活动的模型, 它预测了从任意位置快速移动到一个目标位置所需的时间, 由 2 个位置的距离(D)和目标大小(S)有关, 正比于 D, 反比于 S:

  • 关联性强的 UI 放置在一起
  • 大拇指点击热区
  • 屏幕边界视为无限大 (容易到达)
  • 关机滑动距离长

米勒定律

人的短时记忆能力广度为 7±2 个信息块:

  • 手机号/银行卡号/超大数字分段放置, 信息分层 e.g 134 9999 9999, 999, 999, 999
  • 文章布局时增大段落间 margin, 改变部分文字的粗细/字体/颜色
  • 导航/选项卡不超过 9 个 (超过 9 个可使用 dropdown/subMenu)

席克定律

用户所面临的选择数量越多, 做出选择所花费的时间就越长, 在人机交互的界面中选项越多, 意味着用户做出决策的时间越长:

  • 减少选项并提供默认值
  • 分类分层
  • 分步分页 (大部分手机应用注册界面)

泰斯勒定律

泰斯勒定律又称复杂性守恒定律, 该定律认为每一个过程都有其固有的复杂性, 这个复杂性存在一个临界点, 超过了这个点就不能再简化了, 你只能将固有的复杂性从一个地方移动到另外一个地方:

  • 智能手机: 按键的复杂度转为手机操作系统的复杂度
  • 智能推荐: 用户自己选择筛选条件的复杂度转为人工智能算法的复杂度
',82);t.render=function(s,e,t,i,o,p){return n(),a("div",null,[l])};export default t;export{e as __pageData}; diff --git a/assets/web_css_cssAdvancedNotes.md.fcd5d609.lean.js b/assets/web_css_cssAdvancedNotes.md.fcd5d609.lean.js new file mode 100644 index 00000000000..51cee9ae72c --- /dev/null +++ b/assets/web_css_cssAdvancedNotes.md.fcd5d609.lean.js @@ -0,0 +1 @@ +import{o as n,c as a,a as s}from"./app.6dd2a1b7.js";const e='{"title":"CSS Advanced Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"CSS Framework Key Points","slug":"css-framework-key-points"},{"level":2,"title":"Typography Principles","slug":"typography-principles"},{"level":3,"title":"font-size and spacing","slug":"font-size-and-spacing"},{"level":3,"title":"Table","slug":"table"},{"level":2,"title":"Grid System","slug":"grid-system"},{"level":2,"title":"Form Design Principles","slug":"form-design-principles"},{"level":3,"title":"Buttons Placement Principles","slug":"buttons-placement-principles"},{"level":2,"title":"Graceful Degradation","slug":"graceful-degradation"},{"level":2,"title":"CSS Houdini","slug":"css-houdini"},{"level":2,"title":"CSS Performance","slug":"css-performance"},{"level":3,"title":"Basic Perf Tips","slug":"basic-perf-tips"},{"level":3,"title":"CSS Selectors","slug":"css-selectors"},{"level":3,"title":"CSS Triggers","slug":"css-triggers"},{"level":3,"title":"will-change","slug":"will-change"},{"level":3,"title":"contain","slug":"contain"},{"level":3,"title":"Animation Frame","slug":"animation-frame"},{"level":3,"title":"CSS Loading Tips","slug":"css-loading-tips"},{"level":3,"title":"Animation","slug":"animation"},{"level":2,"title":"CSS Hacks","slug":"css-hacks"},{"level":2,"title":"Design Principles","slug":"design-principles"},{"level":3,"title":"费茨定律","slug":"费茨定律"},{"level":3,"title":"米勒定律","slug":"米勒定律"},{"level":3,"title":"席克定律","slug":"席克定律"},{"level":3,"title":"泰斯勒定律","slug":"泰斯勒定律"}],"relativePath":"web/css/cssAdvancedNotes.md","lastUpdated":1627196035000}',t={},l=s('',82);t.render=function(s,e,t,i,o,p){return n(),a("div",null,[l])};export default t;export{e as __pageData}; diff --git a/assets/web_css_cssBasicNotes.md.8e99d3ef.js b/assets/web_css_cssBasicNotes.md.8e99d3ef.js new file mode 100644 index 00000000000..17e52838d74 --- /dev/null +++ b/assets/web_css_cssBasicNotes.md.8e99d3ef.js @@ -0,0 +1 @@ +import{o as n,c as s,e as a,b as t,d as e}from"./app.6dd2a1b7.js";const l='{"title":"CSS Basic Notes","description":"","frontmatter":{},"headers":[{"level":2,"title":"Cascading and Inheritance","slug":"cascading-and-inheritance"},{"level":3,"title":"Cascading Order","slug":"cascading-order"},{"level":3,"title":"Specificity","slug":"specificity"},{"level":3,"title":"CSS Inheritance","slug":"css-inheritance"},{"level":2,"title":"Property Value","slug":"property-value"},{"level":3,"title":"Initial Value","slug":"initial-value"},{"level":3,"title":"Specified Value","slug":"specified-value"},{"level":3,"title":"Computed Value","slug":"computed-value"},{"level":3,"title":"Used Value","slug":"used-value"},{"level":3,"title":"Actual Value","slug":"actual-value"},{"level":2,"title":"Property Order","slug":"property-order"},{"level":3,"title":"Property Order Summary","slug":"property-order-summary"},{"level":3,"title":"Property Order Details","slug":"property-order-details"},{"level":2,"title":"Naming Convention","slug":"naming-convention"},{"level":3,"title":"Layout Structure Naming Convention","slug":"layout-structure-naming-convention"},{"level":3,"title":"Navigation Naming Convention","slug":"navigation-naming-convention"},{"level":3,"title":"Functional Component Naming Convention","slug":"functional-component-naming-convention"},{"level":3,"title":"CSS Files Naming Convention","slug":"css-files-naming-convention"},{"level":2,"title":"CSS Selector","slug":"css-selector"},{"level":3,"title":"元素选择器","slug":"元素选择器"},{"level":3,"title":"关系选择器","slug":"关系选择器"},{"level":3,"title":"Attribute Selectors","slug":"attribute-selectors"},{"level":3,"title":"Pseudo Class Selectors","slug":"pseudo-class-selectors"},{"level":3,"title":"Pseudo Element Selectors","slug":"pseudo-element-selectors"},{"level":2,"title":"CSS Normalize","slug":"css-normalize"},{"level":2,"title":"CSS Box","slug":"css-box"},{"level":3,"title":"Width","slug":"width"},{"level":3,"title":"Z Index","slug":"z-index"},{"level":3,"title":"Overflow","slug":"overflow"},{"level":3,"title":"Resize Box","slug":"resize-box"},{"level":3,"title":"Box Sizing","slug":"box-sizing"},{"level":3,"title":"Box Height","slug":"box-height"},{"level":3,"title":"Box Column","slug":"box-column"},{"level":2,"title":"Box Model","slug":"box-model"},{"level":3,"title":"Box Margin","slug":"box-margin"},{"level":3,"title":"Box Percentage Length","slug":"box-percentage-length"},{"level":2,"title":"Block Formatting Context","slug":"block-formatting-context"},{"level":2,"title":"Stack Context","slug":"stack-context"},{"level":3,"title":"Create BFC","slug":"create-bfc"},{"level":3,"title":"Margin Collapsing","slug":"margin-collapsing"},{"level":2,"title":"Float Patterns","slug":"float-patterns"},{"level":3,"title":"Fixed Parent","slug":"fixed-parent"},{"level":3,"title":"Float ClearFix","slug":"float-clearfix"},{"level":3,"title":"Float Best Practice","slug":"float-best-practice"},{"level":2,"title":"Position Patterns","slug":"position-patterns"},{"level":3,"title":"Static Position","slug":"static-position"},{"level":3,"title":"Relative Position","slug":"relative-position"},{"level":3,"title":"Absolute Position","slug":"absolute-position"},{"level":3,"title":"Fixed Position","slug":"fixed-position"},{"level":3,"title":"Sticky Position","slug":"sticky-position"},{"level":2,"title":"Flex Patterns","slug":"flex-patterns"},{"level":3,"title":"Flex Under the Hood","slug":"flex-under-the-hood"},{"level":3,"title":"Flex Useful Shorthand","slug":"flex-useful-shorthand"},{"level":3,"title":"Flex Parent Property","slug":"flex-parent-property"},{"level":3,"title":"Flex Children Property","slug":"flex-children-property"},{"level":3,"title":"Flexibility of Float","slug":"flexibility-of-float"},{"level":3,"title":"Pseudo Elements of Flex Box","slug":"pseudo-elements-of-flex-box"},{"level":3,"title":"Fixed Sidebar with Flexbox","slug":"fixed-sidebar-with-flexbox"},{"level":3,"title":"Flexbox Best Practice","slug":"flexbox-best-practice"},{"level":2,"title":"Grid Patterns","slug":"grid-patterns"},{"level":3,"title":"Grid Attention Tips","slug":"grid-attention-tips"},{"level":3,"title":"Grid Alignment","slug":"grid-alignment"},{"level":2,"title":"Column Patterns","slug":"column-patterns"},{"level":3,"title":"Two Column Pattern","slug":"two-column-pattern"},{"level":3,"title":"Three Column Pattern","slug":"three-column-pattern"},{"level":2,"title":"Centering Patterns","slug":"centering-patterns"},{"level":3,"title":"Horizontal Centering Pattern","slug":"horizontal-centering-pattern"},{"level":3,"title":"Vertical Centering Pattern","slug":"vertical-centering-pattern"},{"level":3,"title":"Mixing Centering Pattern","slug":"mixing-centering-pattern"},{"level":2,"title":"CSS Logical Properties and Values","slug":"css-logical-properties-and-values"},{"level":3,"title":"CSS Logical Basis","slug":"css-logical-basis"},{"level":3,"title":"CSS Logical Reference","slug":"css-logical-reference"},{"level":2,"title":"CSS ScrollBar","slug":"css-scrollbar"},{"level":3,"title":"Custom ScrollBar","slug":"custom-scrollbar"},{"level":3,"title":"Hidden ScrollBar","slug":"hidden-scrollbar"},{"level":2,"title":"CSS Opacity","slug":"css-opacity"},{"level":2,"title":"CSS Colors","slug":"css-colors"},{"level":3,"title":"HSL Color","slug":"hsl-color"},{"level":2,"title":"CSS Border","slug":"css-border"},{"level":3,"title":"Border Radius","slug":"border-radius"},{"level":3,"title":"Border Image","slug":"border-image"},{"level":3,"title":"Border Collapse","slug":"border-collapse"},{"level":3,"title":"Border Best Practice","slug":"border-best-practice"},{"level":2,"title":"CSS Background","slug":"css-background"},{"level":3,"title":"Background Image","slug":"background-image"},{"level":3,"title":"Background Size","slug":"background-size"},{"level":3,"title":"Background Repeat","slug":"background-repeat"},{"level":3,"title":"Background Attachment","slug":"background-attachment"},{"level":3,"title":"Background Clip","slug":"background-clip"},{"level":3,"title":"Background Origin","slug":"background-origin"},{"level":3,"title":"Blend Mode","slug":"blend-mode"},{"level":3,"title":"Mask Style","slug":"mask-style"},{"level":3,"title":"Clip Path","slug":"clip-path"},{"level":3,"title":"Background Best Practice","slug":"background-best-practice"},{"level":2,"title":"CSS Text","slug":"css-text"},{"level":3,"title":"Text Alignment","slug":"text-alignment"},{"level":3,"title":"Text Overflow","slug":"text-overflow"},{"level":3,"title":"Text Wrap","slug":"text-wrap"},{"level":3,"title":"Text Transform","slug":"text-transform"},{"level":3,"title":"Text Writing Mode","slug":"text-writing-mode"},{"level":3,"title":"White Space","slug":"white-space"},{"level":2,"title":"CSS Font","slug":"css-font"},{"level":3,"title":"Font Size","slug":"font-size"},{"level":3,"title":"Font Style","slug":"font-style"},{"level":3,"title":"Font Variant","slug":"font-variant"},{"level":3,"title":"Font Size Adjust","slug":"font-size-adjust"},{"level":3,"title":"Font Display","slug":"font-display"},{"level":3,"title":"Custom Fonts Function","slug":"custom-fonts-function"},{"level":3,"title":"Font Best Practice","slug":"font-best-practice"},{"level":2,"title":"CSS Counter","slug":"css-counter"},{"level":2,"title":"CSS Filter","slug":"css-filter"},{"level":3,"title":"SVG Filter","slug":"svg-filter"},{"level":3,"title":"Backdrop Filter","slug":"backdrop-filter"},{"level":2,"title":"CSS Interactive","slug":"css-interactive"},{"level":3,"title":"Cursor and Pointer","slug":"cursor-and-pointer"},{"level":3,"title":"User Select","slug":"user-select"},{"level":3,"title":"CSS Form","slug":"css-form"},{"level":2,"title":"Object Position and Fit","slug":"object-position-and-fit"},{"level":2,"title":"Animation","slug":"animation"},{"level":3,"title":"Animation Property","slug":"animation-property"},{"level":3,"title":"Animation Helper","slug":"animation-helper"},{"level":3,"title":"Transition","slug":"transition"},{"level":3,"title":"Transform","slug":"transform"},{"level":3,"title":"Animation Details","slug":"animation-details"},{"level":3,"title":"Animation Best Practice","slug":"animation-best-practice"},{"level":3,"title":"GreenSock Library","slug":"greensock-library"},{"level":2,"title":"Responsive Design","slug":"responsive-design"},{"level":3,"title":"Responsive Font","slug":"responsive-font"},{"level":3,"title":"Responsive Length","slug":"responsive-length"},{"level":3,"title":"Responsive Size","slug":"responsive-size"},{"level":3,"title":"Responsive Box","slug":"responsive-box"},{"level":3,"title":"Responsive Image","slug":"responsive-image"},{"level":2,"title":"Media Query","slug":"media-query"},{"level":3,"title":"设备类型","slug":"设备类型"},{"level":3,"title":"设备特性","slug":"设备特性"},{"level":3,"title":"Media Query Support Detection","slug":"media-query-support-detection"},{"level":3,"title":"JavaScript Media Query API","slug":"javascript-media-query-api"},{"level":2,"title":"Accessibility","slug":"accessibility"},{"level":3,"title":"Screen Reader Only","slug":"screen-reader-only"},{"level":2,"title":"Awesome Components","slug":"awesome-components"},{"level":3,"title":"Landing Page","slug":"landing-page"},{"level":3,"title":"Modal","slug":"modal"},{"level":3,"title":"Flexible Heading","slug":"flexible-heading"},{"level":3,"title":"Table","slug":"table"},{"level":3,"title":"Form","slug":"form"},{"level":3,"title":"Navigation","slug":"navigation"},{"level":3,"title":"Button","slug":"button"},{"level":3,"title":"Footer","slug":"footer"},{"level":3,"title":"Picture","slug":"picture"},{"level":3,"title":"Animation Tips","slug":"animation-tips"},{"level":3,"title":"Resizable Component","slug":"resizable-component"},{"level":3,"title":"Slides","slug":"slides"},{"level":3,"title":"Layout","slug":"layout"},{"level":3,"title":"Geometry","slug":"geometry"},{"level":2,"title":"CSS Variables","slug":"css-variables"},{"level":3,"title":"Variables DOM API","slug":"variables-dom-api"},{"level":3,"title":"Scope Variables","slug":"scope-variables"},{"level":3,"title":"Invalid and Empty Value in CSS Variables","slug":"invalid-and-empty-value-in-css-variables"},{"level":2,"title":"SVG","slug":"svg"},{"level":3,"title":"Inline SVG","slug":"inline-svg"},{"level":3,"title":"SVG Basic Shape Tags","slug":"svg-basic-shape-tags"},{"level":3,"title":"SVG Text Tag","slug":"svg-text-tag"},{"level":3,"title":"SVG Accessibility Tags","slug":"svg-accessibility-tags"},{"level":3,"title":"SVG Group Tag","slug":"svg-group-tag"},{"level":3,"title":"SVG Defs Tag","slug":"svg-defs-tag"},{"level":3,"title":"SVG Mask","slug":"svg-mask"},{"level":2,"title":"StyleLint Tool","slug":"stylelint-tool"},{"level":2,"title":"Tailwind","slug":"tailwind"},{"level":3,"title":"Tailwind Directives","slug":"tailwind-directives"}],"relativePath":"web/css/cssBasicNotes.md","lastUpdated":1627725673000}',o={},c=t("h1",{id:"css-basic-notes"},[t("a",{class:"header-anchor",href:"#css-basic-notes","aria-hidden":"true"},"#"),e(" CSS Basic Notes")],-1),p=t("p",null,[t("div",{class:"table-of-contents"},[t("ul",null,[t("li",null,[t("a",{href:"#css-basic-notes"},"CSS Basic Notes"),t("ul",null,[t("li",null,[t("a",{href:"#cascading-and-inheritance"},"Cascading and Inheritance")]),t("li",null,[t("a",{href:"#property-value"},"Property Value")]),t("li",null,[t("a",{href:"#property-order"},"Property Order")]),t("li",null,[t("a",{href:"#naming-convention"},"Naming Convention")]),t("li",null,[t("a",{href:"#css-selector"},"CSS Selector")]),t("li",null,[t("a",{href:"#css-normalize"},"CSS Normalize")]),t("li",null,[t("a",{href:"#css-box"},"CSS Box")]),t("li",null,[t("a",{href:"#box-model"},"Box Model")]),t("li",null,[t("a",{href:"#block-formatting-context"},"Block Formatting Context")]),t("li",null,[t("a",{href:"#stack-context"},"Stack Context")]),t("li",null,[t("a",{href:"#float-patterns"},"Float Patterns")]),t("li",null,[t("a",{href:"#position-patterns"},"Position Patterns")]),t("li",null,[t("a",{href:"#flex-patterns"},"Flex Patterns")]),t("li",null,[t("a",{href:"#grid-patterns"},"Grid Patterns")]),t("li",null,[t("a",{href:"#column-patterns"},"Column Patterns")]),t("li",null,[t("a",{href:"#centering-patterns"},"Centering Patterns")]),t("li",null,[t("a",{href:"#css-logical-properties-and-values"},"CSS Logical Properties and Values")]),t("li",null,[t("a",{href:"#css-scrollbar"},"CSS ScrollBar")]),t("li",null,[t("a",{href:"#css-opacity"},"CSS Opacity")]),t("li",null,[t("a",{href:"#css-colors"},"CSS Colors")]),t("li",null,[t("a",{href:"#css-border"},"CSS Border")]),t("li",null,[t("a",{href:"#css-background"},"CSS Background")]),t("li",null,[t("a",{href:"#css-text"},"CSS Text")]),t("li",null,[t("a",{href:"#css-font"},"CSS Font")]),t("li",null,[t("a",{href:"#css-counter"},"CSS Counter")]),t("li",null,[t("a",{href:"#css-filter"},"CSS Filter")]),t("li",null,[t("a",{href:"#css-interactive"},"CSS Interactive")]),t("li",null,[t("a",{href:"#object-position-and-fit"},"Object Position and Fit")]),t("li",null,[t("a",{href:"#animation"},"Animation")]),t("li",null,[t("a",{href:"#responsive-design"},"Responsive Design")]),t("li",null,[t("a",{href:"#media-query"},"Media Query")]),t("li",null,[t("a",{href:"#accessibility"},"Accessibility")]),t("li",null,[t("a",{href:"#awesome-components"},"Awesome Components")]),t("li",null,[t("a",{href:"#css-variables"},"CSS Variables")]),t("li",null,[t("a",{href:"#svg"},"SVG")]),t("li",null,[t("a",{href:"#stylelint-tool"},"StyleLint Tool")]),t("li",null,[t("a",{href:"#tailwind"},"Tailwind")])])])])])],-1),u=t("h2",{id:"cascading-and-inheritance"},[t("a",{class:"header-anchor",href:"#cascading-and-inheritance","aria-hidden":"true"},"#"),e(" Cascading and Inheritance")],-1),r=t("h3",{id:"cascading-order"},[t("a",{class:"header-anchor",href:"#cascading-order","aria-hidden":"true"},"#"),e(" Cascading Order")],-1),i=t("ol",null,[t("li",null,"user agent normal"),t("li",null,"user normal"),t("li",null,"author normal"),t("li",null,"CSS Animations"),t("li",null,"author !important"),t("li",null,"user !important"),t("li",null,"user agent !important")],-1),k=t("h3",{id:"specificity"},[t("a",{class:"header-anchor",href:"#specificity","aria-hidden":"true"},"#"),e(" Specificity")],-1),b=t("p",null,[e("Specificity (Selector Priority) has 4 bits - thousands, hundreds, tens, ones "),t("code",null,"0000"),e(":")],-1),m=t("ul",null,[t("li",null,"thousands: inline-style"),t("li",null,"hundreds: ID selector"),t("li",null,"tens: class selector, attribute selector, pseudo-class(😃"),t("li",null,"ones: element selector, pseudo-element(:😃")],-1),d=t("blockquote",null,[t("p",null,"Universal selector (*), combinators (+, >, ~, ' ') negation pseudo-class (:not) have no effect on specificity, but selectors in it have effect on specificity")],-1),g=t("div",{class:"language-scss line-numbers-mode"},[t("pre",null,[t("code",null,[t("span",{class:"token selector"},"h1 "),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"specificity"),t("span",{class:"token punctuation"},":"),e(" 0001"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token selector"},"#id "),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"specificity"),t("span",{class:"token punctuation"},":"),e(" 0100"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token selector"},"h1 + p::first-letter "),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"specificity"),t("span",{class:"token punctuation"},":"),e(" 0003"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token selector"},"li > a[href*='en-US'] > .inline-warning "),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"specificity"),t("span",{class:"token punctuation"},":"),e(" 0022"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token comment"},'/*

Hello

*/'),e("\n"),t("span",{class:"token selector"},"inline-style "),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"specificity"),t("span",{class:"token punctuation"},":"),e(" 1000"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n")])]),t("div",{class:"line-numbers-wrapper"},[t("span",{class:"line-number"},"1"),t("br"),t("span",{class:"line-number"},"2"),t("br"),t("span",{class:"line-number"},"3"),t("br"),t("span",{class:"line-number"},"4"),t("br"),t("span",{class:"line-number"},"5"),t("br"),t("span",{class:"line-number"},"6"),t("br"),t("span",{class:"line-number"},"7"),t("br"),t("span",{class:"line-number"},"8"),t("br"),t("span",{class:"line-number"},"9"),t("br"),t("span",{class:"line-number"},"10"),t("br"),t("span",{class:"line-number"},"11"),t("br"),t("span",{class:"line-number"},"12"),t("br"),t("span",{class:"line-number"},"13"),t("br"),t("span",{class:"line-number"},"14"),t("br"),t("span",{class:"line-number"},"15"),t("br"),t("span",{class:"line-number"},"16"),t("br"),t("span",{class:"line-number"},"17"),t("br"),t("span",{class:"line-number"},"18"),t("br"),t("span",{class:"line-number"},"19"),t("br"),t("span",{class:"line-number"},"20"),t("br")])],-1),h=t("div",{class:"language-css line-numbers-mode"},[t("pre",null,[t("code",null,[t("span",{class:"token comment"},"/* specificity: 0101 */"),e("\n"),t("span",{class:"token selector"},"#outer a"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"background-color"),t("span",{class:"token punctuation"},":"),e(" red"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token comment"},"/* specificity: 0201 */"),e("\n"),t("span",{class:"token comment"},"/* win */"),e("\n"),t("span",{class:"token selector"},"#outer #inner a"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"background-color"),t("span",{class:"token punctuation"},":"),e(" blue"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token comment"},"/* specificity: 0104 */"),e("\n"),t("span",{class:"token selector"},"#outer div ul li a"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"color"),t("span",{class:"token punctuation"},":"),e(" yellow"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token comment"},"/* specificity: 0113 */"),e("\n"),t("span",{class:"token comment"},"/* win */"),e("\n"),t("span",{class:"token selector"},"#outer div ul .nav a"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"color"),t("span",{class:"token punctuation"},":"),e(" white"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token comment"},"/* specificity: 0024 */"),e("\n"),t("span",{class:"token selector"},"div div li:nth-child(2) a:hover"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"border"),t("span",{class:"token punctuation"},":"),e(" 10px solid black"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token comment"},"/* specificity: 0023 */"),e("\n"),t("span",{class:"token selector"},"div li:nth-child(2) a:hover"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"border"),t("span",{class:"token punctuation"},":"),e(" 10px dashed black"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token comment"},"/* specificity: 0033 */"),e("\n"),t("span",{class:"token comment"},"/* win */"),e("\n"),t("span",{class:"token selector"},"div div .nav:nth-child(2) a:hover"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"border"),t("span",{class:"token punctuation"},":"),e(" 10px double black"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n")])]),t("div",{class:"line-numbers-wrapper"},[t("span",{class:"line-number"},"1"),t("br"),t("span",{class:"line-number"},"2"),t("br"),t("span",{class:"line-number"},"3"),t("br"),t("span",{class:"line-number"},"4"),t("br"),t("span",{class:"line-number"},"5"),t("br"),t("span",{class:"line-number"},"6"),t("br"),t("span",{class:"line-number"},"7"),t("br"),t("span",{class:"line-number"},"8"),t("br"),t("span",{class:"line-number"},"9"),t("br"),t("span",{class:"line-number"},"10"),t("br"),t("span",{class:"line-number"},"11"),t("br"),t("span",{class:"line-number"},"12"),t("br"),t("span",{class:"line-number"},"13"),t("br"),t("span",{class:"line-number"},"14"),t("br"),t("span",{class:"line-number"},"15"),t("br"),t("span",{class:"line-number"},"16"),t("br"),t("span",{class:"line-number"},"17"),t("br"),t("span",{class:"line-number"},"18"),t("br"),t("span",{class:"line-number"},"19"),t("br"),t("span",{class:"line-number"},"20"),t("br"),t("span",{class:"line-number"},"21"),t("br"),t("span",{class:"line-number"},"22"),t("br"),t("span",{class:"line-number"},"23"),t("br"),t("span",{class:"line-number"},"24"),t("br"),t("span",{class:"line-number"},"25"),t("br"),t("span",{class:"line-number"},"26"),t("br"),t("span",{class:"line-number"},"27"),t("br"),t("span",{class:"line-number"},"28"),t("br"),t("span",{class:"line-number"},"29"),t("br"),t("span",{class:"line-number"},"30"),t("br"),t("span",{class:"line-number"},"31"),t("br"),t("span",{class:"line-number"},"32"),t("br"),t("span",{class:"line-number"},"33"),t("br"),t("span",{class:"line-number"},"34"),t("br"),t("span",{class:"line-number"},"35"),t("br"),t("span",{class:"line-number"},"36"),t("br"),t("span",{class:"line-number"},"37"),t("br")])],-1),f=t("p",null,"Styles for a directly targeted element will always take precedence over inherited styles, regardless of the specificity of the inherited rule",-1),y=t("div",{class:"language-css line-numbers-mode"},[t("pre",null,[t("code",null,[t("span",{class:"token selector"},"#parent"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"color"),t("span",{class:"token punctuation"},":"),e(" green"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n\n"),t("span",{class:"token comment"},"/*

element will be purple */"),e("\n"),t("span",{class:"token selector"},"h1"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"color"),t("span",{class:"token punctuation"},":"),e(" purple"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n")])]),t("div",{class:"line-numbers-wrapper"},[t("span",{class:"line-number"},"1"),t("br"),t("span",{class:"line-number"},"2"),t("br"),t("span",{class:"line-number"},"3"),t("br"),t("span",{class:"line-number"},"4"),t("br"),t("span",{class:"line-number"},"5"),t("br"),t("span",{class:"line-number"},"6"),t("br"),t("span",{class:"line-number"},"7"),t("br"),t("span",{class:"line-number"},"8"),t("br")])],-1),v=t("h3",{id:"css-inheritance"},[t("a",{class:"header-anchor",href:"#css-inheritance","aria-hidden":"true"},"#"),e(" CSS Inheritance")],-1),x=t("ul",null,[t("li",null,"Most CSS properties that affect the text node are inherited properties: color, font-size, font-family, etc."),t("li",null,"Most CSS properties that affect the element node are non-inherited properties."),t("li",null,[e("When the "),t("code",null,"unset"),e(" value is set on an inherited property, it resets the property value to its inherited value.")]),t("li",null,[t("code",null,"unset"),e(" value resets a non-inherited property to its "),t("code",null,"initial"),e(" value.")]),t("li",null,[t("code",null,"revert"),e(" reverses the CSS default values to the browser user-agent styles.")])],-1),w=t("h4",{id:"inherited-css-property"},[t("a",{class:"header-anchor",href:"#inherited-css-property","aria-hidden":"true"},"#"),e(" Inherited CSS Property")],-1),S=t("ul",null,[t("li",null,"all elements: visibility, cursor."),t("li",null,"inline elements: letter-spacing, word-spacing, white-space, line-height, color, font, font-family, font-size, font-style, font-variant, font-weight, text-decoration, text-transform, direction"),t("li",null,"block elements: text-indent, text-align"),t("li",null,"list elements: list-style, list-style-type, list-style-position, list-style-image"),t("li",null,"table elements: border-collapse")],-1),C=t("h2",{id:"property-value"},[t("a",{class:"header-anchor",href:"#property-value","aria-hidden":"true"},"#"),e(" Property Value")],-1),q=t("h3",{id:"initial-value"},[t("a",{class:"header-anchor",href:"#initial-value","aria-hidden":"true"},"#"),e(" Initial Value")],-1),F=t("p",null,"The initial value of a CSS property is its default value, as listed in its definition table",-1),z=t("h3",{id:"specified-value"},[t("a",{class:"header-anchor",href:"#specified-value","aria-hidden":"true"},"#"),e(" Specified Value")],-1),P=t("p",null,"The specified value of a CSS property is the value it receives from the document's style sheet",-1),T=t("h3",{id:"computed-value"},[t("a",{class:"header-anchor",href:"#computed-value","aria-hidden":"true"},"#"),e(" Computed Value")],-1),B=t("p",null,"The computed value of a CSS property is the value that is transferred from parent to child during inheritance. It is calculated from the specified value by:",-1),M=t("ol",null,[t("li",null,[e("Handling the special values "),t("code",null,"inherit"),e(", "),t("code",null,"initial"),e(", "),t("code",null,"unset"),e(", and "),t("code",null,"revert")]),t("li",null,'Doing the computation needed to reach the value described in the "Computed value" line in the property\'s definition table')],-1),_=t("h3",{id:"used-value"},[t("a",{class:"header-anchor",href:"#used-value","aria-hidden":"true"},"#"),e(" Used Value")],-1),j=t("p",null,"The used value of a CSS property is its value after all calculations have been performed on the computed value:",-1),A=t("ul",null,[t("li",null,"The used values of dimensions (e.g., width, line-height) are in pixels"),t("li",null,"The used values of shorthand properties (e.g., background) are consistent with those of their component properties (e.g., background-color or background-size) and with position and float")],-1),L=t("h3",{id:"actual-value"},[t("a",{class:"header-anchor",href:"#actual-value","aria-hidden":"true"},"#"),e(" Actual Value")],-1),V=t("p",null,"The actual value of a CSS property is the used value of that property after any necessary approximations have been applied",-1),G=t("p",null,"The user agent performs four steps to calculate a property's actual (final) value:",-1),D=t("ol",null,[t("li",null,"the specified value is determined based on the result of cascading, inheritance, or using the initial value."),t("li",null,"the computed value is calculated according to the specification (for example, a span with position: absolute will have its computed display changed to block)"),t("li",null,"layout is calculated, resulting in the used value"),t("li",null,"the used value is transformed according to the limitations of the local environment, resulting in the actual value")],-1),I=t("blockquote",null,[t("p",null,"initial -> specified -> computed -> used -> actual value")],-1),E=t("div",{class:"language-css line-numbers-mode"},[t("pre",null,[t("code",null,[t("span",{class:"token selector"},"span"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"position"),t("span",{class:"token punctuation"},":"),e(" absolute"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e(),t("span",{class:"token comment"},"/* display computed to `block` */"),e("\n")])]),t("div",{class:"line-numbers-wrapper"},[t("span",{class:"line-number"},"1"),t("br"),t("span",{class:"line-number"},"2"),t("br"),t("span",{class:"line-number"},"3"),t("br")])],-1),H=t("h2",{id:"property-order"},[t("a",{class:"header-anchor",href:"#property-order","aria-hidden":"true"},"#"),e(" Property Order")],-1),N=t("h3",{id:"property-order-summary"},[t("a",{class:"header-anchor",href:"#property-order-summary","aria-hidden":"true"},"#"),e(" Property Order Summary")],-1),O=t("blockquote",null,[t("p",null,"显示属性 -> 自身属性 -> 文本属性")],-1),R=t("h4",{id:"display-property"},[t("a",{class:"header-anchor",href:"#display-property","aria-hidden":"true"},"#"),e(" Display Property")],-1),X=t("ul",null,[t("li",null,"position"),t("li",null,"display"),t("li",null,"float"),t("li",null,"clear")],-1),Y=t("h4",{id:"self-property"},[t("a",{class:"header-anchor",href:"#self-property","aria-hidden":"true"},"#"),e(" Self Property")],-1),W=t("ul",null,[t("li",null,"width"),t("li",null,"height"),t("li",null,"margin"),t("li",null,"padding"),t("li",null,"border"),t("li",null,"list-style"),t("li",null,"background")],-1),U=t("h4",{id:"text-property"},[t("a",{class:"header-anchor",href:"#text-property","aria-hidden":"true"},"#"),e(" Text Property")],-1),Z=t("ul",null,[t("li",null,"color"),t("li",null,"font"),t("li",null,"text-decoration"),t("li",null,"text-align"),t("li",null,"vertical-align"),t("li",null,"white-space"),t("li",null,"other text"),t("li",null,"content")],-1),J=t("h3",{id:"property-order-details"},[t("a",{class:"header-anchor",href:"#property-order-details","aria-hidden":"true"},"#"),e(" Property Order Details")],-1),Q=t("h4",{id:"display-property-details"},[t("a",{class:"header-anchor",href:"#display-property-details","aria-hidden":"true"},"#"),e(" Display Property Details")],-1),K=t("ul",null,[t("li",null,"position"),t("li",null,"z-index"),t("li",null,"top"),t("li",null,"right"),t("li",null,"bottom"),t("li",null,"left"),t("li",null,"box-sizing"),t("li",null,"display"),t("li",null,"visibility"),t("li",null,"float"),t("li",null,"clear")],-1),$=t("h4",{id:"self-property-details"},[t("a",{class:"header-anchor",href:"#self-property-details","aria-hidden":"true"},"#"),e(" Self Property Details")],-1),nn=t("ul",null,[t("li",null,"width"),t("li",null,"min-width"),t("li",null,"max-width"),t("li",null,"height"),t("li",null,"min-height"),t("li",null,"max-height"),t("li",null,"overflow"),t("li",null,"margin"),t("li",null,"margin-top"),t("li",null,"margin-right"),t("li",null,"margin-bottom"),t("li",null,"margin-left"),t("li",null,"padding"),t("li",null,"padding-top"),t("li",null,"padding-right"),t("li",null,"padding-bottom"),t("li",null,"padding-left"),t("li",null,"border"),t("li",null,"border-width"),t("li",null,"border-top-width"),t("li",null,"border-right-width"),t("li",null,"border-bottom-width"),t("li",null,"border-left-width"),t("li",null,"border-style"),t("li",null,"border-top-style"),t("li",null,"border-right-style"),t("li",null,"border-bottom-style"),t("li",null,"border-left-style"),t("li",null,"border-color"),t("li",null,"border-top-color"),t("li",null,"border-right-color"),t("li",null,"border-bottom-color"),t("li",null,"border-left-color"),t("li",null,"outline"),t("li",null,"list-style"),t("li",null,"table-layout"),t("li",null,"caption-side"),t("li",null,"border-collapse"),t("li",null,"border-spacing"),t("li",null,"empty-cells"),t("li",null,"background"),t("li",null,"background-color"),t("li",null,"background-image"),t("li",null,"background-repeat"),t("li",null,"background-position")],-1),sn=t("h4",{id:"text-property-details"},[t("a",{class:"header-anchor",href:"#text-property-details","aria-hidden":"true"},"#"),e(" Text Property Details")],-1),an=t("ul",null,[t("li",null,"color"),t("li",null,"font"),t("li",null,"font-family"),t("li",null,"font-size"),t("li",null,"font-weight"),t("li",null,"line-height"),t("li",null,"text-align"),t("li",null,"text-indent"),t("li",null,"text-transform"),t("li",null,"text-decoration"),t("li",null,"letter-spacing"),t("li",null,"word-spacing"),t("li",null,"white-space"),t("li",null,"vertical-align"),t("li",null,"opacity"),t("li",null,"cursor"),t("li",null,"content"),t("li",null,"quotes")],-1),tn=t("h2",{id:"naming-convention"},[t("a",{class:"header-anchor",href:"#naming-convention","aria-hidden":"true"},"#"),e(" Naming Convention")],-1),en=t("h3",{id:"layout-structure-naming-convention"},[t("a",{class:"header-anchor",href:"#layout-structure-naming-convention","aria-hidden":"true"},"#"),e(" Layout Structure Naming Convention")],-1),ln=t("ul",null,[t("li",null,"容器: container"),t("li",null,"页头:header"),t("li",null,"内容:content/container"),t("li",null,"页面主体:main"),t("li",null,"页尾:footer"),t("li",null,"导航:nav"),t("li",null,"侧栏:sidebar"),t("li",null,"栏目:column"),t("li",null,"页面外围控制整体佈局宽度:wrapper"),t("li",null,"左右中:left right center")],-1),on=t("h3",{id:"navigation-naming-convention"},[t("a",{class:"header-anchor",href:"#navigation-naming-convention","aria-hidden":"true"},"#"),e(" Navigation Naming Convention")],-1),cn=t("ul",null,[t("li",null,"导航:nav"),t("li",null,"主导航:main-nav"),t("li",null,"子导航:sub-nav"),t("li",null,"顶导航:top-nav"),t("li",null,"边导航:sidebar"),t("li",null,"左导航:left-sidebar"),t("li",null,"右导航:right-sidebar"),t("li",null,"菜单:menu"),t("li",null,"子菜单:sub-menu"),t("li",null,"标题: title"),t("li",null,"摘要: summary")],-1),pn=t("h3",{id:"functional-component-naming-convention"},[t("a",{class:"header-anchor",href:"#functional-component-naming-convention","aria-hidden":"true"},"#"),e(" Functional Component Naming Convention")],-1),un=t("ul",null,[t("li",null,"标志:logo"),t("li",null,"广告:banner"),t("li",null,"登陆:login"),t("li",null,"登录条:login-bar"),t("li",null,"注册:register"),t("li",null,"搜索:search"),t("li",null,"功能区:shop"),t("li",null,"标题:title"),t("li",null,"加入:join us"),t("li",null,"状态:status"),t("li",null,"按钮:btn"),t("li",null,"滚动:scroll"),t("li",null,"标籤页:tab"),t("li",null,"文章列表:list"),t("li",null,"提示信息:msg"),t("li",null,"当前的: current"),t("li",null,"小技巧:tips"),t("li",null,"图标: icon"),t("li",null,"注释:note"),t("li",null,"指南:guild"),t("li",null,"服务:service"),t("li",null,"热点:hot"),t("li",null,"新闻:news"),t("li",null,"下载:download"),t("li",null,"投票:vote"),t("li",null,"合作伙伴:partner"),t("li",null,"友情链接:link"),t("li",null,"版权:copyright")],-1),rn=t("h3",{id:"css-files-naming-convention"},[t("a",{class:"header-anchor",href:"#css-files-naming-convention","aria-hidden":"true"},"#"),e(" CSS Files Naming Convention")],-1),kn=t("ul",null,[t("li",null,[e("abstracts: "),t("code",null,"$variables"),e(", "),t("code",null,"@mixin"),e(" function")]),t("li",null,"vendors: external libraries (font-awesome, bootstrap)"),t("li",null,"base: normalize.css, reset.css, utils.css, font.css, base.css (margin-right, text-center, float-right)"),t("li",null,"components: form.css, button.css, navbar.css, dropdown.css"),t("li",null,"layout: columns.css, grid.css, header.css, footer.css, section.css, navigation.css"),t("li",null,"pages: home.css, about.css"),t("li",null,"themes: color.css, font.css"),t("li",null,"main.css")],-1),bn=t("h2",{id:"css-selector"},[t("a",{class:"header-anchor",href:"#css-selector","aria-hidden":"true"},"#"),e(" CSS Selector")],-1),mn=t("p",null,[t("img",{src:"/awesome-notes/assets/CSS3SelectorList.c7ec6bba.png",alt:"CSS3 Selector"})],-1),dn=t("h3",{id:"元素选择器"},[t("a",{class:"header-anchor",href:"#元素选择器","aria-hidden":"true"},"#"),e(" 元素选择器")],-1),gn=t("div",{class:"language-css line-numbers-mode"},[t("pre",null,[t("code",null,[t("span",{class:"token selector"},"p"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"margin-bottom"),t("span",{class:"token punctuation"},":"),e(" 1em"),t("span",{class:"token punctuation"},";"),e("\n "),t("span",{class:"token property"},"line-height"),t("span",{class:"token punctuation"},":"),e(" 1.5em"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n")])]),t("div",{class:"line-numbers-wrapper"},[t("span",{class:"line-number"},"1"),t("br"),t("span",{class:"line-number"},"2"),t("br"),t("span",{class:"line-number"},"3"),t("br"),t("span",{class:"line-number"},"4"),t("br")])],-1),hn=t("h3",{id:"关系选择器"},[t("a",{class:"header-anchor",href:"#关系选择器","aria-hidden":"true"},"#"),e(" 关系选择器")],-1),fn=t("h4",{id:"descendant-selectors"},[t("a",{class:"header-anchor",href:"#descendant-selectors","aria-hidden":"true"},"#"),e(" Descendant Selectors")],-1),yn=t("ul",null,[t("li",null,[t("code",null,"E F"),e(":所有后代选择器")]),t("li",null,[t("code",null,"E > F"),e(":直接子选择器")])],-1),vn=t("div",{class:"language-css line-numbers-mode"},[t("pre",null,[t("code",null,[t("span",{class:"token selector"},"ul li"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"margin-bottom"),t("span",{class:"token punctuation"},":"),e(" 0.5em"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e("\n")])]),t("div",{class:"line-numbers-wrapper"},[t("span",{class:"line-number"},"1"),t("br"),t("span",{class:"line-number"},"2"),t("br"),t("span",{class:"line-number"},"3"),t("br")])],-1),xn=t("div",{class:"language-css line-numbers-mode"},[t("pre",null,[t("code",null,[t("span",{class:"token selector"},"ul > li"),e(),t("span",{class:"token punctuation"},"{"),e("\n "),t("span",{class:"token property"},"list-style"),t("span",{class:"token punctuation"},":"),e(" none"),t("span",{class:"token punctuation"},";"),e("\n"),t("span",{class:"token punctuation"},"}"),e(),t("span",{class:"token comment"},"/* 仅限ul的直接子元素li,忽略嵌套子元素 */"),e("\n")])]),t("div",{class:"line-numbers-wrapper"},[t("span",{class:"line-number"},"1"),t("br"),t("span",{class:"line-number"},"2"),t("br"),t("span",{class:"line-number"},"3"),t("br")])],-1),wn=t("p",null,"Using the descendant selector without more specificity can be really expensive. The browser is going to check every descendant element for a match because the relationship isn’t restricted to parent and child.",-1),Sn=t("p",null,[e("For "),t("code",null,".container ul li a"),e(" selector:")],-1),Cn=t("ul",null,[t("li",null,[e("match every "),t("code",null,""),e(" on the page")]),t("li",null,[e("find every "),t("code",null,""),e(" contained in a "),t("code",null,"
  • ")]),t("li",null,[e("use the previous matches and narrow down to the ones contained in a "),t("code",null,"