idioms

Should we use Option or ptr::null to represent a null pointer in Rust?

余生长醉 提交于 2019-12-13 13:17:41
问题 The standard library's linked-list Node uses the Option type: struct Node<T> { next: Option<NonNull<Node<T>>>, prev: Option<NonNull<Node<T>>>, element: T, } and creates a node with this code: Node { next: None, prev: None, element, } The implementation of LeafNode of BTree , the standard library uses a raw pointer for the parent node: struct LeafNode<K, V> { parent: *const InternalNode<K, V>, parent_idx: MaybeUninit<u16>, len: u16, keys: MaybeUninit<[K; CAPACITY]>, vals: MaybeUninit<[V;

Avoiding unnecessary slice copying in Python

随声附和 提交于 2019-12-12 10:39:30
问题 Is there a common idiom for avoiding pointless slice copying for cases like this: >>> a = bytearray(b'hello') >>> b = bytearray(b'goodbye, cruel world.') >>> a.extend(b[14:20]) >>> a bytearray(b'hello world') It seems to me that there is an unnecessary copy happening when the b[14:20] slice is created. Rather than create a new slice in memory to give to extend I want to say "use only this range of the current object". Some methods will help you out with slice parameters, for example count : >

Pythonic way to have a choice of 2-3 options as an argument to a function

南楼画角 提交于 2019-12-12 08:25:48
问题 I have a Python function which requires a number of parameters, one of which is the type of simulation to perform. For example, the options could be "solar", "view" or "both. What is a Pythonic way to allow the user to set these? I can see various options: Use a string variable and check it - so it would be func(a, b, c, type='solar') Set some constants in the class and use func(a, b, c, type=classname.SOLAR) If there are only two options (as there are for some of my functions) force it into

How does the “#map(&proc)” idiom work when introspecting module classes?

丶灬走出姿态 提交于 2019-12-12 07:45:19
问题 Presenting the Idiom I found an interesting but unexplained alternative to an accepted answer. The code clearly works in the REPL. For example: module Foo class Bar def baz end end end Foo.constants.map(&Foo.method(:const_get)).grep(Class) => [Foo::Bar] However, I don't fully understand the idiom in use here. In particular, I don't understand the use of &Foo , which seems to be some sort of closure, or how this specific invocation of #grep operates on the result. Parsing the Idiom So far, I

How to save type information of template vector

北城以北 提交于 2019-12-12 06:25:46
问题 I am trying to implement a class for serialization (XML for now). The idea is that any derived class can registers its members with the base class and base can write the members in form of XML. The code looks something like this class IXMLINF { protected: struct INFObj { union MemPtr { int* piMem; char* pstrMem; IXMLINF* pINFMem; } MemPtr memObj; }; vec<INFObj*> m_INFObjVec; void addMemToINF(int* piMem) { INFObj* pObj = new INFObj; pObj->memObj.piMem = piMem; m_INFObjVec.append(pObj); } void

self-referencing ternary

前提是你 提交于 2019-12-11 19:22:42
问题 I've been doing this for a while: x = x if x else y In all sorts of contexts where x might be None , False , 0 , '' , [] , or {} . I know the purists would rather I: if not x: x = y but forget that, that's not my question. My question is: Is there anything 'wrong' with x = x if x else y besides the fact it's a ternary? Specifically, is it ok to have a ternary self-assign like that. NOTE My qustion is not is x = x if C else y ok. I know it is. Many thanks 回答1: Nope, nothing wrong with it. It's

how to test condition properly: je or jge

核能气质少年 提交于 2019-12-11 14:49:46
问题 I sometimes use this pattern to iterate array of something: mov [rsp+.r12], r12 ; Choose a register that calls inside the loop won't modify mov r12, -1 .i: inc r12 cmp r12, [rbp-.array_size] je .end_i ; ... program logic ... jmp .i .end_i: mov r12, [rsp+.r12] I understand that it is enough to test for equality but should not one "securely" test for "greater or equal"(prevent situation that will not occur). Should one use je or jge in this cases? I am asking about concrete tip that can reduce

What is the recommended idiom for creating a new file in the same directory as another with better.files?

浪尽此生 提交于 2019-12-11 10:45:42
问题 I'm enjoying the " better.files " library in my scala scripts, and one of the common operations (for me) is to create a new file in the same directory as an existing file. My first attempt turns out to be rather verbose and ugly, so I'm wondering if there's a better idiom. To be specific, here's a bash script that's the equivalent of what I'm wanting to do in scala: #!/bin/bash SRC="$HOME/Downloads/some-file.txt" SRCDIR=$(dirname "$SRC") DST="$SRCDIR/another-file.txt" echo "hello!" > "$DST"

Idiomatic way to copy cell values “down” in an R vector [duplicate]

醉酒当歌 提交于 2019-12-11 10:08:59
问题 This question already has an answer here : Closed 6 years ago . Possible Duplicate: Populate NAs in a vector using prior non-NA values? Is there an idiomatic way to copy cell values "down" in an R vector? By "copying down", I mean replacing NAs with the closest previous non-NA value. While I can do this very simply with a for loop, it runs very slowly. Any advice on how to vectorise this would be appreciated. # Test code # Set up test data len <- 1000000 data <- rep(c(1, rep(NA, 9)), len %/%

Create object only if some condition, otherwise return nullptr

半城伤御伤魂 提交于 2019-12-11 08:16:28
问题 I want to create an object only if some conditions are applied, otherwise retun nullptr. This is how I would do it in Delphi (2009+): function GetGen(n : integer) : Generics.Collections.TList<Integer>; var i : integer; begin result := nil; if n > 0 then begin result := Generics.Collections.TList<Integer>.Create; for i := 0 to n - 1 do result.Add(i); end; end; procedure TestGenList(n : integer); var aInt : integer; aGen : Generics.Collections.TList<Integer>; begin aGen := GetGen(n); if aGen =