array_reduceの話2

array_reduceと arrayのsplat オペレーターを使うと、同じアレーを生成するという意味のない関数ができる。

$source_array=[3,2,4,5];
$nop_array=array_reduce($source_array, fn($carry,$item)=>[...$carry, $item],[]);
print_r($nop_array); //array=>[3,2,4,5]

で、パターンを使って、array_mapもarray_filterもarray_reduceで代用できるというお話



// array reduce to mimic array_map
$source_array=[3,2,4,5];
$double=function($x){
    return $x*2;
};
$mapped_array=array_reduce($source_array,fn($carry,$item)=>[...$carry,$double($item)],[]);

print_r($mapped_array); //array=>[6,4,8,10]

//array reduce to mimic array_filter

$even_fn= function($x){ //return true if even
   return ($x % 2)==0;
};


$filtered_array=array_reduce($source_array,
    fn($carry,$item)=>$even_fn($item)?[...$carry,$item]:$carry,
    []);


print_r($filtered_array); //array=>[2,4]


めでたし、めでたし

array_reduceの話

PHPでプログラミングするとき、例えば、以下のようなアレィがあったとする

$fields=array("a"=>"A", "b"=>"B", "c"=>"C", "d"=>"D")

この内容をキーとともにリストアップしたいとき、まず思いつくのがforeachを使ってこう書く方法

$output="<h3>listing array with foreach</h3>";
foreach($fields as $key=>$value){
    $output.="<pre>".$key.":"$value."</pre>"
}
echo $output;

ただ、このアレィの複数エレメントを一つの値(この場合は一つの文字列)にまとめるという風に考えるとarray_reduceという関数を使っても同じことができる

$output="<h3>Listing array with array_reduce</h3>";
$output=array_reduce(array_keys($fields),fn($curry,$item)=>$curry."<pre>".$item.":".$fields[$item]."</pre>",$output
);
echo $output;

この関数、単純なArray処理は問題ないとして、Associative Arrayを処理するとなると、はたと困るが、渡すArrayを値ではなく、キーを渡すことで可能となる。これは一つ目の因数にarray_keysを使うことで実現できる。コールバックには因数が二つ付くが、慣習として$curry と$itemという変数名を使う。for 文に$iをつかっているのと同様で、もちろんほかの変数名としても問題ない。$curryは結果を集積していく入れ物。$itemはアレィの各エレメント。上の例ではコールバック関数はアローファンクションを使ってインラインの書き込み。 三つ目の因数($output)は初期値

このarray_reduceの使いかたをネットの検索などで探すと、数字を加算していくような例がよく紹介されているが、上のように文字列アレィの加工にも有効。 さらに、このアレィは関数のアレィでもよいわけで、例えば、複数の関数で同じ文字列の処理を繰り返すような場合、例えば、文字列をトリムしあとhtmlにエンコードしたい場合など、つまり同じ文字列に何度もパスをかけて加工したい場合など

$output=trim($output);
$output=htmlspecialchars($output);
//または
$output=htmlspecialchars(trim($output));

のかわりに

$output= array_reduce([trim,htmlspecialchars], fn($curry,$item)=>$item($curry),$output)

と書いてしまう技がある。関数が二つなら最初のアプローチのほうがよさそうだが、かかわる関数が増えてくると、コードのメンテナンスは下のパターンのほうがすっきりするよ、とFunctional Programmingの推進者たちはいっているが、さてどうする。

$functionsArray=[fn1,fn2,fn3,fn4,fn5]; //同じオブジェクトに対し、5つの関数処理をおこないたい
$result=array_reduce($functionArray, fn($curry,$item)=>$item($curry),$initialValue);

“Why did I not think of this?” moment in MODX 3 snippet development with PHPStorm

This is one of those ‘Aha!’ moments.

MODX is one of CMSs (content management system) that I have been using for a long time to maintain my shogi site. Market penetration of this CMS is said to be 0.1%. As of this writing, 43% of of Web sites uses WordPress and no other CMS reach even 10% of market share. (source, w3techs.com . I actually think MODX penetration is much greater than this number, MODX does not leave big footprint to show it’s identity in front of web crawlers. -Many sites I know powered by MODX, including mine – were identified as non-CMS site by w3tech’s tool.— Nevertheless, there is no doubt WordPress dominates the CMS market)

