Python

列表推导筛素数:

def primes(n):
    return [x for x in range(2, n + 1)
            if all(x % i for i in range(2, int(x**0.5) + 1))]

print(primes(30))

Rust

迭代器链式调用,求 1~100 中所有偶数平方和:

fn main() {
    let sum: u32 = (1..=100)
        .filter(|n| n % 2 == 0)
        .map(|n| n * n)
        .sum();
    println!("偶数平方和 = {}", sum);
}

Go

结构体方法 + 字符串格式化:

package main

import (
    "fmt"
    "math"
)

type Point struct{ X, Y float64 }

func (p Point) Dist() float64 {
    return math.Sqrt(p.X*p.X + p.Y*p.Y)
}

func main() {
    p := Point{X: 3, Y: 4}
    fmt.Printf("点 %+v 到原点距离 = %.2f\n", p, p.Dist())
}

JavaScript

数组方法链:

const xs = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];

const result = xs
  .filter(n => n > 2)
  .sort((a, b) => a - b)
  .reduce((acc, n) => acc + n, 0);

console.log("过滤 + 排序 + 求和 =", result);

TypeScript

判别联合 + 类型收窄:

type Shape =
  | { kind: "circle"; r: number }
  | { kind: "square"; size: number };

function area(s: Shape): number {
  switch (s.kind) {
    case "circle": return Math.PI * s.r * s.r;
    case "square": return s.size * s.size;
  }
}

const shapes: Shape[] = [
  { kind: "circle", r: 5 },
  { kind: "square", size: 3 }
];

shapes.forEach(function (s) {
  console.log(s.kind, "面积 =", area(s).toFixed(2));
});

Bash

管道处理:把数字流求平方:

seq 1 6 | while read n; do
  echo "$n^2 = $((n * n))"
done

C

指针与数组:

#include <stdio.h>

int main(void) {
    int xs[] = {3, 1, 4, 1, 5, 9, 2, 6};
    int n = sizeof(xs) / sizeof(xs[0]);
    int sum = 0;
    for (int *p = xs; p < xs + n; p++) {
        sum += *p;
    }
    printf("数组和 = %d\n", sum);
    return 0;
}

C++

STL 排序 + range-based for:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
    std::sort(v.begin(), v.end());
    std::cout << "排序后: ";
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}

Java

Stream API 函数式风格:

import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        int sum = IntStream.rangeClosed(1, 10)
                .filter(n -> n % 2 == 0)
                .map(n -> n * n)
                .sum();
        System.out.println("偶数平方和 = " + sum);
    }
}

Ruby

Range + Block:

result = (1..10).select { |n| n.even? }.map { |n| n * n }.sum
puts "偶数平方和 = #{result}"

PHP

匿名函数与高阶函数:

<?php
$xs = [1, 2, 3, 4, 5];
$squared = array_map(fn($x) => $x * $x, $xs);
$sum = array_sum($squared);
echo "平方和 = $sum\n";

Kotlin

data class + 集合操作:

fun main() {
    var name = "zero"
    println("Hello $name")
}

Swift

可选类型 + 高阶函数:

let names = ["Alice", "Bob", "Charlie", "Dave"]
let longest = names.max(by: { $0.count < $1.count })

if let n = longest {
    print("最长名字: \(n) (共 \(n.count) 个字符)")
}

C#

LINQ 查询:

using System;
using System.Linq;

class Program {
    static void Main() {
        var result = Enumerable.Range(1, 10)
            .Where(n => n % 2 == 0)
            .Select(n => n * n)
            .Sum();
        Console.WriteLine($"偶数平方和 = {result}");
    }
}

Lua

print('Hello lua')