Skip to content

Instantly share code, notes, and snippets.

class HttpRequest {
readonly url : string;
}
const req = new HttpRequest();
req.url = "http://www.setfive.com";
type MyNotification = Email | SMS | VoiceRecording | BulkMessage;
class Email {}
class SMS {}
class VoiceRecording {}
class BulkMessage {}
function assertNever(error : never) : string {
throw Error("Invalid: " + error);
}
type MyNotification = Email | SMS | VoiceRecording;
class Email {}
class SMS {}
class VoiceRecording {}
class BulkMessage {}
function assertNever(error : never) : string {
throw Error("Invalid: " + error);
}
function showNotification(notification){
if(notification instanceof Email){
return "You got an email from $email with title: $title";
}else if(notification instanceof SMS){
return "You got an SMS from $number! Message: $message";
}else if(notification instanceof VoiceRecording){
return "you received a Voice Recording from $name! Click the link to hear it: $link";
}
}
// From https://docs.scala-lang.org/tour/pattern-matching.html
def showNotification(notification: Notification): String = {
notification match {
case Email(email, title, _) =>
s"You got an email from $email with title: $title"
case SMS(number, message) =>
s"You got an SMS from $number! Message: $message"
case VoiceRecording(name, link) =>
s"you received a Voice Recording from $name! Click the link to hear it: $link"
function getPartyGuests() : string[] {
if(Date.now() % 2 == 0){
return [];
}else {
return null;
}
}
const myGuests : string[] = getPartyGuests();
myGuests.push("Ashish");
<?php
$clientIds = [111929, 372530, 180565];
for($i = 0; $i < 100; $i++){
$clientIds[] = rand(1, 100000);
}
$clientIds = array_values(array_unique($clientIds));
$values = [];
for($i = 0; $i < 5000000; $i++){
@adatta02
adatta02 / datecol.sql
Last active September 24, 2017 02:58
mysql> ALTER TABLE client_order ADD COLUMN date date default null;
Query OK, 0 rows affected (46.72 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> UPDATE client_order SET `date` = STR_TO_DATE(DATE(created_at), "%Y-%m-%d");
Query OK, 5000000 rows affected (9 min 52.98 sec)
Rows matched: 5000000 Changed: 5000000 Warnings: 0
mysql> ALTER TABLE client_order ADD INDEX date_client_idx (date, client_id);
Query OK, 0 rows affected (29.43 sec)
mysql> EXPLAIN SELECT SUM(total), client_id FROM client_order WHERE client_id IN (111929, 372530, 180565) AND created_at >= "2016-12-24 00:00:00" AND created_at <= "2016-12-25 24:59:59" GROUP BY client_id;
+----+-------------+--------------+------------+-------+----------------------------------+------------+---------+------+--------+----------+------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+-------+----------------------------------+------------+---------+------+--------+----------+------------------------------------+
| 1 | SIMPLE | client_order | NULL | range | client_idx,created_at_client_idx | client_idx | 5 | NULL | 281308 | 15.22 | Using index condition; Using where |
+----+-------------+--------------+------------+-------+----------------------------------+-----------
mysql> ALTER TABLE client_order DROP INDEX client_id_created_at_idx;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE client_order ADD INDEX created_at_client_id_idx (created_at, client_id);
Query OK, 0 rows affected (20.21 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> EXPLAIN SELECT SUM(total), client_id FROM client_order WHERE client_id IN (111929, 372530, 180565) AND created_at >= "2016-12-24 00:00:00" AND created_at < "2016-12-25 00:00:00" GROUP BY client_id;
+----+-------------+--------------+------------+-------+----------------------------------+-----------------------+---------+------+------+----------+--------------------------------------------------------+