Although both uses same language (PHP) for powering the site, MODX gives much more flexibility in site design but you need to be proficient with HTML and CSS (and JavaScript). In another word, MODX is a tool while WordPress is a product.

What I liked about MODX over WordPress at the time was a separation of PHP code and HTML. This was a long time ago so the situation might have changed on WordPress, but I have been happy with MODX since.

At the time, I also looked at Drupal and Joomra . All of them forces you to follow their interpretation of what website should be. MODX has no opinion on how you want to create Web site. You have a total freedom.

On the other hand, if the only thing you want to do with your site is blogging, there is no better solution than using WordPress.

I digressed. Back to the subject!

With a wealth of plug-ins available, your rarely need to code in PHP. When you do though, MODX separates a PHP coding part from HTML with a thing called Snippets. A snippet is then inserted in to HTML with a use of tag.

[[aSnippet]]

You can edit a snippet within MODX backend’s editor. With a plugin like ‘ACE’ there is a nominal amount of syntax checking to the PHP code. Unfortunately, I am being spoiled with convenience of IDE such as PHPStorm,

MODX backend, Snippet editor with ACE plugin

So there is a desire to be able to write a snippet code using PHPStorm and get benefits of auto-completion and methods hinting, strict error checking and warning.

Unfortunately, there is no plugin in PHPStorm for MODX or MODX plugn for PHPStorm.

When I google “MODX with PHPStorm”, there are several hits, Although none of them gave me a direct answer on what I wanted to do, I was able to put enough facts together and came up with a solution. The solution that was just waiting for me to assemble few known facts, that I have used all of them separately in the past!


1. Fact that PHPstorm can create a project using existing remote site contents, so that PHPStorm can pull all file setup from MODX site.

PHPStorm->File->New Project from Existing Files


2. Fact that PHPstorm can then start syncing file contents of my local project files to server files.

File sync options available in PHPStorm

Ok, but Snippet is stored in Database. the code is not in the file, but then,

3. Fact, MODX snippet code can be from a static file, meaning I can create a PHP file and feed it as a snippet to MODX by turning on “is Static” option and point to the file location

Is Static option is turned on for Snippet. Snippet is now fed from “static file”



Result: When I combine those 3 factors, I can create a snippet with a comfort of PHPStorm IDE environment.

Snippet now editable in PHPStorm
Snippet output on browser page, powered through MODX

What do I gain? Full context support and code hinting, as PHPStorm analyzes whole MODX site and figures out all variables and object and its methods being used inside MODX. I have to give credit to MODX development team for fully documenting source files. Thank you!

PHPstorm explains what getOption method is
PHPStorm autocomplete support in action.

You can also open the corresponding file by pointing to method and do control-b to drill down on function.

I edit a file in PHPStorm, and you either 1) turn on auto-sync between your local files and server files or 2) manually upload changed file to server. Then I can run and test the snippet with MODX backend immediately.

So all of the sudden, I feel like invincible. It will be very hard for me to make coding error. (Ok, logic error still possible)

Once Snippet development is done, you may switch off the ‘is static’ option and keep the snippet inside database, remove the php file from the system.

One thing that is kind of annoying is that by default, PHPstorm thinks $modx variable is undefined. The default setting on PHPStorm’s inspections on undefined variable is somewhat on conservative side. It will not even acknowledge variables from included file!. To mitigate this, you need to put a check on “Search for variable’s definition outside the current file”

PHP settings->Editor->Inspection->PHP->Undefined symbols->Undefined variable

It hurts to think that I have been using both PHPstorm and MODX for more than 10 years and I am figuring this out just now 🙁

Creating a Blog page inside MODX version 3 web site

MODX comes clean slate so there is no ‘ready to use’ provisions for creating Blog style site. One has to set it up using tools available in the CMS.

I took an easy way out for my production site by creating blog site using free wordPress service and then feed content to MODX page. with this, the only extra you will need is Spiefeed to handle rss feed from WordPress.  But for purist approach it is certainly possible to setup blog section within MODX using few extras.

There are several articles available for creating Blog sites using MODX.  The one shown in the modx official site is somewhat dated, but the article actually have a link to the instruction/article of more recent implementation of MODX blog site.  Link is here.   Better yet, this instruction is accompanied with a package published in the github which has all chunks, a snippet and a template packaged up to duplicate the blog site that the article discusses.  A demo site is also available to check out the feel of using this template. The document states that the demo utilizes MODX 2.7.  So can I use this setup with MODX 3 site? Furthermore, the instruction assumes you want the Site to be a blogging site. Can I add ‘Blog page’ to already existing MODX site? Answer to both questions are ‘Yes’ but we need to modify some parts.

To start, I could have created each chunk and template manually following the instruction but to reduce my typing effort, I went ahead and downloaded the demo package. I also installed dependent extras (getResources, getPage, Collections, and Tagger).  Previously, I did not have any of those installed. – I am using pdoTools  to get resources and get page. – <edit> It was fairly easy to change references to getResources and getPage to equivalent pdo* snippets.

Again, the setup instruction is written for MODX version 2.7 and the site I am setting up this blog structure is version 3.0.3.  Setup also assumes the top page is a site home page. There are several area in chunks and template that refers to [[++site_start]] and this need to be changed to point to correct Resource.  I want to have a blog page as a subpage of the site.

Looking through the template and snippets that I setup with the downloaded package , I found some more modifications were needed.

I will list out those changes.

  1. My server uses Nginx and it needs additional html tag to specify base url in <head> section of blog template html. Without it, Friendly URL won’t work correctly.
    <base href="[[++site_url]]">
  2. In MODX 3, [[*class_key]] does not return “modResource”.  It returns “MODX\Revolution\modResource”.
    This means I get “MODX/Revolution\modDocument” or “MODX\Revolution\CollectionContainer” in [[*class_key]] field depending on resource selected. Two sections of “Blog Template” stops working due to this.

    1. Template tries to pull in “jumbotron_CollectionContainer” or “jumbotron chunk based on [[$jumbotron-[[]*class_key]]] tag. This tag doesn’t work in MODX 3 as it returns [[$jumbotron-MODX/Revolution/….]] which does not exist and forward slash within chunk name is probably invalid anyway.
    2. Template tries to insert [[$blog_listing]] or [[$blog_article]] based on conditional modifier [[*id:is`[[++site_start]]`:then….]] but the blog_listing page is not at [[+site_start]] resources in my use case.
      1. To mitigate both problems, I have created a system setting [[++blog_start]] in custom namespace/area and set it to id of a blog home page resource I created. Then rewrote tags as follows.
        For jumbotron section
        [[[[*id:is=`[[++blog_start]]`:then=`$jumbotron-CollectionContainer`:else`$jumbotron-modDocument`]]]]
        For container section
        [[[[*id:is=`[[++blog_start]]`:then=`$blog_listing`:else=`$blog_article`]]]]
      2. Note: It will be probably not that difficult to create snippets and replace conditional tags here.
  3. getResources and getSnippet tags are used to aggregate child resources and parents properties are set to site home page [[++site_start]].   These needed to be changed to aforementioned [[++blog_start]].  These changes had to be made on $blog_listing chunk and $jumbotron-CollectionContainer chunk.
  4. @groups properties are hardcoded in calls to TaggerGetTags snippet.  The snippet is called from $aside chunk is setting @groups to 1 and the snippet called from $headder chunk is setting @groups to 2.  Make sure this agrees with your tagger group settings. I have first created ‘Category’ group and then ‘Archives’ group so I needed to change them to 2 and 1 respectively.
  5. Last and least 🙂   as this is just a demo template, links to “subscribe”, “search”, “Sign up”, “GitHub”, “Twitter” and “Facebook” are all mocked and pointing to itself.  You either need to remove or put actual link to them when modifying to suit your site design.

So, in order to create a Blog section within Modx 3 hosted website, using this instruction and elements in the github repo, One needs to be aware of:

  1. Modx version 3’s breaking change of the model class caused decision logics inside Blog_Template to stop working.  Rework of the logic flow was needed.
  2. Friendly url.  Nginx needs base href reference in head section.
  3. Creating a blog section other than home page location. Needed to change references to [[++site_start]] system settings to [[++blog_start]], a user created system setting
  4. Adjust the template design to fit with other area of site design.

Some more miscellaneous items to note.

  1. $blog_listing_item chunk, content placeholder has a modifier :firstp. There is a firstp snippet that is installed with the package.  I opted to replace it with ellipsis=`500` modifier to limit the content output to first 500 chars followed by ellipsis to suit my preference.
  2. Also in $blog_listing, getPage snippet uses @blog properties set. This set does not exists so the default property set will be used instead. I think idea is to create a blog property set within getPage snippet to override the default property settings in easy way, if customization is needed.

Commenting on the posts are not possible.  This need to be setup by yourself and will require Quip extra. that part is not covered by this article. One has to go back to modx documentation’s tutorial section.

 

factory function?

JavaScriptやPHPで、Function Factoryという言葉を見るのだが、これは共通するパターンのFunctionを生成するためのFunctionということで、例えば

$double = function($x){
    return $x*2;
};

$triple = function($x){
    return $x*3;
};

$quadruple = function($x){
     return $x*4;
};

なんてのは、三つのfunctionを定義していても、よく見ると、2,3,4とかける数字が違うだけで、あとはまったく同じパターン。 このようなfunctionの記述を効率よく行うために

// This is function factory
$create_multiplier = function($y){
    return function($x) use ($y) {
        return $x * $y;
    };
};
//use the function factory to create three functions.
$double = $create_multiplier(2);
$triple = $create_multiplier(3);
$quadruple = $create_multiplier(4);

というような書き方をするテクニック。肝心な部分の記述は一回で済むためコードの管理も簡単になる。

上のコードは返すコードのなかで、$xを認識させるため、 use ( $x )というクローズをいれているが、PHP7.4 からPHPでもアローファンクションが使えるようになり、これだとuse というkeyword無しで、外側で設定されている変数を認識するようになる。ので、

$create_multiplier = function($y){ return fn($x)=>$x*$y;};

//use function factory to create three functions.

$double = $create_multiplier(2);
$triple = $create_multiplier(3);
$quadruple = $create_multiplier(4);

と、ファクトリーの部分の記述がすっきりする。

さらにこの上位のファンクションもアローファンクションで書いてしまうと

$create_multiplier = fn($y)=>fn($x)=>$x*$y;

となり、 プロのコーディングでアローが二つも三つもかさなるような記述が時々でてくるのだが、そろそろ読解がしんどくなってくるので、後で読み返してわかる記述方法としては、よし悪しかなあ。

Week number confusion

Just encountered this problem when I was talking with my colleague.  He thought this week was week 43 but it actually was 42 on my calendar.  Turned out, there are two ways to look at week number.

First one is to simply take January first as a start of week one.  In case of 2021, January 1 is Friday.   depending of what you think as a first day of week, this year’s week 1 is only 2 days long or 3 days long, then Week 2 will start on either January 3rd(Sunday) or January 4th (Monday)

Excel function =WeekNum()  uses this approach.

Then there is a week number definition by ISO 8601.  According to ISO,  Week 1 is the first week of the year that has more than 4 days in a week.  Further more, first day of week will start on Monday.

With this definition, January first of 2021 does not qualify as a  week number 1.  A week starts on January 4th(Monday) is week number 1.

Excel also has function to calculate this as   =ISOWeekNum()

When I first turn on week number display on Outlook,  it showed the week number based on ‘January 1 as week 1’ approach.   This is not correct on many occasion. It appears majority of business now uses ISO week number.   You can change the settings in Outlook calendar to correct this.

In Outlook, go to File then select ‘Option’ then select ‘Calendar’  In Worktime  section, change ‘First day of week’ to ‘Monday’ then First Week of Year from ‘Starts on Jan 1’ to ‘First 4 day week’

Trying to code ISO weeknumber in typescript turned out to be a fun project but that is another story..

Layoffされていて思ったこと

実は去年の4月中旬から7月中旬までレイオフされていた。会社によってはFurloughといったりするが、要は一時解雇。 給料は出ない。保険も一部カバーされなくなる。その代わり、と言っちゃなんだが 失業保険をもらえる’権利’が生ずる。 現在の米国の失業保険は週に360ドルだが、 Covid-19が猛威を振るい始めた段階で、4月から7月にかけては一時的に週に600ドルの特別手当がつくようになった。 なので、週にもらえる手当が960ドルだ。月に換算すると4,114ドル、日本円に換算すると約43万円。自分の場合はすでに子供は自立し、家の支払いも終わっているから、これだけ支給されれば生活にはこまらなかったが、 色々と考えることがあった。

