Last active
April 23, 2021 14:35
-
-
Save htsign/d01e57b9dc819d161c8a0cbc07d18268 to your computer and use it in GitHub Desktop.
get stdin lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// F# | |
open System.IO | |
let getLines : TextReader -> string seq = | |
Seq.unfold (fun r -> | |
match r.ReadLine() with | |
| null -> None | |
| line -> Some (line, r)) | |
[<EntryPoint>] | |
do | |
getLines stdin | |
|> List.ofSeq | |
|> printfn "%A" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Go | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
) | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
lines := make([]string, 0) | |
for scanner.Scan() { | |
lines = append(lines, scanner.Text()) | |
} | |
for _, s := range lines { | |
fmt.Printf("%s\n", s) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java | |
import java.util.Scanner; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
class StdIn { | |
public static void main(String[] args) { | |
try (final var scanner = new Scanner(System.in)) { | |
final var lines = Stream.generate(() -> scanner) | |
.takeWhile(Scanner::hasNextLine) | |
.map(Scanner::nextLine) | |
.collect(Collectors.toList()); | |
System.out.println(lines); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Julia | |
lines = eachline(stdin) |> collect | |
println(lines) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Node.js | |
const fs = require('fs'); | |
const { promisify } = require('util'); | |
(async () => { | |
// require('fs/promises').readFile() doesn't work with process.stdin.fd | |
const data = await promisify(fs.readFile)(process.stdin.fd, 'utf8'); | |
const lines = data.split(/\r?\n/); | |
console.log(lines); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
fun main() { | |
val lines = generateSequence { readLine() }.toList() | |
println(lines) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* OCaml *) | |
let get_lines ch = | |
let rec aux acc = | |
let line = | |
try Some (input_line ch) | |
with End_of_file -> None | |
in | |
match line with | |
| None -> List.rev acc | |
| Some s -> (aux [@tailcall]) (s :: acc) | |
in | |
aux [] | |
let () = | |
let lines = get_lines stdin in | |
print_endline ("[" ^ String.concat ", " lines ^ "]") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nim | |
from sequtils import toSeq | |
let lines = toSeq(stdin.lines) | |
echo lines |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_lines() { | |
while ($line = fgets(STDIN)) { | |
yield $line; | |
} | |
} | |
$lines = iterator_to_array(get_lines()); | |
print_r($lines); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Perl | |
my @lines = map { chomp; $_ } <>; | |
print join ',', @lines; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StdInReader is InputNotify | |
let _env: Env val | |
var _buffer: String ref = String | |
new create(env: Env val) => _env = env | |
fun ref apply(data: Array[U8] iso) => | |
_buffer.append(String.from_iso_array(consume data)) | |
fun ref dispose() => | |
let lines: Array[String val] val = _buffer.split() | |
_env.out.print("[" + ", ".join(lines.values()) + "]") | |
actor Main | |
new create(env: Env val) => | |
env.input(recover iso StdInReader(env) end) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python | |
import sys | |
lines = (line.rstrip() for line in sys.stdin) | |
print(list(lines)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Raku (renamed from Perl6) | |
my @lines = lines; | |
@lines.join(',').say; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby | |
lines = ARGF.map &:chomp | |
p lines |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rust | |
use std::io::{stdin, BufRead}; | |
fn main() { | |
let lines: Vec<String> = stdin().lock().lines() | |
.filter_map(|x| x.ok()) | |
.collect(); | |
println!("{:?}", lines); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Scala | |
object StdIn extends App { | |
val lines = io.Source.stdin.getLines().toSeq | |
println(lines) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swift | |
let lines = Array(AnyIterator { readLine() }) | |
print(lines) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment