-
Hey everyone! In order to improve the quality and standardize our screenshots, we created this code snippet: Snippet/*
* Once upon a time...
*/
class Vampire {
constructor(props) {
this.location = props.location;
this.birthDate = props.birthDate;
this.deathDate = props.deathDate;
this.weaknesses = props.weaknesses;
}
get age() {
return this.calcAge();
}
calcAge() {
return this.deathDate - this.birthDate;
}
}
// ...there was a guy named Vlad
const Dracula = new Vampire({
location: 'Transylvania',
birthDate: 1428,
deathDate: 1476,
weaknesses: ['Sunlight', 'Garlic']
}); Why this is cool?
Where to store them?They are going to be kept on the template repo under the Help us with other languages!Leave a comment on this thread with that same code snippet in other programming languages.
|
Beta Was this translation helpful? Give feedback.
Replies: 23 comments 23 replies
-
Python '''
Once upon a time...
'''
class Vampire:
def __init__(self, traits):
self.location = traits['location']
self.birth_date = traits['birth_date']
self.death_date = traits['death_date']
self.weaknesses = traits['weaknesses']
def get_age(self):
return self.calc_age()
def calc_age(self):
return self.death_date - self.birth_date
# ...there was a guy named Vlad
Dracula = Vampire({
'location': 'Transylvania',
'birth_date': 1428,
'death_date': 1476,
'weaknesses': ['Sunlight', 'Garlic']
}) |
Beta Was this translation helpful? Give feedback.
-
B4X
|
Beta Was this translation helpful? Give feedback.
-
PHP /*
* Once upon a time...
*/
class Vampire
{
public string $location;
public int $birthDate;
public int $deathDate;
public array $weaknesses;
public function __construct(array $props)
{
$this->location = $props['location'];
$this->birthDate = $props['birthDate'];
$this->deathDate = $props['deathDate'];
$this->weaknesses = $props['weaknesses'];
}
public function age(): int
{
return $this->calcAge();
}
private function calcAge(): int
{
return $this->deathDate - $this->birthDate;
}
}
// ...there was a guy named Vlad
$Dracula = new Vampire([
'location' => 'Transylvania',
'birthDate' => 1428,
'deathDate' => 1476,
'weaknesses' => ['Sunlight', 'Garlic']
]); |
Beta Was this translation helpful? Give feedback.
-
Ruby 🙂 #
# Once upon a time...
#
class Vampire
def initialize(opts)
@location = opts[:location]
@birthDate = opts[:birthDate]
@deathDate = opts[:deathDate]
@weaknesses = opts[:weaknesses]
end
def age
calcAge
end
private
def calcAge
@deathDate - @birthDate
end
end
# ...there was a guy named Vlad
dracula = Vampire.new(
location: 'Transylvania',
birthDate: 1428,
deathDate: 1476,
weaknesses: %w[Sunlight Garlic]
)
puts dracula.age |
Beta Was this translation helpful? Give feedback.
-
C! #include <stdlib.h>
struct Vampire {
char *location;
int birthday;
int deathdate;
char *weaknesses[2];
};
int _calcAge(struct Vampire *v) { return v->deathdate - v->birthday; }
int get_age(struct Vampire *v) { return _calcAge(v); }
int main() {
struct Vampire v;
/* There was a guy named Vlad */
v.location = malloc(12 * sizeof(char));
v.location = "Transylvania";
v.birthday = 1428;
v.deathdate = 1476;
v.weaknesses[0] = "Sunlight";
v.weaknesses[1] = "Garlic";
get_age(&v);
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
Typescript /*
* Once upon a time...
*/
interface VampireProps {
location: string;
birthDate: number;
deathDate: number;
weaknesses: string[];
}
class Vampire {
location: string;
birthDate: number;
deathDate: number;
weaknesses: string[];
constructor(props: VampireProps) {
this.location = props.location;
this.birthDate = props.birthDate;
this.deathDate = props.deathDate;
this.weaknesses = props.weaknesses;
}
get age(): number {
return this.calcAge();
}
calcAge(): number {
return this.deathDate - this.birthDate;
}
}
// ...there was a guy named Vlad
const Dracula: VampireProps = new Vampire({
location: 'Transylvania',
birthDate: 1428,
deathDate: 1476,
weaknesses: ['Sunlight', 'Garlic'],
}); |
Beta Was this translation helpful? Give feedback.
-
Kotlin 😄 /*
* Once upon a time ...
*/
class Vampire(
val location: String,
val birthDate: Int,
val deathDate: Int,
val weaknesses: Array<String>
) {
val age: Int
get() = this.calcAge()
fun calcAge() =
this.deathDate - this.birthDate
}
// ... there was a guy named Vlad
fun main() {
val Dracula = Vampire(
"Transylvania",
1428,
1476,
arrayOf("Sunlight", "Garlic")
)
} |
Beta Was this translation helpful? Give feedback.
-
Java class Vampire {
private String location;
private int birthDate;
private int deathDate;
private String[] weaknesses;
public Vampire(String location, int birthDate, int deathDate, String[] weaknesses) {
this.location = location;
this.birthDate = birthDate;
this.deathDate = deathDate;
this.weaknesses = weaknesses;
}
public int getAge() {
return this.calcAge();
}
public int calcAge() {
return this.deathDate - this.birthDate;
}
}
// ...there was a guy named Vlad
Vampire vampire = new Vampire(
"Transylvania",
1428,
1476,
new String[] {"Sunlight", "Garlic"}
); |
Beta Was this translation helpful? Give feedback.
-
Go package main
import "fmt"
/*
Once upon a time...
*/
type Vampire struct {
Location string
BirthDate int
DeathDate int
Weaknesses []string
}
func (v *Vampire) Age() int {
return v.calcAge()
}
func (v *Vampire) calcAge() int {
return v.DeathDate - v.BirthDate
}
// ...there was a guy named Vlad
func main() {
dracula := &Vampire{
Location: "Transylvania",
BirthDate: 1428,
DeathDate: 1476,
Weaknesses: []string{"Sunlight", "Garlic"},
}
fmt.Println(dracula.Age())
} |
Beta Was this translation helpful? Give feedback.
-
Rust // Once upon a time...
#[derive(Debug)]
pub struct Vampire {
location: String,
birth_date: u16,
death_date: u16,
weaknesses: Vec<String>,
}
impl Vampire {
pub fn new(
location: String,
birth_date: u16,
death_date: u16,
weaknesses: Vec<String>,
) -> Self {
Vampire {
location,
birth_date,
death_date,
weaknesses,
}
}
pub fn age(&self) -> u16 {
self.calc_age()
}
fn calc_age(&self) -> u16 {
self.death_date - self.birth_date
}
}
// ...there was a guy named Vlad
fn main() {
let dracula = Vampire::new(
"Transylvania".to_string(),
1428,
1476,
vec!["Sunlight".to_string(), "Garlic".to_string()],
);
println!("{:?}", dracula);
} |
Beta Was this translation helpful? Give feedback.
-
Here's an SML version (I have an alternate). I dropped the getter (SML doesn't have them, and it doesn't really have "objects") and renamed (*
* Once upon a time...
*)
structure Vampire = struct
type params = {location: string,
birthDate: int,
deathDate: int,
weaknesses: string list}
type vampire = params
fun new (v : params) : vampire = v
fun age (v : vampire) : int = (#deathDate v) - (#birthDate v)
end
(* ...there was a guy named Vlad *)
structure Romainia = struct
val dracula = Vampire.new {location="Transylvania",
birthDate=1428,
deathDate=1476,
weaknesses=["Sunlight", "Garlic"]}
end |
Beta Was this translation helpful? Give feedback.
-
Hmm, how will it work for markdown and HTML? |
Beta Was this translation helpful? Give feedback.
-
HTML <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dracula</title>
</head>
<body>
<!--
Once upon a time...
-->
<h1>Vampires</h1>
<form>
<label for="location">Location</label>
<input type="text" name="location" value="Transylvania">
<label for="birthDate">Birth Date:</label>
<input type="number" name="birthDate" value="1428">
<label for="deathDate">Death Date:</label>
<input type="number" name="deathDate" value="1476">
<label for="weaknesses">Weaknesses:</label>
<input type="checkbox" name="weaknesses" value="Sunlight" checked>
<input type="checkbox" name="weaknesses" value="Garlic" checked>
<button type="submit">Submit</button>
</form>
<script>
// ...there was a guy named Vlad
const form = document.querySelector('form');
form.addEventListener('submit', calcAge);
function calcAge(e) {
e.preventDefault();
const { birthDate, deathDate } = e.target;
const age = deathDate.value - birthDate.value;
console.log(age);
}
</script>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
Cool, so for markdown how about a something like <!--
Once a upon a time...
-->
# Vampires
| Name | Value |
|--------------|------------------|
| location | Transylvania |
| birth date | 1428 |
| death date | 1476 |
| weaknesses | Sunlight, Garlic |
<!-- ...There was a guy named Vlad -->
> The **age** is the `deathDate` minus the `birthDate`
|
Beta Was this translation helpful? Give feedback.
-
C++! (Format based on "Google" option from this tool) #include <string>
#include <vector>
typedef std::vector<std::string> weaknesses_list;
/*
* Once upon a time...
*/
class Vampire {
private:
std::string _location;
int _birth_date;
int _death_date;
weaknesses_list *_weaknesses;
public:
Vampire(std::string location, int birth_date, int death_date,
weaknesses_list *weaknesses)
: _location(location),
_birth_date(birth_date),
_death_date(death_date),
_weaknesses(weaknesses) {}
int age() { return this->calc_age(); }
private:
int calc_age() { return this->_death_date - this->_birth_date; }
};
/* ...there was a guy named Vlad */
int main() {
std::string location = "Transylvania", weakness_sunlight = "Sunligth",
weakness_garlic = "Garlic";
int birth_date = 1428, death_date = 1476;
weaknesses_list *weaknesses = new weaknesses_list();
weaknesses->push_back(weakness_sunlight);
weaknesses->push_back(weakness_garlic);
Vampire *dracula = new Vampire(location, birth_date, death_date, weaknesses);
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
CSS /*
* Once upon a time...
*/
:root {
--birthDate: 1428px;
--deathDate: 1476px;
}
body {
background: #000;
}
/* ...there was a guy named Vlad */
#dracula {
opacity: 0;
display: none;
visibility: hidden;
font-family: "Transylvania";
height: calc(var(--deathDate) - var(--birthDate));
}
.cape {
background: #ff0000 !important;
}
@font-face {
font-family: 'Transylvania';
src: url('/location/Transylvania.woff2') format('woff2');
font-weight: 700;
font-style: normal;
} |
Beta Was this translation helpful? Give feedback.
-
Swift /*
* Once upon a time...
*/
class Vampire {
var location: String
var birthDate: Int
var deathDate: Int
var weaknesses: [String]
init(location: String, birthDate: Int, deathDate: Int, weaknesses: [String]) {
self.location = location
self.birthDate = birthDate
self.deathDate = deathDate
self.weaknesses = weaknesses
}
var age: Int {
self.calcAge()
}
func calcAge() -> Int {
self.deathDate - self.birthDate
}
}
// ...there was a guy named Vlad
let dracula = Vampire(location: "Transylvania", birthDate: 1428, deathDate: 1476, weaknesses: ["Sunlight", "Garlic"]) |
Beta Was this translation helpful? Give feedback.
-
Scala /*
* Once upon a time...
*/
class Vampire(location: String, birthDate: Int, deathDate: Int, weaknesses: Array[String]) {
def age(): Int = {
calcAge()
}
def calcAge(): Int = {
this.deathDate - this.birthDate
}
}
// ...there was a guy named Vlad
val dracula = new Vampire(location = "Transylvania", birthDate = 1428, deathDate = 1476, weaknesses = Array("Sunlight", "Garlic")) |
Beta Was this translation helpful? Give feedback.
-
Dart /*
* Once upon a time...
*/
class Vampire {
String location;
int birthDate, deathDate;
List<String> weaknesses;
Vampire({this.location, this.birthDate, this.deathDate, this.weaknesses});
int get age => this.calcAge();
int calcAge() => this.deathDate - this.birthDate;
}
void main() {
// ...there was a guy named Vlad
final Dracula = Vampire(
location: 'Transylvania',
birthDate: 1428,
deathDate: 1476,
weaknesses: ['Sunlight', 'Garlic']);
} |
Beta Was this translation helpful? Give feedback.
-
Elixir defmodule Vampire do
@moduledoc """
Once upon a time...
"""
defstruct [:location, :birth_date, :death_date, :weaknesses]
def new(props) do
%__MODULE__{
location: props[:location],
birth_date: props[:birth_date],
death_date: props[:death_date],
weaknesses: props[:weaknesses]
}
end
def age(vampire) do
calc_age(vampire)
end
defp calc_age(vampire) do
vampire.death_date - vampire.birth_date
end
end
# ...there was a guy named Vlad
dracula = Vampire.new(
location: "Transylvania",
birthDate: 1428,
deathDate: 1476,
weaknesses: ["Sunlight", "Garlic"]
) |
Beta Was this translation helpful? Give feedback.
-
I didn't found a C# snippet, so I created this one... 😄 /*
* Once upon a time...
*/
public class Vampire {
public string Location { get; private set; }
public int BirthDate { get; private set; }
public int DeathDate { get; private set; }
public string[] Weaknesses { get; private set; }
public Vampire(string location, int birthDate, int deathDate, string[] weaknesses) {
Location = location;
BirthDate = birthDate;
DeathDate = deathDate;
Weaknesses = weaknesses;
}
public int Age() {
return calcAge();
}
private int calcAge() {
return DeathDate - BirthDate;
}
}
class Program {
static void Main(string[] args) {
// ...there was a guy named Vlad
var vampire = new Vampire(
"Transylvania",
1428,
1476,
new string[] {"Sunlight", "Garlic"}
);
}
} |
Beta Was this translation helpful? Give feedback.
-
Clojure (comment
"Once upon a time...")
(ns clj-dracula)
(defstruct dracula :location :birth-date :death-date :weaknesses)
(defn age
[vamp] (- (vamp :death-date) (vamp :birth-date)))
;;...there was a guy named Vlad
(let [d (struct dracula "Transylvania" 1428 1476 '("Sunlight", "Garlic"))]
(println (age d))) |
Beta Was this translation helpful? Give feedback.
-
All code samples have been moved to the template repository. 🎉 Thank you all for contributing! |
Beta Was this translation helpful? Give feedback.
All code samples have been moved to the template repository. 🎉
Thank you all for contributing!