1)退職の時期。 自分は日本にいたらすでに退職の年齢に達している。アメリカの場合は退職のタイミングは自分で決めるので、年金を受け取らないことによるペナルティが発生する71歳ぎりぎりまで働くことは可能なのだ。 とは言ってもそこまで働くつもりはあまりなく、 今開発中の製品が生産に移った時点で辞めようと考えていたのだが、 今回仕事をせずに過ごしてみて、少し考えが変わった。 色々習い事をしたり、趣味に時間が取れるのはうれしいのだが、なんともいえないすかすか感が漂う。 まだ引退するには早いかなあ、と思ってしまったのは事実。

2)自分の働く会社は一時キャッシュフローが全くの0になってしまったので、従業員の数を25%くらいにまで落とした。 2か月ほどで50パーセントまで戻したが自分はその勘定に入っていない。職種を考えると当たり前なのだが、給料がもらえないということより、自分が必要とされていないという疎外感のほうが精神的にきつかった。

というわけで退職後の生活を本当にどうすれば気持ちの健康を保てるのか、ということを考える良い機会になった。

 

ありがたいことに米国人の上司が自分の年季はあと一年くらいしかないということを知りながら、呼び戻してくれた。

最終的に呼び戻されたのは全従業員の2/3で、 残りは解雇。 それから3か月たったが仕事の流れに支障がないといえばウソになる。自動車業界というのは恐ろしいところで、2か月間生産がストップしたため市場在庫が底をつき、これを埋める形で生産の量はほぼ通常に戻ってしまったどころか、あちこちで部品の欠品が発生し、今や生産を維持するために毎日が火消し状態。単純に考えても仕事の量が50パーセント増えたのに、これだ。 えらいことである、

と 書いたのが1年前、 そのころは2021年の夏も過ぎればこの状況収まっているのだろうと思っていたのだがこれがとんでもない誤算。

まずコロナ騒ぎがおさまっていない。アメリカにしてもワクチンの接種率が頭打ちになってしまった。ワクチンに不安を持った層を共和党が煽った結果だが、アメリカという国民は科学や合理性を信じていない輩が半分ちかくいる、これが簡単に騙される、ということを改めて痛感した。 FaceBook/Twitter/youtube などにあふれているFake Newsにいとも簡単に乗せられている。これらのSNSは似たような記事をお勧めに載せるようにできているので、嘘でもどんどん信じるようにできているようだ。

それはともかく。

部品欠品の状況が一向に収まらない。これはアジアの生産拠点などが順繰りにコロナで閉鎖されたりすることによってSupply Chainが滞ってしまっていることが原因だが、それにより、各自動車メーカーが減産を余技なくされている。

自動車は何千点の部品で構成されているので一つでも欠品すると自動車として完成しないのだ。 困ったことに、全体的に減産しているので余っている部品は在庫がふくらみ、業者にとっては踏んだり蹴ったりである。

 

Hyperbolic function on Casio fx-991EX

There is no dedicated HYP key to call up cosh() or sinh() function in Casio fx-991EX.   This is a stern contrast to Casio fx-115ES keyboard.  It does not mean you cant use the hyperbolic funtction on fx-991EX

Hit “OPTNOption” key then select “Hyperbolic Func”

You need to be in “Calculate” mode to see this option, so for repeatability,

Hit menu, select 1.   Hit OPTNOption, select 1.

This calculator is obviously not RPN.  The calculator uses “natural notation” format.  It basically let you create equation and then evaluate the answer.  No concern on intermediate calculation.  Just answers, and it is really a wonder that you can get those devices for less than $20.

DM42

自分が使っている計算機はいまだにRPNである。 いまや科学技術計算はもとより、日ごろの営業計算もコンピューター上のエクセルで行ったほうがよっぽど早いので、専用の卓上計算機は 机上のアクセサリーであるとしか言えず、 たまに使うときも 製品単価のマージンをみるときぐらい。 そう %キー Δ%キーを押すほうがエクセル上で計算式をタイプするより簡単な時ぐらいである。 老後のマネービルディングを考えるのにも必要な機能は金融計算。 そういうことで、ビジネス計算機のHP-30bで事足りる。

