Skip to content

Instantly share code, notes, and snippets.

View BSN4's full-sized avatar
🎯
Focusing

Bader BSN4

🎯
Focusing
View GitHub Profile
@BSN4
BSN4 / rock_paper_scissors_in_rounds.ex
Created November 26, 2024 18:33
rock_paper_scissors_in_rounds.ex
defmodule RPS.Game do
defmodule Score do
defstruct player: 0, computer: 0
end
def play do
%Score{}
|> play_round(1)
|> play_round(2)
|> play_round(3)
@BSN4
BSN4 / gist:bb4663363cbd3f9f4b85d89e3627752d
Created November 26, 2024 18:40 — forked from rxaviers/gist:7360908
Complete list of github markdown emoji markup

People

:bowtie: :bowtie: 😄 :smile: 😆 :laughing:
😊 :blush: 😃 :smiley: ☺️ :relaxed:
😏 :smirk: 😍 :heart_eyes: 😘 :kissing_heart:
😚 :kissing_closed_eyes: 😳 :flushed: 😌 :relieved:
😆 :satisfied: 😁 :grin: 😉 :wink:
😜 :stuck_out_tongue_winking_eye: 😝 :stuck_out_tongue_closed_eyes: 😀 :grinning:
😗 :kissing: 😙 :kissing_smiling_eyes: 😛 :stuck_out_tongue:
defmodule RPS do
defmodule Score, do: defstruct player: 0, computer: 0
def play do
%Score{}
|> Stream.iterate(&play_round/1)
|> Enum.at(3)
|> announce_winner()
|> maybe_play_again()
end
@BSN4
BSN4 / rock_paper_scissors_context.ex
Created November 26, 2024 20:05
rock_paper_scissors_context.ex
defmodule RPS do
defmodule Score, do: defstruct player: 0, computer: 0
defmodule Context do
defstruct [
score: %Score{},
rounds_to_play: 3,
valid_moves: [:rock, :paper, :scissors],
win_conditions: %{rock: :scissors, paper: :rock, scissors: :paper},
io: IO
@BSN4
BSN4 / rock_paper_scissors.rs
Created January 2, 2025 01:38
rock_paper_rust v1
use rand::Rng;
use std::io::{self, Write};
#[derive(Debug)]
struct Score {
player: u32,
computer: u32,
}
#[derive(Debug, PartialEq, Clone)]
@BSN4
BSN4 / rock_paper_scissors2.rs
Created January 2, 2025 01:39
rock_paper_scissors rust v2
use rand::Rng;
use std::io::{self, Write};
#[derive(Debug, Default)]
struct Score {
player: u32,
computer: u32,
}
#[derive(Debug, PartialEq, Clone, Copy)]
@BSN4
BSN4 / async.rs
Created January 8, 2025 10:37
async rust example
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{sleep, Duration};
// Our shared context that will be accessed by all workers
#[derive(Debug)]
struct SharedContext {
counter: i32,
last_updated_by: String,
}
@BSN4
BSN4 / async2.rs
Created January 8, 2025 11:17
rust async example
use std::sync::Arc;
use tokio::sync::{broadcast, Mutex};
use tokio::time::{sleep, Duration};
#[derive(Debug)]
struct SharedContext {
counter: i32,
last_updated_by: String,
}
@BSN4
BSN4 / owenership.md
Last active January 8, 2025 18:09
rust non-async ownership

Rust Borrowing and Ownership Rules

Struct Methods

Use &mut self when:

  • Modifying struct fields (e.g., update_score, play_round)
  • Maintaining state between calls

Use &self when:

  • Only reading values (e.g., get_player_move, announce_winner)
@BSN4
BSN4 / rust_async.md
Created January 8, 2025 18:36
Async Rust Practical Patterns and Gotchas

Async Rust Practical Patterns and Gotchas

Struct Patterns

// For shared state across tasks, these are your main tools:
struct MyService {
    // Immutable shared state
    config: Arc<Config>,
    // Mutable shared state