Skip to content

Instantly share code, notes, and snippets.

@tuxcanfly
Created December 13, 2011 05:47
Show Gist options
  • Save tuxcanfly/1470779 to your computer and use it in GitHub Desktop.
Save tuxcanfly/1470779 to your computer and use it in GitHub Desktop.
Polyglot Prime Check
#!/usr/bin/env python
###
#Everything in between these three hashes is executed only in python
# make console.log print stuff
from __future__ import print_function
class console:
log = print
# make require().eval() exec code in python
class require:
def __init__(self, x):
pass
def eval(self, *args, **kwargs):
self.code = "\n".join(args[0].splitlines()[3:])
exec(self.code)
###
# this code will be eval'd in coffeescript, and last 2 lines are
# exec'ed in python
# range and all are implemented in coffeescript
code = """
range = (x,y) -> [x..y-1]
all = (x) -> x.reduce((x, y) -> x and y)
is_prime = all(42%i != 0 for i in range(2, 42))
console.log(is_prime)
"""
require('coffee-script').eval(code, { 'fileName': '__stdin' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment