GithubHelp home page GithubHelp logo

alien-snippets's Introduction

alien-snippets

Programming exercises for Python based on documentation of new programming languages.

The key idea for this repo is that newer programming languages provide us with useful code examples in their documentation. Using these examples might give some fresh air, authority and intrigue to an introductory Python class or a self-study session. You can ask your students:

Can you make this work in Python? People made a whole new programming language to make this work!

Languages considered

Anything rather new, less mainstream and possible for a human to read qualifies, examples:

  • Zig
  • vlang
  • Vale
  • Pony
  • Crystal
  • Julia
  • D
  • Odin

Personal preferences: really like vโ€™s one-page documentation, fond of parts of Julia (but it is kind of mainstream lang now), and cannot remember that one programming language that had hello world in different human languages at their front page - it would make a great example to replicate.

Exercises

Can you do the examples below in Python? Steps involved:

  • go over the code in an alien programming language
  • make up your mind what this code does
  • match to familiar concepts
  • come up with implementation in Python.

Using ChatGPT is only allowed when you are totally stuck, even then ask for clues, not a ready solution.

Maps from ballerina

Hint ... are dictionaries in Python!
import ballerina/io;

public function main() {
    // Creates a `map` constrained by the `int` type.
    map<int> ages = {
        "Tom": 23,
        "Jack": 34
    };

    // Gets the entry for `Tom`.
    int? v = ages["Tom"];
    io:println(v);

    // As there exists an entry for `Tom`, it can be accessed using the `map:get()` method. 
    // Using `ages["Tom"]` wouldn't work here because its type would be `int?` and  not `int`.
    int age = ages.get("Tom");
    io:println(age);

Gleam allows keyword arguments

Refactoring idea Does the order of arguments in the orginal Gleam function seem natural to you?
import gleam/io

pub fn main() {
  // Without using labels
  io.debug(calculate(1, 2, 3))

  // Using the labels
  io.debug(calculate(1, add: 2, multiply: 3))

  // Using the labels in a different order
  io.debug(calculate(1, multiply: 3, add: 2))
}

fn calculate(value: Int, add addend: Int, multiply multiplier: Int) {
  value * multiplier + addend
}

Case switch in Nim

Precaution Only a part of this code can be translated to Python `match`/`case`.
# Case switch.
var letter = 'c'

case letter
of 'a':
  echo "letter is 'a'"
of 'b', 'c':
  echo "letter is 'b' or 'c'"
of 'd'..'h':
  echo "letter is between 'd' and 'h'"
else:
  echo "letter is another character"

What century is it now?

Example from excellent v docs.

Extra question What is the significance of `0.009999794661191` constant?
import time
import math

type MyTime = time.Time

fn (mut t MyTime) century() int {
	return int(1.0 + math.trunc(f64(t.year) * 0.009999794661191))
}

fn main() {
	mut my_time := MyTime{
		year: 2020
		month: 12
		day: 25
	}
	println(time.new(my_time).utc_string())
	println('Century: ${my_time.century()}')
}

Vale: print a list of planets

Probably the easiest task on the list.

Extension idea Consider adding "Hello, Venusians!" and varieties for Earth and Mars to your print statement.
import stdlib.*;

exported func main() {
 planets = [#]("Venus", "Earth", "Mars");
 foreach planet in planets {
   println("Hello " + planet + "!");
 }
}

Notes

  • Code examples are abundant at Rosetta Code, but I find the new programming language docs specifically to be a place where a lot of effort, knowledge and attention focus is concentrated, thus making them a good resource to draw from.
  • A great cross-language reference for mainstream and older programming langauges is hyperpolyglot.
  • If I'm to provide some basis for comparing languages I'd use this Reddit comment of mine that got surprisingly popular. Would probably need more account for programming paradigms though.

alien-snippets's People

Contributors

epogrebnyak avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.