These are chat archives for rust-lang/rust

3rd
Aug 2017
lemonxah
@lemonxah
Aug 03 2017 10:03
is there still issues with the syntax checking plugins for macros?
since this compiles and runs fine but the vscode plugin tells me the types are wrong
Sherab Giovannini
@Shaddy
Aug 03 2017 10:06
is there a way to produce items of a range in a custom iterator?
meaning, something like Struct { range: 0..10} and fn iter(...) { range.next() }
Jonas Platte
@jplatte
Aug 03 2017 10:08
@Shaddy: the type is named Range and can be found in std::ops. https://doc.rust-lang.org/std/ops/struct.Range.html
So you could have something like this:
struct MyIter {
    range: Range<usize>,
}
Sherab Giovannini
@Shaddy
Aug 03 2017 10:08
ok, thanks @jplatte :)
Jonas Platte
@jplatte
Aug 03 2017 10:09
impl MyIter {
    pub fn new() -> MyIter {
        MyIter { range: 0..10 }
    }
}
Sherab Giovannini
@Shaddy
Aug 03 2017 10:09
i'm trying to chain 3 ranges, let's see how it goes xD
Jonas Platte
@jplatte
Aug 03 2017 10:11
What do you mean by chaining? First iterating over the first range, then when it's depleted the second, then the third?
Sherab Giovannini
@Shaddy
Aug 03 2017 10:12
Something like this
for a in 0..10 {
 for b in 0..10 {
  for c in 0..10{
  }
 }
}
so each next() should produce the same behaviour
Jonas Platte
@jplatte
Aug 03 2017 10:14
Ah, so you want to not nest three loops and use a tuple instead? A sequence like (0, 0, 0) -> (0, 0, 1) -> (0, 0, 2) -> ... (0, 0, 9) -> (0, 1, 0) -> (0, 1, 1) -> ... (9, 9, 9)?
Sherab Giovannini
@Shaddy
Aug 03 2017 10:14
exactly
I'm producing a Triangle struct, which is the same as a 3 value tuple
Jonas Platte
@jplatte
Aug 03 2017 10:15
You can probably do that with iterator combinators instead of building your own iterator (if you want to, that is)
Sherab Giovannini
@Shaddy
Aug 03 2017 10:15
I tried with nested zip, but it only does with 1, 1, 1 => 2, 2, 2 etc
probably there is a way, but I think I will learn more about implementing custom iterators by doing this
xD
Jonas Platte
@jplatte
Aug 03 2017 10:15
I think you'd need two or three maps
let me see... ^^
Zakarum
@omni-viral
Aug 03 2017 10:16
(0..10).into_iter().flat_map(|x| (0..10).into_iter().flat_map(|y| (0..10).into_iter().map(|z| (x,y,z))
Try this
Sherab Giovannini
@Shaddy
Aug 03 2017 10:16
let me check
Jonas Platte
@jplatte
Aug 03 2017 10:16
Heh, @omni-viral beat me to it.
Zakarum
@omni-viral
Aug 03 2017 10:17
Myabe there is no need in into_iter if Range: Iterator
I don't quite remember
Sherab Giovannini
@Shaddy
Aug 03 2017 10:18
3 |     for item in (0..10).into_iter().flat_map(|x| (0..10).into_iter().flat_map(|y| (0..10).into_iter().map(|z| (x,y,z)))) {
  |                                                                               ---                              ^      - borrowed value only lives until here
  |                                                                               |                                |
  |                                                                               capture occurs here              does not live long enough
4 |         println!("item: {:?}", item);
5 |     }
  |     - borrowed value needs to live until here
Zakarum
@omni-viral
Aug 03 2017 10:19
fn main() {
    for (x, y, z) in (0..4).flat_map(move |x| (0..4).flat_map(move |y| (0..4).map(move |z| (x,y,z)))) {
        println!("x: {}, y: {}, z: {}", x, y, z);
    }
}
Sherab Giovannini
@Shaddy
Aug 03 2017 10:19
oh, themove
Zakarum
@omni-viral
Aug 03 2017 10:19
Adding some move
Jonas Platte
@jplatte
Aug 03 2017 10:19
Yeah x and y need moves, on the first closure you don't need it.
Zakarum
@omni-viral
Aug 03 2017 10:19
And remove into_iter
No need in it
Denis Lisov
@tanriol
Aug 03 2017 10:20
Jonas Platte
@jplatte
Aug 03 2017 10:20
right
Sherab Giovannini
@Shaddy
Aug 03 2017 10:21
xDDDD
there appear like 4 different solutions in 5minutes
Jonas Platte
@jplatte
Aug 03 2017 10:21
well, cartesian_product only works for two elements
yup, haha ^^
Zakarum
@omni-viral
Aug 03 2017 10:21
@tanriol I think cartesian_product do just same flat_mapping
@jplatte (0..10).cartesian_product(0..10).cartesian_product(0..10)
Denis Lisov
@tanriol
Aug 03 2017 10:22
Probably... there's actually an iproduct! macro in itertools.
Sherab Giovannini
@Shaddy
Aug 03 2017 10:22
how do I define this => (0..4).flat_map(move |x| (0..4).flat_map(move |y| (0..4).map(move |z| (x,y,z)))) in a struct?
Jonas Platte
@jplatte
Aug 03 2017 10:22
@tanriol This time I was faster! xD
Sherab Giovannini
@Shaddy
Aug 03 2017 10:22
wow
#[macro_use] extern crate itertools;
// Iterate over the coordinates of a 4 x 4 x 4 grid
// from (0, 0, 0), (0, 0, 1), .., (0, 1, 0), (0, 1, 1), .. etc until (3, 3, 3)
for (i, j, k) in iproduct!(0..4, 0..4, 0..4) {
   // ..
}
Jonas Platte
@jplatte
Aug 03 2017 10:23
@Shaddy Oh, you want to store the iterator instead of just iterating over it?
Sherab Giovannini
@Shaddy
Aug 03 2017 10:23
its just for idiomatic stuff
Denis Lisov
@tanriol
Aug 03 2017 10:23
@Shaddy Only as a boxed trait object, the type of closures cannot be written.
Sherab Giovannini
@Shaddy
Aug 03 2017 10:23
I prefer to do for triangle in looper::limit(1000) { triangle.is_pythagoric() ...}
being looper an iterator that produces triangles (0, 0, 0) => (0, 0, 1) ...
ok, so it's really hard to capture closure types and so on in a struct
not hard but messy
Jonas Platte
@jplatte
Aug 03 2017 10:25
Oh, just define a function
It does require boxing or nightly with impl_trait though
Sherab Giovannini
@Shaddy
Aug 03 2017 10:25
what I was trying to do basically is this:
pub fn find() -> Option<u32> {
    for triangle in Looper::new(1000) {
        if triangle.is_pythagoric() {
            println!("Triangle: {:?}", triangle);

            if triangle.sum() == 1000 {
                return Some(triangle.product())
            }
        }
    }

    None
}
this looks pretty idiomatic for me, and I can encapsulate the ugly stuff inside looper/triangle
but looks like next should be written with context's stored at self structs, so looping is not as expressive as using functional features
^ That's the impl Trait version. 0 overhead and a nice and short looper function :)
Sherab Giovannini
@Shaddy
Aug 03 2017 10:29
can't see your code since there is no gist hash xD
Jonas Platte
@jplatte
Aug 03 2017 10:29
whoops
Sherab Giovannini
@Shaddy
Aug 03 2017 10:29
:P
Jonas Platte
@jplatte
Aug 03 2017 10:29
but requires nightly
Sherab Giovannini
@Shaddy
Aug 03 2017 10:29
i use nightly always :)
Jonas Platte
@jplatte
Aug 03 2017 10:29
I even generated a gist URL, but then copied the url in my address bar..
Sherab Giovannini
@Shaddy
Aug 03 2017 10:30
lets see
damn man, I feel bad now
I was writting a lot of code, and you just simplified it really beauty XD
Jonas Platte
@jplatte
Aug 03 2017 10:31
haha, no reason to feel bad ^^
Also I didn't really do anything other than looking through some docs. The real heroes are the itertools authors :)
Sherab Giovannini
@Shaddy
Aug 03 2017 10:33
but you guys are really up to date into rust sexy features, thats awesome, thanks :)
Jonas Platte
@jplatte
Aug 03 2017 10:34
Yeah, that's one thing I've actually noticed here on Gitter (and on matrix) much more than on IRC
Not sure why, but there seem to be less people who will answer in a short timespan there
Sherab Giovannini
@Shaddy
Aug 03 2017 10:36
and with a deep knowledge
Jonas Platte
@jplatte
Aug 03 2017 10:36
But maybe that's just my skewed perception, because I only ask harder questions there..
anyway, gonna be afk for a bit now.
Sherab Giovannini
@Shaddy
Aug 03 2017 11:08
well, with your help @jplatte it looked really idiomatic

#![feature(conservative_impl_trait)] // this requires nigthly version

#[macro_use]
extern crate itertools;

#[derive(Debug)]
struct Triangle {
    a: u32,
    b: u32,
    c: u32,
}

impl Triangle {
    fn is_pythagoric(&self) -> bool {
        self.a.pow(2) + self.b.pow(2) == self.c.pow(2)
    }

    fn product(&self) -> u32 {
        self.a * self.b * self.c
    }

    fn sum(&self) -> u32 {
        self.a + self.b + self.c
    }

    fn generate(max: u32) -> impl Iterator<Item = Triangle> {
        iproduct!(1..max, 1..max, 1..max).map(|(x, y, z)| Triangle{a: x, b: y, c: z})
    }
}


pub fn find() -> Option<u32> {
    for triangle in Triangle::generate(1000) {
        if triangle.is_pythagoric() {

            if triangle.sum() == 1000 {
                return Some(triangle.product())
            }
        }
    }

    None
}
I'm wondering if there is a way to just optimize it (range1(1..max), range2(range1, max), range3(range1 + range2, max))
lemonxah
@lemonxah
Aug 03 2017 11:13
(1..max).flat_map(|x| (x..max)...).flat_map(|y | (y..max)..)...
wouldn't something like that be more optomized beacuse (1,2,3) and (2,1,3) is the same thing
and you have to check it twice
Sherab Giovannini
@Shaddy
Aug 03 2017 11:14
cant be 2, 1, 3 since a is 2, b will start always in 3
lemonxah
@lemonxah
Aug 03 2017 11:15
rather have (1, 1>, 1> ), (2, 2>, 2>)
Sherab Giovannini
@Shaddy
Aug 03 2017 11:15
0..x, a+1..x, b+a+1..x
lemonxah
@lemonxah
Aug 03 2017 11:15
not mine i say that his one 1..max 1..max 1..max
Sherab Giovannini
@Shaddy
Aug 03 2017 11:15
oh yes
thats why I was wondering if there was a way to optimize it
let me try
lemonxah
@lemonxah
Aug 03 2017 11:15
that is why suggest doing it this way so that you spend less time checking things that didn't work
Denis Lisov
@tanriol
Aug 03 2017 11:15
Maybe just do it the other way round - check only triangles with (a, b, 1000 - a - b)?
Sherab Giovannini
@Shaddy
Aug 03 2017 11:16
also
lemonxah
@lemonxah
Aug 03 2017 11:16
doing projecteuler?
Sherab Giovannini
@Shaddy
Aug 03 2017 11:16
exercism.io which shares a lot of euler's problems
the good stuff with exercism.io is that it provides already the project and some tests
and a commandline tool that allows to submit/fetch next exercise
lemonxah
@lemonxah
Aug 03 2017 11:20
is there something like haskell's list comprehension in rust? [(x,y,z) | x <- [1..], y <- [x..], z <- [y..], (x + z+ y) == 1000]
Sherab Giovannini
@Shaddy
Aug 03 2017 11:20
(1..max).flat_map(move |x| (x+1..max).flat_map(move |y| (max-x+y..max).map(move |z| Triangle{a: x, b: y, c: z})))
this seems to work, but don't find the cookie xD
lemonxah
@lemonxah
Aug 03 2017 11:21
maybe not +1 but just x
Sherab Giovannini
@Shaddy
Aug 03 2017 11:21
yep, don't as well
lemonxah
@lemonxah
Aug 03 2017 11:22
because you will miss 1,1,1 and 1,1,2 | 1,2,1 | 1,2,2
Denis Lisov
@tanriol
Aug 03 2017 11:22
@lemonxah Can be done with macros, but not as a special syntax.
sigh .. hold on
Sherab Giovannini
@Shaddy
Aug 03 2017 11:38
I got it working :)
the only remaining task is that if I test it at release, it works, but in debug alerts me that I could overflow
Sherab Giovannini
@Shaddy
Aug 03 2017 11:38
fn generate(max: u32) -> impl Iterator<Item = Triangle> {
    (1..max).flat_map(move |x| {
        (x + 1..max).flat_map(move |y| {
            ((max - (x + y))..max).map(move |z| Triangle { a: x, b: y, c: z })
        })
    })
}
added cmp::min and done
lemonxah
@lemonxah
Aug 03 2017 11:42
max - (x + y).. max are yous ure you want to do that?
lets say max is 100000 then your first triangle is going to be (1,2,99997)
Michal 'vorner' Vaner
@vorner
Aug 03 2017 11:50
I have a situation where I have a key→value mapping, with only few items. But I'll have a lot of these maps. So I'm looking at a way to eradicate needless heap allocations, if possible. I like the smallvec and linear_map crates. But does anyone know if there are similar crates that would combine them together?
Sherab Giovannini
@Shaddy
Aug 03 2017 11:54
I ended up with this, seems the most efficient:
    fn generate(max: u32) -> impl Iterator<Item = Triangle> {
        (1..max).flat_map(move |x| {
            (x + 1..max - x).map(move |y| {
                (Triangle {
                     a: x,
                     b: y,
                     c: (max - (x + y)),
                 })
            })
        })
    }
@lemonxah yes, thats what I wanted to do xD, finally got this last one
lemonxah
@lemonxah
Aug 03 2017 11:58
so for this one `generate(5) will be [Triangle(1,2,2), Triangle(1,3,1), Triangle(1,4,0), Triangle(2,3, .. no value will fit this
lemonxah
@lemonxah
Aug 03 2017 12:04
how do you convert Iterator to a vec? is this possitble?
Sergey Noskov
@Albibek
Aug 03 2017 12:05
lemonxah
@lemonxah
Aug 03 2017 12:06
.collect() did waht i wanted
@Shaddy just checking if this is expected behaviour generate(5) will only produce [Triangle { a: 1, b: 2, c: 2 }, Triangle { a: 1, b: 3, c: 1 }]
David Harvey-Macaulay
@alteous
Aug 03 2017 12:33
I want to mutate the iterator at each call to next and return the (borrowed) data to the user.
lemonxah
@lemonxah
Aug 03 2017 12:37
why would you want to do that though? what is the end goal? what if something is using a borrowed value and then you mutate the inner value?
David Harvey-Macaulay
@alteous
Aug 03 2017 12:42
@lemonxah The end goal is a bit complex to explain. I want to perform this split function in an iterator. The iterator would return (Option<Positioning>, etc.) instead of Primitive.
David Harvey-Macaulay
@alteous
Aug 03 2017 12:51
I'm just trying to avoid allocations. The problem could be solved by returning a Vec<ProcessedPrimitive>instead of performing Primitive -> ProcessedPrimitive in an iterator.
Never mind, I have a reasonable alternative instead.
Sherab Giovannini
@Shaddy
Aug 03 2017 13:16
@lemonxah yup, maybe it's a bit ad-hoc for this particular problem. I just updated it to make it faster but it should generate the entire range.
(1..max).flat_map(move |x| ...) y and z
love that flat_map function <3
lemonxah
@lemonxah
Aug 03 2017 13:17
functional programming is win
Sherab Giovannini
@Shaddy
Aug 03 2017 13:18
yes
i'm just getting used to (lets say) limitations of Rust over Python freedom
lemonxah
@lemonxah
Aug 03 2017 13:19
its not really Rust limitations, because coming from something like haskell Rust is less limited
Sherab Giovannini
@Shaddy
Aug 03 2017 13:19
but, as more Rust I discover, for some reason, I'm getting bored of Python (which is the most loved language by me so far)
I didn't knew Haskell or Scala, but it's pretty common nowadays to "modernize" languages by integrating the most sexy features of them
lemonxah
@lemonxah
Aug 03 2017 13:19
its more of paradigm limitations rather than a langauge limitation if that makes sense
Sherab Giovannini
@Shaddy
Aug 03 2017 13:20
i see
Kotlin is integrating modern functional features, TypeScript a more strong typed JS, and then all those crystal-lang, etc. languages that are modernizing themselves
lemonxah
@lemonxah
Aug 03 2017 13:21
TypeScript isn't strong typed JS, Its just type aware
because you can still fall into normal js in typescript files
Sherab Giovannini
@Shaddy
Aug 03 2017 13:22
yes, I know, but since it's applied to a dynamic language, its kinda like type constraints
the goal (guess) is to have a less buggy js code
i'm not a web developer or such like this, just went into him thanks to vscode so, can't really have an empiric opinion
lemonxah
@lemonxah
Aug 03 2017 13:23
not really, if you look at something like scala.js or purescript you will see that is type safe and strongly typed because you cant just use js :P but i guess we are getting a bit off topic
Sherab Giovannini
@Shaddy
Aug 03 2017 13:24
Rust is mainly developed under VsCode, and VsCode writes extensions in TypeScript, so...
just a joke xD
lemonxah
@lemonxah
Aug 03 2017 13:25
i use vim, does that mean i shouldn't be using rust? :(
heh
Sherab Giovannini
@Shaddy
Aug 03 2017 13:25
VsCode has a really nice Vim emulator, try it :P
I use vim emulator over VsCode, and it's one of the best emulators I've used so far
Jonas Platte
@jplatte
Aug 03 2017 13:26
@Shaddy Do you use multiple cursors though? IIRC the vim extensions messes with the ability to add cursors :(
Sherab Giovannini
@Shaddy
Aug 03 2017 13:26
if got used to do it with find and replace of vim, but if I want just some different behaviour you can just toggle vim emulator easily
lemonxah
@lemonxah
Aug 03 2017 13:26
i have not found a good vim emulator, i will stick with vim for now, i am more productive in vim
Sherab Giovannini
@Shaddy
Aug 03 2017 13:28
lets see if someone post an issue about that
Sherab Giovannini
@Shaddy
Aug 03 2017 13:38
@jplatte I'm testing multi-line cursor, and I have no problem.
you can add lines below, above, add word selection (Windows/MacKey + D) and so on
and vim actions (dw, x, w, etc) still work
Sherab Giovannini
@Shaddy
Aug 03 2017 13:52
btw, @lemonxah the C stuff aims to just generate Triangles that fits with a sum of 1000
thats why 1000 - (a + b)
Jonas Platte
@jplatte
Aug 03 2017 14:11
@Shaddy What about alt+lmb though?
Sherab Giovannini
@Shaddy
Aug 03 2017 14:15
never used, but I've tested right now, it works
Jonas Platte
@jplatte
Aug 03 2017 14:15
huh
Sherab Giovannini
@Shaddy
Aug 03 2017 14:15
I'm under OSX tho
Jonas Platte
@jplatte
Aug 03 2017 14:15
I'll try again..
Sherab Giovannini
@Shaddy
Aug 03 2017 14:15
xD
try to click the same work, if you double-click, it selects, if you single click, the cursor is under the mouse
could be weird if you click under white spaces tho
not sure
This is just awesome
#![feature(conservative_impl_trait)] // this requires nigthly version

fn divisors(number: u32) -> Box<Iterator<Item = u32>> {
    Box::new((1u32..number).filter(move |n| number % n == 0))
}
conservative_impl_trait leads to idiomatic code
Jonas Platte
@jplatte
Aug 03 2017 14:20
If you use Box you should remove the feature flag though ;)
Sherab Giovannini
@Shaddy
Aug 03 2017 14:22
xD
ok, I see
the background is that the features comes up to solve this
anyway, didn't find a way to do by impl trait
since it asks for lifetimes
Jonas Platte
@jplatte
Aug 03 2017 14:22
huh? this should be no problem.. let me check
Sherab Giovannini
@Shaddy
Aug 03 2017 14:23
If I remove the Box::new
ok
forget what I said
the problem was about the "move" error
Jonas Platte
@jplatte
Aug 03 2017 14:24
Yeah was about to say that
Sherab Giovannini
@Shaddy
Aug 03 2017 14:25
I think I should take an overview over Rust libraries to get more used to common features/code
James McCoy
@jamessan
Aug 03 2017 14:37
re: the VsCode discussion earlier, there's ongoing work directly embedding neovim instead of emulating vim :)
Jonas Platte
@jplatte
Aug 03 2017 14:40
@jamessan I already have "vim.enableNeovim": true in my config, that probably means I have that integration, right?
hmmmmm I don't like how the vim thing overwrites keyboard shortcuts
James McCoy
@jamessan
Aug 03 2017 14:41
assuming you have neovim installed, probably. I don't actually use VsCode, I just know about this from talking to the person doing the integration
Jonas Platte
@jplatte
Aug 03 2017 14:41
I've never used Ctrl+W in vim, and now I can't close tabs with that anymore
ah, alright
well enough off-topic talk, I should probably ask about this in another channel ^^
// Enable some vim ctrl key commands that override otherwise common operations, like ctrl+c
"vim.useCtrlKeys": true,
I can disable it! \o/
only issue is now ctrl+v pastes instead of going into visual block mode :D
BalaB
@BbalajiSg_twitter
Aug 03 2017 23:32
I am looking for "open-source toolkit for building REST/HTTP-based integration layers for Rust"
Need your recommendation