词条 | One-liner program |
释义 |
Originally, a one-liner program was textual input to the command-line of an operating system shell that performs some function in just one line of input. The one-liner can be
Certain dynamic scripting languages such as AWK, sed, and Perl have traditionally been adept at expressing one-liners. Specialist shell interpreters such as Unix shells or Windows PowerShell allow for the construction of powerful one-liners. The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line. HistoryThe concept of a one-liner program has been known since the 1960s[1] with the release of the APL programming language. With its terse syntax and powerful mathematical operators, APL allowed useful programs to be represented in a few symbols. In the 1970s, one-liners became associated with the rise of the home computer and BASIC. Computer magazines published type-in programs in many dialects of BASIC. Some magazines devoted regular columns solely to impressive short and one-line programs.[2] The word One-liner also has two references in the index of the book The AWK Programming Language (the book is often referred to by the abbreviation TAPL). It explains the programming language AWK, which is part of the Unix operating system. The authors explain the birth of the one-liner paradigm with their daily work on early Unix machines: {{"|The 1977 version had only a few built-in variables and predefined functions. It was designed for writing short programs […] Our model was that an invocation would be one or two lines long, typed in and used immediately. Defaults were chosen to match this style […] We, being the authors, knew how the language was supposed to be used, and so we only wrote one-liners.}}Notice that this original definition of a one-liner implies immediate execution of the program without any compilation. So, in a strict sense, only source code for interpreted languages qualifies as a one-liner. But this strict understanding of a one-liner was broadened in 1985 when the IOCCC introduced the category of Best One Liner for C, which is a compiled language. ExamplesOne-liners are also used to show off the differential expressive power of programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner. BASICA single line of BASIC can typically hold up to 255 characters, and one liners ranged from simple games[3] to graphical demos. One of the better-known demo one-liners is colloquially known as 10PRINT, written for the Commodore 64: CThe following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC). This one-liner program is a glob pattern matcher. It understands the glob characters `*' meaning `zero or more characters' and `?' meaning exactly one character, just like most Unix shells. Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples: AWKThe TAPL book contains 20 examples of one-liners at the end of the book's first chapter. Here are the very first of them:
JHere are examples in J:
PerlHere are examples in the Perl programming language:
Many one-liners are practical. For example, the following Perl one-liner will reverse all the bytes in a file: While most Perl one-liners are imperative, Perl's support for anonymous functions, closures, map, filter (grep) and fold (List::Util::reduce) allows the creation of 'functional' one-liners. This one-liner creates a function that can be used to return a list of primes up to the value of the first parameter: It can be used on the command line, like this: to print out a comma-separated list of primes in the range 2 - number. HaskellThe following Haskell program is a one-liner: it sorts its input lines ASCIIbetically. An even shorter version: Usable on the command line like: RacketThe following Racket program is equivalent to the above Haskell example: and this can be used on the command line as follows: PythonPerforming one-liners directly on the Unix command line can be accomplished by using Python's -cmd flag (-c for short), and typically requires the import of one or more modules. Statements are separated using ";" instead of newlines. For example, to print the last field of unix long listing: import sys;[sys.stdout.write(' '.join([line.split(' ')[-1]])) for line in sys.stdin]" Python wrappersSeveral open-source scripts have been developed to facilitate the construction of Python one-liners. Scripts such as pyp or Pyline import commonly used modules and provide more human-readable variables in an attempt to make Python functionality more accessible on the command line. Here is a redo of the above example (printing the last field of a unix long listing): Executable librariesThe Python CGIHTTPServer module for example is also an executable library that performs as a web server with CGI. To start the web server enter: Serving HTTP on 0.0.0.0 port 8000 … bashIf you use default ubuntu on a docker, there is no vi editor in the system. In the situation, you can use one-liner shell such as, or other one-liner shell to order to achieve your purpose. Windows PowerShellFind palindromes in file words.txt Piping semantics in PowerShell help enable complex scenarios with one-liner programs. This one-liner in PowerShell script takes a list of names and counts from a comma-separated value file, and returns the sum of the counts for each name. See also
References1. ^{{cite book |title=10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 |date=2014 |publisher=Mit Press |isbn=9780262526746 |page=148 |url=http://nickm.com/trope_tank/10_PRINT_121114.pdf |accessdate=3 July 2018}} {{DEFAULTSORT:One-Liner Program}}2. ^{{cite web|url=https://archive.org/stream/run-magazine-35/Run_Issue_35_1986_Nov#page/n109/mode/2up|title=RUN magazine issue 35}} 3. ^{{cite web |title=Acorn User One-Line Games (Escape From Voros, Lexxias, Race To Varpon, Storm Clouds Over Zaqqit, Zander (AKA Lurch)) |url=http://bbcmicro.co.uk/game.php?id=2718 |website=bbcmicro.co.uk |accessdate=3 July 2018 |language=en}} 6 : Computer programming|Articles with example Haskell code|Articles with example C code|Articles with example Perl code|Articles with example Python code|Articles with example Racket code |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。