ともあれ、Sine/Cosineをはじめとした三角関数 ルートやべき乗の関数。や数百ステップのプログラム機能を持った科学計算電卓というのはパソコン黎明期以前にエンジニアになった身としては特有な感慨がある。 これだけならばCasioのfx計算機を一台持っていればよいことになる(去年、Casio fx-991exという学校教材用の電卓をTargetというスーパーマーケットで15ドルで購入し、コスパの高さに驚愕した。)が、RPNという計算法を一度肌で覚えてしまうとどうしても計算機はHPがいいよね。ということになる。

専用のアルファキーがついているので、便利、この部分は完全に裏側に折りたためるので普通の計算機のように片手で持って操作していた。

自分として最初のHPはHP-28Sという、手帳のように折りたためるRPL機だったが、これはフレキシブルケーブルがダメになったか、アルファ側のキーが反応しなくなったので、その後、やはりRPLを実装した48Gを購入。48Gはいまだに動作するが、さすがに動作速度がおそく、Android AppのDroid48とかEmu48はグラフィックを一瞬で描画するが、元祖のほうの描画速度はとてつもなく遅い。 どちらにせよ、28にしても48にしても自分が必要とする機能は100パーセント以上満たしている。

これで終わっていればよかったのだが、 HPの計算機というのはコレクター意欲をそそる側面があるようで、その後 趣味の世界に突入し HP-33S,HP-35S、HP-12C, HP-15C LE、HP-30b とRPN機への散財が続くようになった。 さらに オタク化し、金融計算機のhp20-b/30bをフラッシュしなおしてwp-34sという科学計算機を”作成”して使用中。

WP34Sは自作するか、カスタムメードしてくれる個人からの購入になるが、マニュアルは製本されたものがAmazonで購入可能

このwp-34S 、キーに割り付けられた機能が多いわりには非常に使いやすいのだが、コンセプトのベースとなったのがHP-42sであり、42sを使いなれた人には操作でまごつくことはないはず、という但し書きがついている。

42sはHPのフラッグシップでNASAと一緒に宇宙まで行った41Cの後継機だが、、モジュールプラグインなどの拡張機能がないので41シリーズの影に隠れて存在感が薄い。 ただし、その後Free42というオープンソースプロジェクトにてアプリとして復活し、いまやWindowsはもちろん、スマホやタブレットなどでも使うことが可能。

Free42ですごいのはこれが単なるロムのエミューレーションではなく、一から書き直されているところ。そして、プログラムのロード・セーブがエミュレーターのプラットフォーム上でできるため、42sの弱点であった、プログラムライブラリーの利用が手入力でしかできないという欠点が解消されている。41cとは開示されている仕様内ではプログラム言語が上位互換なので、41C/41CV/41CXのプログラムライブラリーがほとんど使えるということだ。

で、そのFree42をArmコアでハードウエア化してしまったのが、SwissMicros社から発売されているDM42である。

値段は安くはない。今時卓上計算機に$200ドル以上払うのはコレクター以外にはいないんだろうと思えるのだが、(自分もその一人) 今回コロナウイルスの影響で一時解雇されていたわが身が職場復帰できたのを一人で勝手に祝って購入してみた機体のシリアル番号は6000番台である。 この計算機は2017年に発売開始だから、年間2000台は売れていることになる。

ちなみにSwiss Mailで配送され、北米のミシガン州にはオーダーしてから2週間以内で届いた。

 

使い勝手は非常に良い。 大きなメモリーグラフィック液晶の表示も見やすいし、ボタンのクリック感もCasioやシャープとは一線を画し、HPの計算機に近い感じ。 Youtubeを探すとすでに何件も動画があがっているようで参考にしてみてほしい。 動画の一部で言っているキーボードの不具合はすでに解決されていた。 USBを使ってのFirmWareの書き換えも可能。

Logan West氏によるHP-42SとDM42の比較動画

昨日、SwissMicrosからお知らせが来て、同じハードウエアで41xを発売開始したとある。こちらはオリジナルROMのイメージを使ったEmulatorなので、41Cシリーズとはマイクロコードレベルでの互換性がある。しかもExpansion Moduleはすべて格納済みだそうである。  コレクションにもう一台増えそうだな、と思っている。