未加星标

Schwartzian Transforms in Javascript

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

There’s a style of programming that I have come to love that allows you to think like pipes in a prompt. I figured it out after trying to understand the Schwartzian Transform , which came about from Randal Schwartz answering questions on UseNet in the 1990s. The problem was that someone wanted to sort this data by last name.

adjn:Joshua Ng adktk:KaLap Timothy Kwong admg:Mahalingam Gobieramanan admln:Martha L. Nangalama

And Schwartz suggested this.

#!/usr/bin/perl require 5; # new features, new bugs! print map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, /(\S+)$/] } <>;

I have explained this style before , and the key is to read it from the bottom up.

<> means we’re getting one entry at a time from STDIN map { [$_, /(\S+)$/] } means we’re turning the string adjn:Joshua Ng into an anonymous array ["adjn:Joshua Ng","Ng"] sort { $a->[1] cmp $b->[1] } sorts the whole thing on the 2nd value in each array map { $_->[0] } undoes the split into anonymous arrays, making an array of strings again print prints them

You could have made this into a series of painstaking steps my @array = <>; my @array2 ; for my $e ( @array ) {...} , to introduce more modern Perlishness into this, but this is clean, by which I mean there are not a series of named variables sitting around.

So, I’ve been writing a lot of javascript these days, and have been learning some about arrow functions and other JS hackishness.

"use strict"; console.log( function() { /* adjn:Joshua Ng adktk:KaLap Timothy Kwong admg:Mahalingam Gobieramanan admln:Martha L. Nangalama */ } .toString() .split(/\/\*/)[1] .split(/\*\//)[0] .split(/\n/) .filter(x => String(x).match(/\w/)) .map(x => [x.split(/\s/).pop(), x]) .sort((a, b) => (a[0] > b[0] ? 1 : 0 - 1)) .map(x => x[1]) .join("\n") );

Which outputs:

admg:Mahalingam Gobieramanan adktk:KaLap Timothy Kwong admln:Martha L. Nangalama adjn:Joshua Ng

This isn’t bottom-to-top, so

we make an anonymous function that is entirely a comment block holding our data which we cast as a String with .toString() use .split() to get the data part of the data by splitting on the open and close comment make into an array by .split() on the newline character .filter() to ensure each entry has a word character use .map() to make an anonymous array, this time with the key coming first, by splitting on string characters and .pop() -ing off the last element as the last name doing the sort, using a ternary operator within .sort() to save space using .map() to get back to the original string we want sorted and finally, making it into a string join .join() with newlines.

Funny that there was no mention of JavaScript in that Wikipedia article.

There are other things we can use to add to this style. I found out earlier today that you can kinda get to Perl’s range operator, like 0 .. 9 , by doing creating an anonymous array, declaring the size and getting a list of the indexes by abusing the object nature, like:

let ranged = [...Array(10).keys()] # [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

And, of course, you can map and filter these things into exactly the shape you want.

If you have any questions or comments, I would be glad to hear it. Ask me on Twitter or make an issue on my blog repo .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Schwartzian Transforms in Javascript
本站链接:https://www.codesec.net/view/611302.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(74)