Skip to content

Instantly share code, notes, and snippets.

@gavr123456789
gavr123456789 / features.md
Last active August 13, 2022 21:17
Nim Podvodniy

Общее

Девиз: Efficient, Expressive, Elegant где под эффективностью понимается скорость С, под выразительностью синтаксис Python, а под элегантностью метапрограммирование Lisp https://nim-lang.org/

Структурная парадигма

Под структурной парадигмой автор понимает что у вас есть один вход в блок кода и один выход. То есть использовать return, break, continue не идиоматично  

Так например внутри цикла вместо continue по условию можно инвертировать условие избавившись от continue(но увеличив вложенность)

@gavr123456789
gavr123456789 / pipe.ts
Created January 15, 2022 14:35
pipe typescript
export interface Pipe<A> {
to<B, Rest extends unknown[]>(
fn: (value: A, ...args: Rest) => B,
...args: Rest
): Pipe<B>;
out<B, Rest extends unknown[]>(
fn: (value: A, ...args: Rest) => B,
@gavr123456789
gavr123456789 / functional ts.ts
Last active November 22, 2021 16:08
functional form rules ts
type Customer = {
name: string;
age: number;
}
type Rule<T> = (x: T) => null | string;
const ageOfMajority: Rule<number> =
x => x < 18 ? "Too young" : null;
// Bad example
interface IRemoteData<T> {
result?: T
error?: string
isLoading: boolean
isLoaded: boolean
}
// Same on OOP
@gavr123456789
gavr123456789 / vala.vala
Created February 7, 2021 13:39
Vala GTK 4 minimal example
int main (string[] args) {
var app = new Gtk.Application ("com.example.GtkApplication", ApplicationFlags.FLAGS_NONE);
app.activate.connect (() => {
var win = new Gtk.ApplicationWindow (app);
win.present ();
});
return app.run (args);
}
@gavr123456789
gavr123456789 / Create an instance of type from type.vala
Created January 5, 2021 02:12
Create an instance of type from type
class Customer: Object {
private string _firstName;
private string _surname;
public string firstName {
set { _firstName = value; prin(_firstName); }
}
public string surName {
set { _surname = value; prin(_surname); }
@gavr123456789
gavr123456789 / isTypesTheSame.vala
Created January 5, 2021 02:10
check if all types are the same
bool sus(params Type[] types){
if (types.length != 0){
var a = types[0];
for (var i = 1; i < types.length; ++i){
if (!a.is_a(types[i]))
return false;
else
a = types[i];
}
@gavr123456789
gavr123456789 / async_GTK_gui_example.vala
Last active August 15, 2023 20:50
async GTK 3 4 gui example vala nim
using Gtk;
Button button;
void main (string[] args) {
Gtk.init (ref args);
var window = new Window ();
button = new Button.with_label ("Start counting");
button.clicked.connect (() => {
namespace ObjectFactory {
private Gee.Map<Type, Type> _value_types;
public void set_value_type_for_prop_type (Type prop_type, Type value_type) {
if (_value_types == null) {
_value_types = new Gee.HashMap<Type, Type> ();
}
_value_types[prop_type] = value_type;
string task(string str){
var result = "";
unichar c;
for (int i = 0; str.get_next_char (ref i, out c);) {
if (c ==' ')result+="%20";
else result+=c.to_string();
}
return result;
}